UVa 156 Ananagrams

因为要字母重排,所以要把每一个单词标准化,即全部转化为小写字母后再进行排序。用map来统计标准化后的单词出现的次数,最后将值为1的单词加入到ans集合中。

(标准化必须是动态的,不能改变原来的单词!)

#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
using namespace std;

string s;
map<string,int> cnt;
vector<string> words;  //【不能】去重,所以用数组
set<string> ans;  //不需要去重,但需要排序,可以用集合 

string repr(string s) {
	for (int i=0; i<s.length(); i++) s[i]=tolower(s[i]);
	sort(s.begin(), s.end());
	return s;
}

int main()
{
	while (cin >> s && s!="#") {
		words.push_back(s);
		string re=repr(s);   //这里减少调用次数以增加运行速度 
		if (!cnt.count(re)) cnt[re]=1;
		else cnt[re]++;
	}
	for (int i=0; i<words.size(); i++)
		if (cnt[repr(words[i])] == 1) ans.insert(words[i]);
	
	for (set<string>::iterator it = ans.begin(); it != ans.end(); it++)   //迭代器 
		cout << *it << endl;
		
	return 0;
 } 


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值