UVA - 156 Ananagrams

题目大意:输入一些单词,按字典序顺序输出无视大小写及字母顺序后,只出现一次的单词。如 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; 
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值