UVA 156 反片语

UVA 156 反片语

题目描述

输入一些单词,找出所有满足如下条件的单词:该单词不能通过字母重排,得到输入文 本中的另外一个单词。在判断是否满足条件时,字母不分大小写,但在输出时应保留输入中 的大小写,按字典序进行排列(有大写字母在所有小写字母的前面)。
样例输入

ladder came tape soon leader acme RIDE lone Dreis peat ScAlE orb eye Rides dealer NotE derail LaCeS drIed noel dire Disk mace Rob dries #

样例输出

Disk NotE derail drIed eye ladder soon(为节约空间不予以换行)

大致意思就是所有的单词都转为小写,找出那种字母重新排序后和其他的单词都不一样的
先把每次的单词变成小写, 然后将其存入map的键, 值就是原单词.

//把每个单词“标准化”,即全部转化为小写字母后再进行排序 
#include<iostream>
#include<cstring>
#include<vector>
#include<map>
#include<cctype>
#include<algorithm>
using namespace std;
map<string,int> cnt;
vector<string> words;
//首先将单词标准化(转换为小写) 
string repr(const string &s)
{
	string ans=s;
	for(int i=0;i<ans.length();i++)
	{
		ans[i]=tolower(ans[i]);
	}
	sort(ans.begin(),ans.end());
	return ans;
}
int main()
{
	int n=0;
	string s;
	while(cin>>s)
	{
		if(s[0]=='#') break;
		words.push_back(s);   //words装的是原单词 
		string r=repr(s);  //r标准化的s 
		if(!cnt.count(r)) cnt[r]=0; //没有匹配的 ,就创建一个  对于字符串r,关联数组中没有,就新建一个cnt[r],它的值为零。
		else cnt[r]++; 
	}
	vector<string> ans;
	for(int i=0;i<words.size();i++)
	if (cnt[repr(words[i])]==0)  
	ans.push_back(words[i]);      //用一个vector储存所有标准化后只出现一次的词,排序后输出即为答案
	sort(ans.begin(), ans.end());
	for(int i = 0; i < ans.size(); i++) 
	cout << ans[i] << "
";
	return 0;
}

原文地址:https://www.cnblogs.com/serendipity-my/p/12643381.html