HDOJ 1113 Word Amalgamation

本文转载于:笑着走完自己的路csdn

Word Amalgamation

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2818    Accepted Submission(s): 1354


Problem Description
In millions of newspapers across the United States there is a word game called Jumble. The object of this game is to solve a riddle, but in order to find the letters that appear in the answer it is necessary to unscramble four words. Your task is to write a program that can unscramble words.
 

Input
The input contains four parts:

1. a dictionary, which consists of at least one and at most 100 words, one per line;
2. a line containing XXXXXX, which signals the end of the dictionary;
3. one or more scrambled `words' that you must unscramble, each on a line by itself; and
4. another line containing XXXXXX, which signals the end of the file.

All words, including both dictionary words and scrambled words, consist only of lowercase English letters and will be at least one and at most six characters long. (Note that the sentinel XXXXXX contains uppercase X's.) The dictionary is not necessarily in sorted order, but each word in the dictionary is unique.
 

Output
For each scrambled word in the input, output an alphabetical list of all dictionary words that can be formed by rearranging the letters in the scrambled word. Each word in this list must appear on a line by itself. If the list is empty (because no dictionary words can be formed), output the line ``NOT A VALID WORD" instead. In either case, output a line containing six asterisks to signal the end of the list.
 

Sample Input
 
   
tarp given score refund only trap work earn course pepper part XXXXXX resco nfudre aptr sett oresuc XXXXXX
 

Sample Output
 
   
score ****** refund ****** part tarp trap ****** NOT A VALID WORD ****** course ******
 
 
这题看了好几天了,之前找到各种题解,有用map映射做的,有用qsotr的,大部分都是C++做法。本渣太弱,题解都看不懂。
还好看了振宇大神博客,他的方法简单易懂。   长见识了,原来sort还可以对字符串进行排序。
 
 
题目的意思是:给出一个以XXXXXX为结尾的字符串集作为字典,再输入一个以XXXXXX为结尾的字符串集作为要在字典中查找的目标字符串。判断目标字符串是否是由字典中的字符串经过变换得到的。
 
 
思路:按照字典序变换并记录 字符串集里面的每个字符串,对于目标字符串 也按字典序排列,并找出 与 排序后的目标字符串 相等的串存储其变换前对应字符串,按字典序排序后输出即可。
 
 
具体代码如下:
 
 
#include
#include
#include
using namespace std;
struct rec
{
	char st[10];
}num[110];
char s[110][10],cpy[110][10],str[10];

bool cmp(rec a,rec b)//利用结构体储存一个目标字符串在字典中有多个字符串对应的情况 
{
	return strcmp(a.st,b.st)<0;//该种情况下按照strcmp函数的升序输出 
}

int main()
{
	int n=0,i,t,sign;
	while(scanf("%s",s[n])&&strcmp(s[n],"XXXXXX")!=0)
	{
		strcpy(cpy[n],s[n]); 
		sort(cpy[n],cpy[n]+strlen(cpy[n]));//对字典中的字符串进行排序 
		n++;
	}
	while(scanf("%s",str)&&strcmp(str,"XXXXXX")!=0)
	{
		t=0;//记录字典中与目标字符串匹配字母的个数 
		sign=0;//标记是否有匹配字符串 
		sort(str,str+strlen(str));
		for(i=0;i

 

你可能感兴趣的:(结构体排序,字符串小题)