uva 644 Immediate Decodability

刚开始看这道题的时候,我本来觉得它应该挺难处理的,所以就把它暂时放下了,不过后来在我小憩的时候突然就想到了这道题目实际上比前面做过的简单多了,就是做一些小的比较久OK了,看来做字符串处理的题目时必须要特别的细心,如果第一次提交错了,改起来就难了,总是找不到错误在哪,不过这道题型号我一次就AC了,呵呵,这道题就是让你查找一下短一点的字符串是不是长一点字符串的前缀,比前面的题目简单了很多哦下面是代码,写的比较怪一些,不过确实十分简洁

View Code
#include<stdio.h>
#include<string.h>
char st[200][20];
int judge(int top)
{
    int i, j, k;
    for(i = 0;i < top; i++)
    {
        for(j = 0;j < top; j++)
        {
            if((strlen(st[i]) < strlen(st[j])) && (i != j))
            {
                for(k = 0;k < strlen(st[i]); k++)
                if(st[i][k] != st[j][k])
                break;
                if(k == strlen(st[i]))
                return 0;
            }
        }
    }
    return 1;
}
int main()
{
    int top = 0, j = 1;
    while(gets(st[top]) != NULL)
    {
        if(st[top][0] == '9')
        {
            if(judge(top))
            printf("Set %d is immediately decodable\n",j++);
            else
            printf("Set %d is not immediately decodable\n",j++);
            top = 0;
        }
        else
        top++;
    }
    return 0;
}
原文地址:https://www.cnblogs.com/SDUTYST/p/2528865.html