您的位置:首页 > 其它

UVA - 156 Ananagrams

2016-07-09 18:29 471 查看
题目大意:输入一些单词,按字典序顺序输出无视大小写及字母顺序后,只出现一次的单词。如 STOP、SOPT、spTO 算重复出现。

解题思路:保留输入单词,把输入的单词化为小写后对字符串排序,这时候出现字母个数相同的单词会被排成一样,然后遍历查找是否有相同单词,没有的话把对应原单词存于一个二维数组中,对二维数组排序后输出。

#include<iostream>
#include<cstdio>
#include<string.h>
#include<stdlib.h>
#include<cmath>
using namespace std;
char word[100][50];
char tmp[100][50];
char ans[100][50];
int num[100];
int tot = 0;
int cmp(const void*a,const void*b) {
return *(char*)a - *(char*)b;
}
int comp(const void*a,const void*b) {
return strcmp((char*)a,(char*)b);
}
int main() {
while(scanf("%s", &word[tot]) && strcmp(word[tot], "#")) {
for (int i = 0; i < strlen(word[tot]); i++)
if (word[tot][i] >= 'A' && word[tot][i] <= 'Z')
tmp[tot][i] = word[tot][i] - 'A' + 'a';
else tmp[tot][i] = word[tot][i];
qsort(tmp[tot], strlen(tmp[tot]), sizeof(tmp[tot][0]), cmp);
tot++;
}
for (int i = 0; i < tot - 1; i++)
for (int j = i + 1; j < tot; j++)
if (strcmp(tmp[i], tmp[j]) == 0) {
num[i]++; num[j]++;
}
int j = 0;
for (int i = 0; i < tot; i++)
if(num[i] == 0) strcpy(ans[j++], word[i]);
qsort(ans, j, sizeof(ans[0]), comp);
for (int i = 0 ; i < j; i++)
printf("%s\n", ans[i]);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  uva