HDU 5510 Bazinga

Source: Internet
Author: User

http://acm.hdu.edu.cn/showproblem.php?pid=5510

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

ForN Given stringsS1,S2,?,SN, labelled from1 toN, 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   s I is another string that occurs , in  Si. For example, the ' Ruiz ' is a substring of ' ruizhang ', and ' Rzhang ' are not a substring of ' Ruizhang '. Input:the first line contains an integerT(1≤T≤50) which is the number of the test cases.
For each test case, the first line is the positive integerN(1≤N≤500) and in the followingN lines list is the stringss 1, S2 ,sn.
all strings is given in lower-case letters and strings is no l Onger than  2000 letters. Output:for each test case, output the largest label you get. If It does not exist, output−1. Sample INPUT:45ABABCZABCABCDZABCD4YOULOVINYOUABOUTLOVINYOUALLABOUTLOVINYOU5DEDEFABCDABCDEABCDEF3ABACCC Sample Output:case #1:4Case #2: -1case #3:4Case #4:3 The main idea: to give you a string of n, numbering from 1 to N to find a string I see whether there is a string in the above string is not the string I substring, if there is no output-1, Otherwise output I (make the number I as large as possible) analysis: through the test instructions can be known, I string substring must be found on the I string above, then we can compare the two two as this example:

5
6
F (1)
D (2)
BA (3)
ZABACD (4)
ZABACD (5)
ZABACDC (6)

They are numbered 1, 2, 3, 4, 5, 6, so we start with the highest place I can find

(6) and (5) The comparison found (5) is a substring of (6), then the comparison (5) and (4), the Discovery (4) is (5) the substring, then the comparison (4) and (3) ...< since (5) is (6) the substring, then only is

(5) Substring, then it must be (6) the substring, so that two two of the comparison, if one is another substring does not need to re-tube, continue to find, until found not substring (or so string to find out) so far >

When comparing to (3) and (2) found (2) is not a substring of (3), then it is necessary to see (2) is not (4) to (6) the substring, found (2) is (4) to (6) The substring, then we have to

The result I is 3, but 3 is not the maximum value we want to get, we continue to start from (2), compare (2) and (1), find (1) also not (2) substring, then look (1) is not

(3) < why is it 3 instead of 1? Because we have been to a result of 3, then we find the next I as long as the >=3 can be, so the comparison just need to find from 6 to 3 can be,> to

(6) The substring, found (1) is not (6) of the substring, the result of the I is 6, is not greater than the previous result 3, so the final result is 6 (these processes can be done with a recursive function)

(Is it a little unclear ^_^!.... Debug This example again, OK)

#include <stdio.h>#include<math.h>#include<string.h>#include<stdlib.h>#include<algorithm>using namespacestd;Const intN = .;intn, num;Chars[510][n];voidSolveintk) {    if(k <=0|| num = = N-1)        return ; if(Strstr (S[k], s[k-1])) solve (k-1); Else    {         for(inti = n-1; I >= num; i--)        {            if(Strstr (S[i], s[k-1]) ==0) {num=i;  Break; }} solve (k-1);//This step is recursive to ensure that the value you are looking for is the largest    }}intMain () {intT, x =0; scanf ("%d", &t);  while(t--) {num= -1; X++; scanf ("%d", &N);  for(inti =0; I < n; i++) scanf ("%s", S[i]); Solve (n-1); if(num = =-1) printf ("Case #%d: -1\n", x); Elseprintf ("Case #%d:%d\n", X, num +1); }    return 0;}/*56FDBAZABACDZABACDZABACDC*/

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.