码迷,mamicode.com
首页 > 其他好文 > 详细

UVA 156(STL_G题)解题报告

时间:2018-01-21 01:14:42      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:space   string   ble   数据结构   src   font   结果   http   begin   

题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=92

----------------------------------------------------------------------------------------------------------------------------------------------------------------

题意:对于一段文字,其中有些词语是乱序的,要求输出只出现一次的单词,按字典序。

思路:对于每种字符串,保存后将其转为小写,在按字典序排序。如果两个不同字符串按照上述操作进行后为相同的字符串,认为该两个字符串为相同字符串。同时需要保存排序前和排序后的结果,便于之后查照删除,于是采用了map的数据结构。

代码:

技术分享图片
#include<cstdio>
#include<sstream> 
#include<algorithm>
#include<set>
#include<iostream>
#include<string>
#include<map>
using namespace std;
set<string> s2;
set<string>s1;
map<string,string>m1;
map<string,string>::iterator iter;
int main(void){
    string cur ="0";
    string s ="0";
    while(getline(cin,s)){
        if (s=="#")    break;
        stringstream input1(s);
        while(input1>>cur){
            string cs = cur;
            for(int i=0;i<cur.size();i++){
                cur[i]=tolower(cur[i]);
            }
            sort(cur.begin(),cur.end());
            m1.insert(pair<string, string>(cur,cs));
            if(s1.find(cur)==s1.end()){
                s2.insert(cs);
                s1.insert(cur);
            }else{
                iter = m1.find(cur);
                if(iter!=m1.end())
                {
                    s2.erase(iter->second);
                } 
            }
        }
    }

    set<string>::iterator it; 
    for(it=s2.begin();it!=s2.end();it++) 
    cout<<*it<<endl;    
    return 0;

}
View Code

 

UVA 156(STL_G题)解题报告

标签:space   string   ble   数据结构   src   font   结果   http   begin   

原文地址:https://www.cnblogs.com/caomingpei/p/8322400.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!