TTTTTTTTTTTTTTTT hdu 5510 Bazinga 字符串+哈希

Bazinga

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2078    Accepted Submission(s): 642


Problem Description
Ladies and gentlemen, please sit up straight.
Don't tilt your head. I'm serious.

For n given strings S1,S2,,Sn, labelled from 1 to n, you should find the largest i (1in) such that there exists an integer j (1j<i) and Sj is not a substring of Si.

A substring of a string Si is another string that occurs in Si. For example, ``ruiz" is a substring of ``ruizhang", and ``rzhang" is not a substring of ``ruizhang".
 

 

Input
The first line contains an integer t (1t50) which is the number of test cases.
For each test case, the first line is the positive integer n (1n500) and in the following n lines list are the strings S1,S2,,Sn.
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
 

 

Source
题意:,依次给出n个串, 找出下标最大且不能包含前面所有的串的串(即前面所有的串都是这个串的子串)。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;
typedef  long long  ll;
typedef unsigned long long ull;
#define MM(a,b) memset(a,b,sizeof(a));
#define inf 0x7f7f7f7f
#define FOR(i,n) for(int i=1;i<=n;i++)
#define CT continue;
#define PF printf
#define SC scanf
const int mod=1000000007;
const int N=505+10;
ull seed=13331;
ull num[N];
int a[N];
char s[505][2005];

bool inter(int id,int k)
{
    int len=strlen(s[id]);
    ull tmp=0,big=0,base=1;

    for(int i=0;i<len;i++)
        {
            tmp=tmp*seed+s[id][i];
            big=big*seed+s[k][i];
        }
    for(int i=0;i<len-1;i++) base*=seed;
    if(tmp==big) return true;
    for(int i=len;s[k][i]!='\0';i++)
    {
        big=(big-s[k][i-len]*base)*seed+s[k][i];
        if(big==tmp) return true;
    }
    return false;
}

int main()
{
    int cas,n,kk=0;scanf("%d",&cas);
    while(cas--){
        scanf("%d",&n);
        int pos=0,ans=-1;
        for(int i=1;i<=n;i++)
        {
            scanf("%s",s[i]);
            while(1)
            {
                if(!pos) {a[++pos]=i;break;}
                if(inter(a[pos],i)) pos--;
                else
                 {
                     a[++pos]=i;
                     ans=i;
                     break;
                 }
            }
        }
        printf("Case #%d: %d\n",++kk,ans);
    }
    return 0;
}

 分析:

1.复杂度没有想到合理的办法降下来,正确的做法是,设置一个栈(或数组),当栈顶字符串完全包含于当前的字符串时,

那么就弹出栈顶字符串,然后再进行比较,因此栈内保存的全是能符合题目要求的字符串:理论依据:如果a是b的子串,

那么判断时,如果b完全包含于c,那么a也必定完全包含于c,如果b不完全包含于c,那么c肯定是符合要求的字符串,弹入

 总的复杂度:2*500*2000=2*10^6;
2.在计算base时犯了个错误,因为base是seed的(l-1)次方,所以我先循环了l次,再除以一次seed,,结果这样是错的
因为ull是64位的,超过64位自动溢出,相当于取模,所以base超了ull时,这样先循环再除肯定是错的
 
posted @ 2016-07-31 22:54  快点说我帅  阅读(230)  评论(0编辑  收藏  举报