uva 156 - Ananagrams

11 篇文章 0 订阅
6 篇文章 0 订阅

题意:把每个单词全部转化成小写字母,对每个单词,看它的字母重排后得到的单词在所有输入的单词中是否出现过,若没有出现,就输出原单词。所有要输出的单词按字典序排列输出。

水题一道   结构体排序自己写一个   计算的时候注意标记序号和是否出现    否者排序了后没法输出

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cctype>
using namespace std;

const int  maxn =1005;
int T = 0;

struct Words
{
    char s[25];
    int visit,len,n;
}st[1005],words[1005];

int cmp(Words a, Words b)
{
    int i;
    for(i = 0; a.s[i]!='\0' && b.s[i]!='\0'; i++)
        if(a.s[i] < b.s[i])return 1;
        else if(a.s[i] > b.s[i])return 0;
    if(a.s[i]=='\0')return 1;
    else return 0;
}

void Solve()
{
    int i,j,k;
    for(i = 0; i < T-1; i++)
    {
        char a[25], b[25];
        strcpy(a,st[i].s);
        sort(a,a+strlen(a));
        for(j = i+1; j < T; j++)
        {
            strcpy(b,st[j].s);
            sort(b,b+strlen(b));
            if(strcmp(a,b) == 0)
                st[i].visit = st[j].visit= 0;
        }
    }
}

int main()
{
    #ifdef LOCAL
    freopen("in.txt","r",stdin);
    #endif // LOCAL
    char str[100];
    while(gets(str)!=NULL)
    {
        if(str[0] == '#')
        {
            Solve();
            sort(words,words+T,cmp);
            for(int i = 0; i < T; i++)
                if(st[words[i].n].visit)puts(words[i].s);
            T = 0;
        }
        else
        {
            int str_len = strlen(str);
            for(int i = 0; i < str_len; i++)//把单词全部存到st中
            {
                if(!isalpha(str[i]))continue;
                int t = 0;
                while(isalpha(str[i]))
                {
                    words[T].s[t] = str[i];
                    st[T].s[t++] = tolower(str[i++]);
                }
                words[T].s[t] = st[T].s[t] = '\0';
                st[T].len = strlen(st[T].s);
                st[T].visit = 1;
                words[T].n = st[T].n = T;
                T++;
            }
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值