zoukankan      html  css  js  c++  java
  • 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<<"	"<<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;
    }
    
  • 相关阅读:
    [BZOJ]4810: [Ynoi2017]由乃的玉米田
    VK Cup 2017
    Educational Codeforces Round 19
    [BZOJ]4162: shlw loves matrix II
    2017-4-14校内训练
    第一次 CSP-S 的游记
    APIO2009 采油区域
    NOIP2017 逛公园
    NOIP2013 货车运输
    【9018:1458】征兵
  • 原文地址:https://www.cnblogs.com/pengwill/p/7367219.html
Copyright © 2011-2022 走看看