HDU 5510 Bazinga

Source: Internet
Author: User


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

problem DescriptionLadies 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 (1≤i≤n) such that there exists an inte Ger J (1≤j<i) and Sj are not a substring of Si.

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

The first line contains a integer t (1≤t≤50) which is the number of test cases.
For each test case, the first line is the positive integer n (1≤n≤500) and in the following N lines list is the strings S 1,s2,?, Sn.
All strings is given in lower-case letters and strings is no longer than in letters.

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

Sample Input4 5 AB ABC zabc ABCD ZABCD 4 you lovinyou aboutlovinyou allaboutlovinyou 5 de def ABCD ABCDE abcdef 3 a BA CCC

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

Source2015ACM/ICPC Asia Shenyang Station-Replay (thanks to Tohoku University)

Problem solving: KMP violence, if a is a substring of B, B is a substring of C, then a must be a substring of c. If B is not a substring of C, a may be a substring of c.

What does that mean? This means that if a is a substring of B, then a is useless, and this is the process of removing redundant data, followed by violence. Because the subject is required to be its substring before it must be, the redundant string can be removed as long as the adjacent string is compared.

1#include <bits/stdc++.h>2 using namespacestd;3 Const intMAXN = .;4 intFAIL[MAXN];5 voidGetfail (Charstr[]) {6      for(inti =0, j = fail[0] = -1; Str[i]; ++i) {7          while(J! =-1&& str[i]! = str[j]) j =Fail[j];8Fail[i +1] = ++J;9     }Ten } One BOOLMatchCharSa[],Charsb[]) { A Getfail (SA); -      for(inti =0, j =0; Sb[i]; ++i) { -          while(J! =-1&& sb[i]! = sa[j]) j =Fail[j]; the         if(!sa[++j])return true; -     } -     return false; - } + Chars[501][MAXN]; - BOOLinvalid[501]; + intMain () { A     intKase,n,cs =1; atscanf"%d",&Kase); -      while(kase--) { -scanf"%d",&n); -memset (Invalid,false,sizeofinvalid); -          for(inti =0; I < n; ++i) { -scanf"%s", S[i]); in             if(I && match (s[i-1],s[i])) -invalid[i-1] =true; to         } +         intRET =-1; -          for(inti = n1; I >=0&& ret = =-1; --i) { the              for(intj =0; J < i && ret = =-1; ++j) { *                 if(Invalid[j])Continue; $                 if(!match (S[j],s[i])) ret = i +1;Panax Notoginseng             } -         } theprintf"Case #%d:%d\n", cs++, ret); +     } A     return 0; the}
View Code

HDU 5510 Bazinga

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.