POJ 3080 Blue Jeans 暴力枚举+KMP

题目地址: http://poj.org/problem?id=3080


这道题从昨天晚上开始做的,一直做到现在才过了,太坑了

看了别人才知道可以暴力枚举的,然后各种小错误,

哎……C语言的字符串函数学的不好啊……………………

还有一个他说 comes first in alphabetical order,我一看就是第一个出现的,结果wa好几次,然后看discuss,别人说字典序最小的,
然后果断在翻译一下,果然是的。


思路:

枚举第一个字符串,从长度为1的一直到60开始一个一个枚举,

直到长度为n所有字符串 在下面的字符串中至少有一个没有出现,结束。

中间还要记录字典序最小的。


AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <list>
#include <deque>
#include <queue>
#include <iterator>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
#include <cctype>
using namespace std;

char s[12][62];
int n,wlen,next[62];
char word[62];
int be;

void getnext(char *p)
{
    int j=0,k=-1;
    next[0]=-1;
    while(j<wlen)
    {
        if(k==-1||p[j]==p[k])
        {
            j++;    k++;
            next[j]=k;
        }
        else
            k=next[k];
    }
}

bool kmp(char *text,char *word)
{
    int i=0,j=0,k=0;
    while(i<60)
    {
        if(j==-1||text[i]==word[j])
        {
            i++;    j++;
        }
        else
            j=next[j];
        if(j==wlen)
            return true;
    }
    return false;
}

int main()
{
    int i,j,k,T;
    cin>>T;
    while(T--)
    {
        scanf("%d",&n);

        for(i=0;i<n;i++)
            scanf("%s",s[i]);
        int be=0,len=0;
        int h=0;
        char pp[62]="";
        for(i=1;i<=60;i++)
        {
            wlen=i;
            int g=0,kkk=0;
            for(j=0;j+i<=60;j++)
            {
                strncpy(word,s[0]+j,i);
                word[wlen]='\0';
                getnext(word);
                int f=0;
                for(k=1;k<n;k++)
                    if(!kmp(s[k],word))//匹配不成功
                    {
                        f=1;break;
                    }
                if(f==0)//当从j开始的i个匹配成功
                {
                    be=j;   len=i;
                    if(kkk==0||strcmp(pp,word)>0)
                    {
                        kkk=1;strcpy(pp,word);
                    }
                    g=1;
                }
            }
            if(g==0)//当有i个字符的时候都没有匹配
                break;
        }
        if(len<3)
            printf("no significant commonalities\n");
        else
        {
            printf("%s\n",pp);
        }
    }
    return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值