您的位置:首页 > 其它

uva 156 Ananagrams(字符串模拟搜索)

2014-07-02 00:22 429 查看
题意是找到一个字符串里面所有可以重排的单词,最后字典序输出。

把所有单词找出来,全换成小写,并将字母排序。

搜索排序后一样的单词,标记。

最后没上标记的字典序输出就行了。

脑袋糊糊的,写的乱七八糟的。

代码:

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
const int Maxn = 1010;

struct String
{
char str[100];
char low[100];
bool no;//记录单词是否存在
} s[Maxn];

struct Ans//保存结果
{
char str[100];
} a[Maxn];

//字典序排序
bool cmp(Ans a, Ans b)
{
return strcmp(a.str, b.str) < 0;
}

int main()
{
#ifdef LOCAL
freopen("in.txt", "r", stdin);
#endif // LOCAL
int cnt = 0;
while (scanf("%s", s[cnt].str) != EOF)
{
if (s[cnt].str[0] == '#')
break;
//将大写转换成小写
for (int i = 0; i < strlen(s[cnt].str); i++)
{
if (s[cnt].str[i] <= 'Z' && s[cnt].str[i] >= 'A')
s[cnt].low[i] = s[cnt].str[i] + 32;
else
s[cnt].low[i] = s[cnt].str[i];
}
//将输入的单词字母排序,排序后单词存在low中,原单词在str中
sort(s[cnt].low, s[cnt].low + strlen(s[cnt].str));
//标记为字符串中未出现
s[cnt++].no = true;
}
//搜索
for (int i = 0; i < cnt; i++)
{
for (int j = i + 1; j < cnt; j++)
{
//若存在重排单词 即 单词出现
if (strcmp(s[i].low, s[j].low) == 0)
{
s[i].no = false;
s[j].no = false;
}
}
}
int n = 0;
for (int i = 0; i < cnt; i++)
{
//若单词未出现
if (s[i].no)
{
strcpy(a[n++].str, s[i].str);
}
}
//字典序
sort(a, a + n, cmp);
for (int i = 0; i < n; i++)
printf("%s\n", a[i].str);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: