HDU 5510 Bazinga (Kmp+dfs pruning) 2015ACM/ICPC Asia Shenyang Station-Replay (thanks to Tohoku University)

Source: Internet
Author: User

Nonsense:

It took me a lot of work to do this problem. First of all, I will not KMP the algorithm, but also specifically learned the algorithm. Second, even if the kmpis used, there is no doubt that if the violence is enumerated, it will explode. So on The basis of Dfs added two pruning to solve this problem.

Test instructions

I did not read the question, but the team-mates explained to me test instructions, then I wrote according to test instructions's question.

It's probably meant to give n strings, from top to bottom, labeled 1--n, looking for a string with the largest number of tags, This string is required to satisfy: there is at least one substring in the string that is smaller than the tag.

Input:

The first line enters an integer t, which indicates a common t - group data.

The first row of each group of data is an integer n, which indicates that there are n strings.

Next n rows, one string per line.

Output:

The output format is "case #x: Y", where x is the number of groups andy represents the tag of the string.

If there is a string that satisfies the condition, the token of the string is output, otherwise output -1.

Exercises

    1. With Dfs, the source string starts with the largest string of tokens (string n-1), and the matching string starts from a string smaller than the source string, matching from large to small.
    2. If two strings match, the smaller match string continues to match. If two strings do not match, the matching string is recorded, and the source string and the answer are compared, and the answer takes a larger value and executes 3. If the string matches until it matches the least-marked string (string 0), all strings can match the source string, and 4 is executed .
    3. The source string is traced back and then compared with the traced source string and the recorded match string, i.e., 2.
    4. The current source string does not meet the criteria, backtracking to the end of Dfs .
    5. Output the answer.

There are two points to note--

  1. if the current source string aj matches the current string ai No match, then the parent string of the current source string ak Span style= "font-family: the song Body;" , which is the source string of the current source string, can also be directly associated with ai compare without needing to aj and ai al (i < &NBSP;L&NBSP;<&NBSP;J)   must be ak
  2. If the source string Ak is consistently recursive to the smallest string A0 , then any string Ai (0 < i <= k) and A0 match, i.e. A0 is any string Ai substring of the string. Then any Ai can not be the answer.

The code is as follows--

1#include <cstdio>2#include <cstring>3#include <cmath>4#include <algorithm>5 using namespacestd;6 7 Const intN = .;8 9 intNext[n];Ten Chars[510][n]; One intt, N; A intLenS, LenT; - intans; - intMid//pruning, placing a string to be accessed multiple times the  - voidInit () - { -scanf"%d", &n); +      for(inti =0; I < n; i++) scanf ("%s", S[i]); -Ans =-2; +MID =-1; A } at  - voidKmpnext (CharT//Calculate next Array - { -     inti =1; -next[0] = -1; -      while(I <LenT) in     { -         intj =0; to          while(T[j] = =T[i]) +         { -Next[i] =J; thei++; *J + +; $         }Panax NotoginsengNext[i] =J; -i++; the     } + } A  the BOOLKmpCharSCharT//KMP Algorithm + { -LenS =strlen (S); $LenT =strlen (T); $ Kmpnext (T); -     inti =0, j =0; -      while(I < LenS && J <LenT) the     { -         if(j = =-1)Wuyi         { thei++; -j =0; Wu         } -         Else if(S[i] = =T[j]) About         { $i++; -J + +; -         } -         Elsej =Next[j]; A     } +     if(j = = LenT)return 1; the     return 0; - } $  the BOOLDfsintX//Recursive search for sub-strings the { the      for(inti = X1; I >=0; i--) the     { -         if(Mid! =-1&& Mid! = i)Continue;//pruning, if the string Ai has never been queried, or if the string ax's substring contains no strings of AI, then query ax contains AI in         if(KMP (s[x], s[i])) the         { the             if(i = =0)return 1; AboutMID =-1;//If string ax contains a string of AI, then mid is 1 the             BOOLFlag =DFS (i); the             if(flag)return 1;//pruning, if the string A0 is a substring of the string ax, then AX does not meet the criteria the         } +         Else -         { theAns = ans > x? ans:x;//always take maximum valueBayiMID =i; the              Break; the         } -  -  the     } the     return 0; the } the  - voidWork () the { theDFS (N-1); the }94  the voidOutit (intTM) the { theprintf"Case #%d:%d\n", TM, ans+1);98 } About  - intMain ()101 {102scanf"%d", &t);103      for(intTM =1; TM <= T; tm++)104     { the init ();106 Work ();107 Outit (tm);108     }109}
View Code

KMP Algorithm--

Http://www.cnblogs.com/mypride/p/4950245.html

PS. Look at other people's code, some directly using the iterative method, the complexity of N^2/2, but with a feeling less reliable pruning. I figured it out, worst case time seems to explode. But the result of the operation is not much slower than mine, drunk. Maybe there's something wrong with the way I'm doing time? Or is the data special?

Alas...... The revolution has not been successful, comrades still need to work Ah!

HDU 5510 Bazinga (Kmp+dfs pruning) 2015ACM/ICPC Asia Shenyang Station-Replay (thanks to Tohoku University)

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.