【思维题】Bazinga

【题目】http://acm.hdu.edu.cn/showproblem.php?pid=5510

【题意】找到从最后数第一个串,使得前面的串存在串不是它的子串

【思路】i从第一个开始扫,如果j是i的子串,那么标记这个串,以后不扫了:如果这个串不是以后要扫的串的子串,那么只要扫之前的i就能行(j不是他的子串那么j的父亲更不会是他的子串),如果这个串是以后要扫的串的子串,那么这个串也没有用。

【代码】

#include<cstring>
#include<cstdio>
#include<algorithm>
#include<string>
using namespace std;
char a[505][2005];
int nxt[505][2005];
int vis[505];
int len[505];


int main()
{
    int t,tt=0,n;
    scanf("%d",&t);
    while(tt++<t)
    {
        memset(vis,0,sizeof(vis));
        scanf("%d",&n);
        for(int i=1; i<=n; i++)
        {
            scanf("%s",a[i]);
            len[i]=strlen(a[i]);
            getNext(a[i],len[i],nxt[i]);
        }
        int ans=-1;
        for(int i=1; i<=n; i++)
        {
            for(int j=i-1; j>=1; j--)
            {
                if(vis[j])
                    continue;
                if(strstr(a[i],a[j])==NULL)
                {
                    ans=i;
                }
                else
                    vis[j]=1;
            }
        }
        printf("Case #%d: %d\n",tt,ans);
    }

}

猜你喜欢

转载自blog.csdn.net/qq_32259423/article/details/81430506