因为痛,所以叫青春

我有一个梦想:穷屌丝变身富屌丝
uva 156 Ananagrams

一次就AC了,而且做道题目用时极少,状态来了,挡都挡不住,哈哈

View Code
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
int cmp(const void *a,const void *b)
{
    return *(char *)a - *(char *)b;
}
int ccmp(const void *a,const void *b)
{
    return (strcmp((char *)a,(char *)b));
}
int main()
{
    char st[100][100], ch, _st[100][100], s[100];
    int top = 0, k = 0, flag = 1, i;
    while((ch = getchar()))
    {
        if((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z'))
        {
            flag = 1;
            st[top][k] = ch;
            if(isalpha(ch))
                ch = toupper(ch);
            s[k++] = ch;
        }
        else
        {
            if(flag)
            {
                st[top][k] = '\0';
                s[k] = '\0';
                qsort(s,k,sizeof(s[0]),cmp);
                strcpy(_st[top],s);
                top++;
                k = 0;
                flag = 0;
            }
        }
        if(ch == '#')
            break;
    }
    for(i = 0;i < top; i++)
    {
        flag = 1;
        for(k = 0;k < top; k++)
        {
            if(k == i)
                continue;
            if(strcmp(_st[k],_st[i]) == 0)
            {flag = 0;break;}
        }
        if(flag == 0)st[i][0] = '\0';
    }
    qsort(st,top,sizeof(st[0]),ccmp);
    for(i = 0;i < top; i++)
        if(st[i][0] != '\0')
        {
            printf("%s\n",st[i]);
        }
    return 0;
}

posted on 2012-07-16 20:06  Nice!  阅读(209)  评论(0编辑  收藏  举报