现在的位置: 首页 > 综合 > 正文

[poj 3450]Corporate Identity[暴力枚举子串]

2018年03月17日 ⁄ 综合 ⁄ 共 611字 ⁄ 字号 评论关闭

题意:

和poj 3080相同.

同样是暴力.

又是循环边界的小毛病...

#include <cstring>
#include <cstdio>
//372K	110MS
const int R = 4005;
const int C = 205;
char s[R][C];
char t[C],ans[C];
int n;
int main()
{
    while(scanf("%d",&n)==1 && n)
    {
        for(int i=0;i<n;i++)
            scanf("%s",s[i]);
        int c = strlen(s[0]);
        bool next_id,get_len,get = false;
        for(int len=1;len<=c;len++)
        {
            get_len = false;
            for(int id=0;id<=c-len;id++)
            {
                strncpy(t,&s[0][id],len);
                t[len] = '\0';
                next_id = false;
                for(int i=1;i<n;i++)
                {
                    if(!strstr(s[i],t))
                    {
                        next_id = true;
                        break;
                    }
                }
                if(!next_id)
                {
                    if(!get)    get = true;
                    if(!get_len)
                    {
                        get_len = true;
                        strcpy(ans,t);
                    }
                    else
                        if(strcmp(t,ans)<0) strcpy(ans,t);
                }
            }
            if(!get_len)    break;
        }
        printf("%s\n",get?ans:"IDENTITY LOST");
    }
}

抱歉!评论已关闭.