UVa 156(map)

Sample input

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

#

 Sample output

Disk

NotE

derail

drIed

eye

ladder

soon

扫描二维码关注公众号,回复: 2277833 查看本文章

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

#include <iostream>
#include <cstring>
#include <cctype>
#include <vector>
#include <map>
#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]); //ps:toupper()是将小写转大写 
	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); //存入vector 
		string r=repr(s);
		if(!cnt.count(r)) cnt[r]=0;  
		cnt[r]++;
	}
	vector<string> ans;
	for(int i=0;i<words.size();i++)
		if(cnt[repr(words[i])]==1) ans.push_back(words[i]); 
	sort(ans.begin(),ans.end());
	for(int i=0;i<ans.size();i++) cout<<ans[i]<<endl;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/henu_xujiu/article/details/81133980