首页 > 其他 > 详细

UVa 156 反语片

时间:2014-02-07 11:37:49      阅读:318      评论:0      收藏:0      [点我收藏+]

/*

* 解题思路:

* 经典的字母重排问题,忽略大小写的比较,如果输入没有该单词对应的其他重排单词,则原单词输出

*/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#define A 1000
char s1[ A ][ A ],s2[ A ][ A ];
char s3[ A ][ A ];
int p,q;
int cmp_char( const void *_a , const void *_b )
{
    char *a = (char * )_a;
    char *b = (char* )_b;
    return *a-*b;
}
int cmp_string( const void *_a , const void *_b )
{
    char * a = (char *)_a;
    char *b = (char * )_b;
    return strcmp( a , b );
}
int search( int x )
{
    int i,j;
    for( i=0;i<p;i++ )
    {
        if( i == x ) continue;
        for( j=0;j<strlen( s2[x] );j++ )
            if( s2[ x ][ j ] != s2[ i ][ j ] )
                break;
        if( j == strlen( s2[ x ]) && j == strlen( s2[ i ]) ) return 1;
    }
    return 0;
}
int main( )
{
    int i,j;
    int x,q;
    char c;

    p = 0;
    while( scanf("%s",&s1[ p ] ) && s1[ p ][ 0 ] != ‘#‘ ) p++;

    for( i=0;i<p;i++ )
    {
        for( j=0;j<strlen( s1[ i ] ) ;j++ )
            s2[ i ][ j ] = tolower( s1[ i ][ j ] );
        s2[ i ][ j ] = ‘\0‘;
    }

    for( i=0;i<p;i++ )
        qsort( s2[ i ] , strlen( s2[ i ] ) , sizeof(char ) , cmp_char );

    q = 0;
    for( i=0;i<p;i++ )
    {
        x = search( i );
        if( x == 0 )
            strcpy( s3[ q++ ] , s1[ i ] );
    }

    qsort( s3 , q , sizeof( s3[ 0 ]) , cmp_string );

    for( i=0;i<q;i++)
        printf("%s\n",s3[ i ] );
    return 0;
}


UVa 156 反语片

原文:http://blog.csdn.net/u011886588/article/details/18954529

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!