輸入一個字典(用******結尾),然后再輸入若干單詞。每輸入一個單詞w,你都需要在字典中找出所有可以用w的字母重排后得到的單詞,并按照字典序從小到大的順序在一行中輸出(如果不存在,輸出:( )輸入單詞之間用空格或空行隔開。注意,字典中的單詞不一定按字典排列。
樣例輸入:
tarp given score refund only trap work earn course pepper part
******
resco nfudre aptr sett oresuc
樣例輸出:
score
refund
part tarp trap
:(
course
代碼:
#include <iostream>
#include <algorithm>
#include <string>
#include <string.h>
const int maxn = 1000+10;
using std :: string;
using std :: sort;
using std :: cin;
using std :: cout;
using std :: endl;
int main()
{
string word[maxn]; //用string類,方便對字符串排序
char sorted[maxn][maxn],buf[maxn];
int i = 0;
for( ; ; i++)
{
cin >> word[i];
if(word[i][0]=='*') break; //把字典存入word[]中
}
sort(word,word+i); //先把字典排序
for(int t=0; t<i; t++)
{
strcpy(sorted[t],word[t].c_str()); //把字典中的數據存入另一個數組里
sort(sorted[t],sorted[t]+strlen(sorted[t])); //把存儲完的字典中每一個單詞的字母排序
}
while(~scanf("%s", buf))
{
bool found = 0 ;
sort(buf,buf+strlen(buf)); //把讀入的字符串按字母排序
for(int t=0; t < i ; t++)
{ //從排序后的字典中查找
if(!strcmp(buf,sorted[t])) //如果找到
{
found = 1;
cout << word[t] << " " ; //輸出原字典中對應的單詞
}
}
if(!found) cout << ":(--->>>Not found"; //如果沒有找到則輸出Not found
cout << endl;
}
return 0;
}
posted on 2010-05-26 18:32
Vontroy 閱讀(1425)
評論(0) 編輯 收藏 引用 所屬分類:
ACM Experience