Word Amalgamation(hdoj1113)
2015-05-29 21:53
232 查看
Word Amalgamation
[align=left]Problem Description[/align]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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
tarp
given
score
refund
only
trap
work
earn
course
pepper
part
XXXXXX
resco
nfudre
aptr
sett
oresuc
XXXXXX
[align=left]Sample Output[/align]
score
******
refund
******
part
tarp
trap
******
NOT A VALID WORD
******
course
******
#include<cstdio> #include<iostream> #include<cstring> #include<algorithm> using namespace std; int cmp(int n,char *p,char *q) { sort(p,p+n); sort(q,q+n); if(0==strcmp(p,q)) return 1; else return 0; } int main() { char a[102][7],b[102][7],c[102][7],i=0,j,count1,count2; bool flags=0; // freopen("in.txt","r",stdin); while(gets(a[i])) { if(!strcmp(a[i],"XXXXXX")) break; else i++; } count1=i,i=0;/*个数*/ while(gets(b[i])) { if(!strcmp(b[i],"XXXXXX")) break; else i++; } count2=i; for(i=0;i<count2;i++) { int k=0; int lenb=strlen(b[i]); char bb[7]; strcpy(bb,b[i]); for(j=0;j<count1;j++) { int lena=strlen(a[j]); char aa[7]; strcpy(aa,a[j]); if((lena==lenb)&&cmp(lena,bb,aa)) { strcpy(c[k],a[j]); k++; flags=1; } } char uu[7]; int p; for(j=0;j<k-1;j++) for(p=j+1;p<k;p++) { if(strcmp(c[j],c[p])>=0) { strcpy(uu,c[j]); strcpy(c[j],c[p]); strcpy(c[p],uu); } } for(j=0;j<k;j++) cout<<c[j]<<endl; if(flags) cout<<"******"<<endl; else cout<<"NOT A VALID WORD"<<endl<<"******"<<endl; flags=0; } }
相关文章推荐
- http://www.zhihuishi.com/class/1
- Git学习笔记之Git常用命令
- Matlab 常用命令记录
- zookeeper伪分布式集群搭建
- Subline Text 使用技巧
- ScrollView+ListView滚动冲突,没有滑动效果 解决办法
- 站立会议05(第二阶段)
- win7(windows) 下 git bash 调用 notepad++ 或者其他编辑器的方法
- B\S备忘录16——tomcat压缩版安装
- recursion demo
- nyoj 86 找球号 (一)
- 类族的设计
- MacPorts基础教程
- PHP中常量总结
- Hadoop到底能做什么?怎么用hadoop?
- poj 1106 Transmitters
- java简易编辑器
- SQL小结(二)
- 第十七天:还是出差
- 新手入门iOS,浅谈学习经验