NYOJ327 affinity string [KMP]

Source: Internet
Author: User

 

 

Affinity string time limit: 1000 MS | memory limit: 65535 KB difficulty: 3
Description
Recently, zyc encountered a very difficult problem: Judging the affinity string can be seen directly when judging the affinity string before, but now it is different. Now the two strings are very large, the zyc header is dizzy. So zyc hopes that you can help him find a way to quickly judge the affinity string. Affinity string definition: Given two strings s1 and s2, if s2 can be contained in s1 by repeating the movement through s1, then s2 is the affinity string of s1.
Input
This topic contains multiple groups of test data. The first line of each group contains the input string s1, the second line contains the input string s2, And the s1 and s2 length are less than 100000.
Output
If s2 is an s1 affinity string, "yes" is output; otherwise, "no" is output ". The output of each group of tests occupies one row.
Sample Input
AABCDCDAAASDASDF
Sample output
yesno

 

 

 

#include 
     
      #include 
      
       #define maxn 100000 + 5char str1[2 * maxn], str2[maxn];int next[maxn], len1, len2;;void getNext(){int j = -1, i = 0;next[0] = -1;while(i < len2){if(j == -1 || str2[i] == str2[j]){++i; ++j;if(str2[i] != str2[j]) next[i] = j;else next[i] = next[j];}else j = next[j];}}bool KMP(){getNext();int i = 0, j = 0;while(i < len1 && j < len2){if(j == -1 || str1[i] == str2[j]) ++j, ++i;else j = next[j];}return j == len2;}int main(){while(scanf("%s%s", str1, str2) == 2){len1 = strlen(str1);len2 = strlen(str2);if(len1 < len2){printf("no\n");continue;}memcpy(str1 + len1, str1, len1);len1 *= 2;if(KMP()) printf("yes\n");else printf("no\n");}return 0;}        
      
     


 

 

 

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.