HDU 2203 affinity string (simple KMP, but pre-judgment required)

Source: Internet
Author: User
Tags first string
Affinity string

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


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 Input

AABCDCDAAASDASDF
 


Sample output

yesno
If the first string is shifted cyclically, move the previous one to the back. If the second string can be contained, yes is output; otherwise, no is output.
Solution: Replace the first string with the standard KMP.
Subject address: affinity string
AC code:
# Include <iostream> # include <string> # include <cstring> # include <cstdio> using namespace STD; char S1 [100005], S2 [100005], s [200005]; int next [100005], len1, len2, Len; void getnext () // S2 is the mode string {int I, j; next [0] = 0, next [1] = 0; for (I = 1; I <len2; I ++) {J = next [I]; while (J & S2 [I]! = S2 [J]) J = next [J]; If (s2 [I] = S2 [J]) next [I + 1] = J + 1; else next [I + 1] = 0 ;}} int KMP () {int I, j = 0; for (I = 0; I <Len; I ++) {While (J & S [I]! = S2 [J]) J = next [J]; If (s [I] = S2 [J]) J ++; If (j = len2) return 1;} return 0;} int main () {While (~ Scanf ("% S % s", S1, S2) {len1 = strlen (S1); len2 = strlen (S2); Len = 2 * len1; strcpy (S, s1); strcat (S, S1); // copy S1 to exist twice in S3 getnext (); If (KMP () puts ("yes "); else puts ("no ");}}

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.