POJ 1318 字典排序
2016-07-24 17:25
246 查看
Word Amalgamation
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 9164 Accepted: 4375
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
******
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 9164 Accepted: 4375
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
******
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <stdlib.h> using namespace std; //const int MAX = 120; char str1[120][120],str2[120][120],str3[120][120]; int cmp(const void *a, const void *b) { return (*(char*)a)-(*(char*)b); } int cmp1(const void *a,const void *b) //对字典里的单词打乱按照字典序排序,方便比较 { return strcmp((char*)a,(char*)b); } int main() { int i,j,flag; int k = 0,n=0; while(gets(str1[k]),strcmp(str1[k],"XXXXXX")){ k++; } qsort(str1,k,sizeof(str1[0]),cmp1); //对str1的多个字符串进行升序排序 题目要求匹配到的按照字典序输出 for(i=0;i<k;i++) { strcpy(str2[i],str1[i]); //复制排好的多个字符串 qsort(str2[i],strlen(str2[i]),sizeof(str2[i][0]),cmp);//把每个字符串的字符按照最小的字典序重新排列,组成方便比较的新的字符串 } while(cin>>str3 ,strcmp(str3 ,"XXXXXX")) { qsort(str3 ,strlen(str3 ),sizeof(str3 [0]),cmp);//排序输入的打乱的字符串后,方便比较 n++; } for(i=0;i<n;i++) { flag=0; for(j=0;j<k;j++) { if(string (str3[i])==string (str2[j]))//关键!数组之间不能比较,需转换成string类型 { cout<<str1[j]<<endl;//str1和str2是对应的,str1是排序好的字典库的原字符串,str2是每个字符串打乱了(因为排序了) flag=1; } } if(!flag) { cout<<"NOT A VALID WORD"<<endl; cout<<"******"<<endl; } else cout<<"******"<<endl; // for(i=0;i<=k;i++) // printf("%s",str1[i]); } return 0; }改进后的代码,用了next_permutation(ss,ss+ll)感觉省了很多啊,还是能多用模板就多用
#include <iostream> #include <algorithm> #include <cstring> using namespace std; int main() { char s[100][20],ss[20]; int i,j,ls,ll; ls=0; while(cin>>s[ls++]) if (string(s[ls-1])=="XXXXXX") break; while(cin>>ss) { if (string(ss)=="XXXXXX") break; ll=strlen(ss); j=0; sort(ss,ss+ll);//把单词打乱按照字典序存下现有字符所能组成最小的序列 //因为下面的next_permutation()函数是按照从小到大依次举出, //比原序列小的不能举出 do { //cout<<"-----"<<endl; for(i=0;i<ls;i++) if (string(s[i])==string(ss)) { cout<<ss<<endl; j=1; } } while(next_permutation(ss,ss+ll)); if (j==0) cout<<"NOT A VALID WORD"<<endl; cout<<"******"<<endl; } return 0; }
相关文章推荐
- php 自动加载类方法小结
- 深入理解Java的接口和抽象类
- No.001 Two Sum
- 《大话数据结构》读书笔记
- poj1182 并查集
- 树的面试题(一):二叉树的镜像
- Java三大框架之struts的验证框架(验证字段)
- Observer观察者模式
- ORACLE数据库---多表链接查询
- debian彻底卸载软件
- invalidate()和postInvalidate() 的区别
- Android Device Administration 设备管理器——实现一键锁屏
- 287. Find the Duplicate Number
- [php]include()和require()区别【学习笔记】
- Failed to load the LayoutLib: com/android/layoutlib/bridge/Bridge : Unsupported major.minor version
- HDU1251统计难题 trie树
- RDD-Transformation——groupByKey
- Android Device Administration 设备管理器——实现一键锁屏
- No.002 Add Two Numbers
- <hdu - 1002> A + B Problem II