UVa-156 Ananagrams(map映射)

本文介绍了一个使用C++进行字符串处理和数据结构应用的例子,包括将字符串转换为小写并排序,利用map和set进行数据统计与管理。通过对输入的字符串进行预处理,实现了对重复字符串的有效识别。

参考:https://blog.youkuaiyun.com/hoaresky1998/article/details/51351588

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <set>
 5 #include <map>
 6 #include <algorithm>
 7 using namespace std;
 8 map<string,int> ma;
 9 set<string> sea;
10 string change(string x)
11 {
12     for (unsigned int i=0;i<x.length();i++)
13     {
14        x[i]=towlower(x[i]);
15     }
16     sort(x.begin(),x.end());//排序string类型的方法
17     return x;
18 }
19 int main()
20 {
21 //    freopen("in.txt","r",stdin);
22     string s;
23     while (cin>>s)
24     {
25         if (s=="#")
26         {
27             break;
28         }
29         sea.insert(s);//运用set自身的有序性
30         string temp=change(s);//转化为小写形式,且字母按字典序排序
31         if (!ma.count(temp))
32         {
33             ma[temp]=1;
34         }
35         else
36         {
37             ma[temp]++;
38         }
39     }
40     set<string>::iterator it;
41     for (it=sea.begin();it!=sea.end();it++)
42     {
43         if (ma[change(*it)]==1)
44         {
45             cout<<*it<<endl;
46         }
47     }
48 
49     return 0;
50 }

 

转载于:https://www.cnblogs.com/hemeiwolong/p/9600837.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值