Cf 149e Martian strings (KMP)

Source: Internet
Author: User

Reprinted please indicate the source, thank youHttp://blog.csdn.net/ACM_cxlove? Viewmode = Contents
By --- cxlove

Question: I gave a mother string, followed by Multiple matching strings, and asked if I could find two child strings that do not overlap in the mother string and splice them into matching strings.

Http://codeforces.com/problemset/problem/149/E

My approach is

Forward to KMP once, record each length of the matching string on the leftmost side of the pattern string that can be matched, that is, the position of the first match

Then, reverse the parent string and matching string, and KMP again.

Then the enumerated length is used to determine whether there is overlap.

I still think it's a good question, mainly because KMP won't do it now, sad...

# Include <iostream> # include <cstdio> # include <map> # include <cstring> # include <cmath> # include <vector> # include <algorithm> # include <set> # include <string> # include <queue> # define INF 1600005 # define M 40 # define n 100005 # define maxn 300005 # define EPS 1e-12 # define zero () FABS (a) <EPS # define min (A, B) (a) <(B )? (A) :( B) # define max (A, B) (a)> (B )? (A) :( B) # define Pb (a) push_back (a) # define MP (a, B) make_pair (a, B) # define MEM (A, B) memset (a, B, sizeof ()) # define ll long # define mod 1000000007 # define lson step <1 # define rson step <1 | 1 # define sqr (A) (a) * ()) # define key_value ch [CH [root] [1] [0] # define test puts ("OK"); # define PI ACOs (-1.0) # define lowbit (X) (-(x) & (x) # define hash 1331 # pragma comment (linker, "/Stack: 1024000000,1024000000 ") Using namespace STD; int next [1005]; char STR [100005], str_2 [100005], Pat [1005]; int m; int L [1005], R [1005]; void get_next (char * s, int Len) {next [0] =-1; int I = 0, j =-1; while (I <Len) {If (j =-1 | s [I] = s [J]) {I ++; j ++; if (s [I] = s [J]) next [I] = next [J]; else next [I] = J ;} else J = next [J] ;}} void match (char * Pat, int LP, char * STR, int ls, int * DP) {int I = 0, j = 0; while (I <LP & J <ls) {if (I =-1 | Pat [I] = STR [J]) {I ++; j ++; DP [I] = min (DP [I], J);} else I = next [I]; if (I = LP) I = next [I];} void Init (INT Len) {L [0] = R [0] = 0; For (INT I = 1; I <= Len; I ++) {L [I] = inf; R [I] = inf ;}} int main () {// freopen ("input.txt", "r", stdin ); while (scanf ("% s", STR )! = EOF) {int CNT = 0, length = strlen (STR); For (INT I = 0; I <length; I ++) str_2 [I] = STR [length-1-i]; scanf ("% d", & M); While (M --) {scanf ("% s", Pat ); int Len = strlen (PAT); If (LEN = 1) continue; Init (LEN); get_next (Pat, Len); match (Pat, Len, STR, length, l); For (INT I = 0; I <Len/2; I ++) {swap (Pat [I], Pat [len-i-1]);} get_next (Pat, len); match (Pat, Len, str_2, length, R); For (INT I = 1; I <= Len; I ++) {// cout <I <"" <L [I] <"" <R [Len-I] <Endl; if (L [I] + R [Len-I] <= length) {CNT ++; break ;}} printf ("% d \ n", CNT );} 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.