(KMP 1.6) hdu 2203 affinity string (determine whether the mode string can be included in the pattern string after the cyclic shift of the text string ),

Source: Internet
Author: User

(KMP 1.6) hdu 2203 affinity string (determine whether the mode string can be included in the pattern string after the cyclic shift of the text string ),

Question:

Affinity stringTime Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission (s): 9139 Accepted Submission (s): 4174


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
 
AuthorEddy
Recommendlcy | We have carefully selected several similar problems for you: 3336 2201 3068 2200


Question Analysis:

KMP. Simple question.


Make the "s1 cyclic shift" followed by another s1.



The Code is as follows:

/** Hdu2206.cpp ** Created on: July 15, April 18, 2015 * Author: Administrator */# include <iostream> # include <algorithm> # include <cstdio> # include <cstring> using namespace std; const int maxn = 100001; int n; // the length of the text string int m; // The length of the target string char text [maxn]; // the length of the text string char pattern [maxn]; // mode string int nnext [maxn]; // next array. directly starting next may obtain the next array */void get_next () {nnext [0] = nnext [1] = 0 with the specified name/* O (m) in the system; for (int I = 1; I <m; I ++) {int j = nnex T [I]; while (j & pattern [I]! = Pattern [j]) j = nnext [j]; nnext [I + 1] = pattern [I] = pattern [j]? J + 1: 0 ;}}/* o (n) Time for matching ** returns the first matched position */bool kmp () {bool flag = false; // indicates whether the mode string int j = 0 can be found in the text string;/* The first position of the mode string initialized */for (int I = 0; I <n; I ++) {/* traverse the entire text string */while (j & pattern [j]! = Text [I])/* follows the mismatched edge until it can be matched. The worst case is j = 0 */j = nnext [j]. if (pattern [j] = text [I])/* if the match is successful, continue to the next position */j ++; if (j = m) {/* If direct output is found */flag = true;} return flag;} int main () {char tmp [maxn]; while (scanf ("% s", text, pattern )! = EOF) {memcpy (tmp, text, sizeof (text); strcat (text, tmp); n = strlen (text); m = strlen (pattern ); get_next (); if (kmp () = true) {printf ("yes") ;}else {printf ("no ");} printf ("\ 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.