#include <iostream>
#include <cstdio>
#include <string>
#include <cctype>
#include <sstream>
#include <algorithm>
using namespace std;
const int maxn=85;
struct Words
{
string changedWord;
string word;
}words[maxn*maxn];
string anagrams[maxn*maxn];
void change(int m)
{
words[m].changedWord="";
for(int i=0;i<words[m].word.size();i++)
{
words[m].changedWord+=tolower(words[m].word[i]);
}
sort(words[m].changedWord.begin(),words[m].changedWord.end());
}
bool cmp_words(const Words &a,const Words &b)
// Compilation error
//sort中比较函数定义时,传入的参数必须为const常量
{
return a.changedWord<b.changedWord;
}
int main()
{
/*
freopen("156.in","r",stdin);
freopen("156.out","w",stdout);
*/
string line;
int u=0;
while(getline(cin,line))
{
if(line=="#")
break;
istringstream sin(line);
string a;
while(sin>>a)
{
words[u].word=a;
change(u);
u++;
}
}
sort(words,words+u,cmp_words);
int num=1;
int v=0;
for(int i=0;i<u;i++)
{
if(words[i].changedWord==words[i+1].changedWord)
num++;
else
{
if(num==1)
{
anagrams[v++]=words[i].word;
}
else
num=1;
}
}
sort(anagrams,anagrams+v);
for(int i=0;i<v;i++)
cout<<anagrams[i]<<endl;
return 0;
}
156 - Ananagrams
最新推荐文章于 2021-04-05 18:20:39 发布
本文介绍了一个使用C++编写的程序,该程序能够读取一行文本输入,并将其中的单词按字母顺序重新排列形成新的单词组合。通过将每个单词转换为小写并排序,程序可以找出具有相同字母组成的单词,即互为字谜(anagrams)的单词,并将这些单词进行归类。
505

被折叠的 条评论
为什么被折叠?



