HDOJ HDU 1113 Word Amalgamation

来源:互联网 发布:网络大电影投资公司 编辑:程序博客网 时间:2024/06/05 11:08

HDOJ 1113 Word Amalgamation

题目

点此查看 HDOJ 1113 Word Amalgamation

分类

模拟
字符处理

题意

给出 几个词 做字典
接下来给出的词 找 字典里 的 词 是否存在 乱序 后的该词 存在 输出字典中的词根
字典
tarp
refund
trap
part

nfudre 输出 refund
aptr 输出
part
tarp
trap
sett 输出 NOT A VALID WORD
注意第二个词 的输出 有多个词根时要按字典序全部输出

题解

按题意模拟即可

技巧

怎么样对比
词典和词呢
我们可以将词典和词全部 “标准化”
由于词是乱序过的
那么 我们排序在比较即可
标准化的词根要对应词库中的词 由于是 一对多的关系 可以用 mutimap实现

代码

#include <iostream>#include <queue>#include <algorithm>#include <map>using namespace std;multimap<string,string> m;string Amalgamation(string s);bool cmp(const string & a,const string & b){    return a < b;    };priority_queue<string,vector<string>,greater<string> > q;int main(){    string line;    while(getline(cin,line) && line != "XXXXXX")    {        string key = Amalgamation(line);        m.insert(pair<string,string>(key,line));    }    while(getline(cin,line) && line != "XXXXXX")    {        string key = Amalgamation(line);        int num = m.count(key);        if(num == 0)            cout << "NOT A VALID WORD" << endl;        else        {            multimap<string,string>::iterator it;            it = m.find(key);            for(int i = 0;i < num;i++)            {//                cout << (*it).second << endl;]                q.push((*it).second);                it++;            }            while(!q.empty())            {                cout << q.top() << endl;                q.pop();            }        }        cout << "******" << endl;    }    return 0;}string Amalgamation(string s){//    string am;    sort(s.begin(),s.end());//    am += s[0];/*    for(int i = 1;i < s.length();i++)    {        if(s[i] == s[i-1])            continue;        am += s[i];    }*/    return s;}