Problem : Word Amalgamation
Description:给你一个字典,以“XXXXXX”结束字典的输入。再给出一些乱序的单词,找出这些单词在字典中所对应的单词。
Solution:对字典里的单词内部按字典序排一次序,然后对后来输入的乱序的单词也排一次序。排好序后到字典中排过序的数组中去找,如果一样则输出原来的单词。
Code(C++):
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
string strd[105];
string dict[105];
string ans[105];
string str,str1;
int k=0;
int i,t;
while(cin>>str1,str1!="XXXXXX")
{
strd[k]=str1;
dict[k]=str1;
sort(dict[k].begin(),dict[k].end());
k++;
}
while(cin>>str,str!="XXXXXX")
{
for(i=0;i<105;i++)
ans[i].clear();
t=0;
sort(str.begin(),str.end());
for(i=0; i<k; i++)
{
if(dict[i]==str)
{
ans[t++]=strd[i];
}
}
if(t==0)
cout<<"NOT A VALID WORD"<<endl;
sort(ans,ans+t);
for(i=0;i<t;i++)
cout<<ans[i]<<endl;
cout<<"******"<<endl;
}
return 0;
}