UVA156Ananagrams

uva-156
题目:
Most crossword puzzle fans are used toanagrams— groups of words with the same letters in differentorders — for example OPTS, SPOT, STOP, POTS and POST. Some words however do not have thisattribute, no matter how you rearrange their letters, you cannot form another word. Such words arecalledananagrams, an example is QUIZ.Obviously such definitions depend on the domain within which we are working; you might thinkthat ATHENE is an ananagram, whereas any chemist would quickly produce ETHANE. One possibledomain would be the entire English language, but this could lead to some problems. One could restrictthe domain to, say, Music, in which case SCALE becomes arelative ananagram(LACES is not in thesame domain) but NOTE is not since it can produce TONE.Write a program that will read in the dictionary of a restricted domain and determine the relativeananagrams. Note that single letter words are, ipso facto, relative ananagrams since they cannot be“rearranged” at all. The dictionary will contain no more than 1000 words.InputInput will consist of a series of lines. No line will be more than 80 characters long, but may contain anynumber of words. Words consist of up to 20 upper and/or lower case letters, and will not be brokenacross lines. Spaces may appear freely around words, and at least one space separates multiple wordson the same line. Note that words that contain the same letters but of differing case are considered tobe anagrams of each other, thus ‘tIeD’ and ‘EdiT’ are anagrams. The file will be terminated by a lineconsisting of a single ‘#’.OutputOutput will consist of a series of lines. Each line will consist of a single word that is a relative ananagramin the input dictionary. Words must be output in lexicographic (case-sensitive) order. There will alwaysbe at least one relative ananagram.Sample Inputladder came tape soon leader acme RIDE lone Dreis peatScAlE orb eye Rides dealer NotE derail LaCeS drIednoel dire Disk mace Rob dries#Sample OutputDiskNotEderaildrIedeyeladdersoon
ac代码:

#include<iostream>
#include<cstdio>
#include<string>
#include<string.h>
#include<vector>
#include<map>
#include<algorithm>
using namespace std;
const int N=1000+10;
map<string,int> d;
vector<string> word,ret;
string dealstr(const string &s ){
	string str=s;
	int len=str.size();
	for(int i=0;i<len;i++){
		//不管大小写,全部转化成小写,然后判断是不是符合题意 
		str[i]=tolower(str[i]);
	}
	sort(str.begin(),str.end());
	return str;
}
int main(){
	string s;
	while(cin>>s){
		if(s[0]=='#') break;
		string str=dealstr(s);
		d[str]++;
		word.push_back(s);
	}
	int l=word.size();
	for(int i=0;i<l;i++){
		if(d[dealstr(word[i])]==1)//如果某个元素出现次数仅仅是1,那就把这个元素放到答案数组里面 
			ret.push_back(word[i]);
	}
	sort(ret.begin(),ret.end());
	int l1=ret.size();
	for(int j=0;j<l1;j++){
		cout<<ret[j]<<endl;
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值