HDU-2203 affinity string KMP

Source: Internet
Author: User

  

Affinity string

Time Limit: 3000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 2427 accepted submission (s): 1080

Problem description: The more people get older, the smarter they get, and the more stupid they get. This is a question that deserves the attention of scientists around the world. Eddy has been thinking about the same problem, when he was very young, he knew how to judge the affinity string. But he found that when he was growing up, he did not know how to judge the affinity string, so he had to ask you again to solve the problem with a smart and helpful person.
The affinity string is defined as follows: Given two strings S1 and S2, if S2 can be contained in S1 through S1 cyclic shift, then S2 is the affinity string of S1.


The input question 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 inputaabcd
Cdaa
ASD
ASDF


Sample outputyes
No. This question requires whether or not another string is contained after a string loop. In fact, you only need to repeat the parent string and then perform KMP matching, because during the process of repeating the parent string, in fact, all the possibilities after the loop have been listed. For example, after the "ABCD" string is repeated, it is "abcdabcd" in this string "BCDA ", "cdab" and "dabc" appear one after another. In the process of solving the problem using this method, it should also be noted that when the length of the substring exceeds the length of the parent string, the matching is not performed, because it may output incorrect judgments, for example, if the neutron string in the previous example is "abcda", yes is also output. The Code is as follows:
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std;

char s1[200010], s2[100005], temp[100005];

int next[100005];

void getnext( )
{
int k = 1, j = 0, len = strlen( s2 + 1 );
while( k < len )
{
if( j == 0 || s2[k] == s2[j] )
{
++j, ++k;
if( s2[j] == s2[k] )
{
next[k] = next[j];
}
else
next[k] = j;
}
else
j = next[j];
}
}

bool kmp( )
{
int k = 0, j = 0, len1= strlen( s1 + 1 ), len2 = strlen( s2 + 1 );
while( k <= len1 && j <= len2 )
{
if( j == 0 || s1[k] == s2[j] )
{
++j, ++k;
}
else
{
j = next[j];
}
}
if( j > len2 )
return true;
else
return false;
}

int main()
{
while( scanf( "%s %s", s1 + 1, s2 + 1 ) != EOF )
{
int len1 = strlen( s1 + 1 ), len2 = strlen( s2 + 1 );
if( len2 > len1 )
{
puts( "no" );
continue;
}
strcpy( temp, s1 + 1 );
strcat( s1 + 1, temp );
getnext();
printf( kmp() ? "yes\n" : "no\n" );
}
}

  

  

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.