Hdu5510 Bazinga

Description

Ladies and gentlemen, please sit up straight.
Don't tilt your head. I'm serious.
For \(n\) given strings \(S_{1},S_{2},\cdots,S_{n}\), labelled from \(1\) to \(n\), you should find the largest \(i (1 \le i \le n)\) such that there exists an integer \(j (1 \le j < i)\) and \(S_{j}\) is not a substring of \(S_{i}\).
A substring of a string \(S_{i}\) is another string that occurs in \(S_{i}\). For example, "ruiz" is a substring of "ruizhang", and "rzhang" is not a substring of "ruizhang".

Input

The first line contains an integer \(t\) \((1 \le t \le 50)\) which is the number of test cases.
For each test case, the first line is the positive integer \(n\) \((1\le n \le 500)\) and in the following n lines list are the strings \(S_{1},S_{2},\cdots,S_{n}\).
All strings are given in lower-case letters and strings are no longer than \(2000\) letters.

Output

For each test case, output the largest label you get. If it does not exist, output \(−1\).

Sample Input

4
5
ab
abc
zabc
abcd
zabcd
4
you
lovinyou
aboutlovinyou
allaboutlovinyou
5
de
def
abcd
abcde
abcdef
3
a
ba
ccc

Sample Output

Case #1: 4
Case #2: -1
Case #3: 4
Case #4: 3

开始做的姿势不对,想的是裸暴力或者什么ac自动机啊。。。。(I 'm so puny!!!)
其实这题正解就是暴力的剪枝,想如果\(S_{j}\)\(S_{i}\)的子串(\(i > j\)),那么对于\(k > i\),若\(S_{i}\)\(S_{k}\)的子串,那么\(S_{j}\)一定也是\(S_{k}\)的子串;如果不是\(k\)就可以更新答案,那么也就说明只需要匹配\(i\)而不要匹配\(j\),由此可以打个\(vis\)标记剪枝了。

#include<cstring>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
using namespace std;

typedef long long ll;
#define maxn (510)
#define maxl (2010)
#define rhl (5000011)
#define xi (127)
int T,N,len[maxn],mi[maxl],pre[maxn][maxl],ans;
char s[maxl]; bool exist[maxn];

inline bool find(int a,int b)
{
    for (int i = len[b];i <= len[a];++i)
    {
        int key = pre[a][i]-(ll)pre[a][i-len[b]]*(ll)mi[len[b]]%rhl;
        if (key < 0) key += rhl; if (key == pre[b][len[b]]) return true;
    }
    return false;
}

int main()
{
    freopen("5510.in","r",stdin);
    freopen("5510.out","w",stdout);
    scanf("%d",&T); mi[0] = 1;
    for (int i = 1;i <= 2000;++i) mi[i] = (mi[i-1]*xi)%rhl;
    for (int Cas = 1;Cas <= T;++Cas)
    {
        printf("Case #%d: ",Cas);
        memset(exist,false,sizeof(exist));
        scanf("%d",&N); ans = 0;
        for (int i = 1;i <= N;++i)
        {
            scanf("%s",s+1); len[i] = strlen(s+1);
            for (int j = 1;j <= len[i];++j) pre[i][j] = (pre[i][j-1]*xi+s[j]-'a'+1)%rhl;
            for (int j = i-1;j;--j)
            {
                if (exist[j]) continue;
                if (find(i,j)) exist[j] = true; else ans = i;
            }
        }
        if (ans) printf("%d\n",ans); else puts("-1");
    }
    fclose(stdin); fclose(stdout);
    return 0;
}

转载于:https://www.cnblogs.com/mmlz/p/5961244.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值