Uva:156 - Ananagrams

题意:输入纯英文单词文本(单词间可隔一个或多个空格且单词不会在行间断开输入),输入以#结束。在文本中寻找这样的单词(relative ananagram):在文本中没有该单词的各字母经过重新排列后得到的单词(不论字母大小写)。最后将这样的单词按字典序一个一行地输出。

边输入边标记这样的单词效率会高些。

#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<algorithm>
using namespace std;

const int maxn=1000000;
char word[maxn][30];
int p[maxn],t[30];

struct Ran{
    char r[30];
};
Ran ran[maxn];

bool cmp(Ran a,Ran b){
    return strcmp(a.r,b.r)<0;
}

int main()
{
    int n=0,i=0;
    char c;
    memset(p,0,sizeof(p));

    while(c=getchar()){
        if(c=='#') break;
        if(isalpha(c)){
            word[n][i++]=c;
        }
        else if(!isalpha(c)&&i){
            word[n][i]='\0';
            for(int j=n-1;j>=0;j--){
                int len=strlen(word[j]);
                if(len==i&&len!=1){
                    memset(t,0,sizeof(t));
                    int count=0;
                    for(int x=0;x<i;x++){
                        for(int y=0;y<len;y++){
                            if(toupper(word[n][x])==toupper(word[j][y])&&!t[y]){
                                t[y]=1;
                                count++;
                                break;
                            }
                        }
                    }
                    if(count==i){
                        p[j]=p[n]=1;
                    }
                }
            }
            n++; i=0;
        }
    }

    i=0;
    for(int j=0;j<n;j++){
        if(!p[j]) strcpy(ran[i++].r,word[j]);
    }
    sort(ran,ran+i,cmp);
    for(int j=0;j<i;j++)
        printf("%s\n",ran[j].r);
    return 0;
}

--------------------------------------------------------------------------------------------

          Keep It Simple,Stupid!

--------------------------------------------------------------------------------------------

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值