输入一个字典(用******结尾),然后再输入若干单词。每输入一个单词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 阅读(1387) 评论(0)  编辑 收藏 引用 所属分类: ACM Experience

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理