HDOJ.1113 Word Amalgamation(map)

Word Amalgamation

点我挑战题目
点我一起学习STL-MAP

题意分析

给出字典。之后给出一系列======乱序======单词,要求你查字典,如过这个乱序单词对用有多个有序单词可以输出,那么按照字典序将其输出。 若没有对应单词,输出NOT A VALID WORD。
可见这是一组组对应关系,可以用map来实现。map字典中first保存原本的单词(因为first按字典序),second保存原本单词排序后的单词。每次读入一个乱序单词后,sort遍历map并查找和second匹配的NODE,并输出其first即可。

代码总览

/*
    Title:HDOJ.1113
    Author:pengwill
    Date:2016-11-21
*/
#include <iostream>
#include <map>
#include <algorithm>
#include <string>
using namespace std;
string s,b,str;
typedef map<string,string> mp;
mp p;
int main()
{
    cin.sync_with_stdio(false);
    cin.tie(0);
    freopen("in.txt","r",stdin);
    int t = 0;
    while(cin>>str){
        if(str.compare("XXXXXX") == 0 && t== 0){
            t++;
        }else if(str.compare("XXXXXX") == 0 && t== 1){
            break;
        }else if(!t){
            //读入字典
            s =  b = str;
            sort(b.begin(),b.end());
            p[s] = b;
        }else if(t){
            //输出结果
            int judge = 0;
            s = str;
            sort(s.begin(),s.end());
            mp::iterator iter;
            for(iter = p.begin() ; iter!=p.end() ; iter++){
                    //cout<<iter->first<<"\t"<<iter->second<<endl;
                    if(iter->second == s){
                        cout<<iter->first<<endl;
                        judge  = 1;
                    }


            }
            if(judge){
                cout<<"******"<<endl;
            }else{
                cout<<"NOT A VALID WORD"<<endl<<"******"<<endl;
            }
        }
    }
    return 0;
}
posted @ 2016-11-21 14:22  pengwill  阅读(116)  评论(0编辑  收藏  举报