HDU 5510 Bazinga string + hash

Source: Internet
Author: User

Bazinga

Time limit:2000/1000 MS (java/others) Memory limit:65536/65536 K (java/others)
Total submission (s): 2078 Accepted Submission (s): 642


problem DescriptionLadies and gentlemen, please sit up straight.
Don ' t tilt your head. I ' m serious.

ForNGiven stringss1,s2,?,sn , labelled from1ToN, you should find the largesti (1≤i≤n) such that there exists an integerJ (1≤J<i) andSJ is not a substring ofSi .

A substring of a string si is another string, occurs in si. For example, the ' Ruiz ' is a substring of ' ruizhang ', and ' Rzhang ' are not a substring of ' Ruizhang '.  

InputThe first line contains an integerT (1≤t≤) Which is the number of the test cases.
For each test case, the first line is the positive integern (1≤n≤) and in the followingNLines list is the stringss1,s2,?,sn .
All strings is given in lower-case letters and strings is no longer than Letters. 

Outputfor each test case, the output of the largest label you get. If It does not exist, output −1.  

Sample Input45ABABCZABCABCDZABCD4YOULOVINYOUABOUTLOVINYOUALLABOUTLOVINYOU5DEDEFABCDABCDEABCDEF3ABACCC 

Sample OutputCase #1:4Case #2: -1case #3:4Case #4:3 

Source2015ACM/ICPC Asia Shenyang Station-Replay (thanks to Tohoku University)Test instructions:, in turn, give n strings, find the subscript maximum and cannot contain the string of the preceding string (that is, all the strings are the substring of the string).
#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#def ine for (i,n) for (int i=1;i<=n;i++) #define CT continue; #define PF printf#define SC scanfconst int Mod=1000000007;const i    NT 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]!= ' n '; 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;}

Analysis:

1. Complexity did not think of a reasonable way down, the right way is to set a stack (or array), when the stack top string is completely contained in the current string,

Then the stack-top string is popped, and then compared, so the stack is full of strings that meet the requirements of the topic: Theoretical basis: If A is a substring of B,

So when judging, if B is completely contained in C, then a must also be completely contained in C, if B is not completely contained in C, then C must be a string that conforms to the requirement, bouncing in .

total complexity: 2*500*2000=2*10^6;2. I made a mistake in computing base because base is the l-1 of seed, so I'm going to loop the L-times and divide the seed once, and it turns out to be wrong . because the ull is 64-bit, more than 64 automatic overflow, equivalent to modulo, so base super-ull, so that the first cycle and then apart must be wrong

HDU 5510 Bazinga string + hash

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.