Codeforces 536B Tavas and Malekas for overlapping positions KMP

Source: Internet
Author: User

Topic Link: Click to open the link

Test instructions

Constructing n-long strings with lowercase letters S,M requirements

String P

below M position. A1, A2 AM (Input ordered)

To cause the string s to start with AI and followed by a P-string.

Number of methods to ask for construction

Ideas:

In fact, for AI, ai+1, two positions, if these two positions interact (i.e. Ai+1-ai < Len)

Copy a P-like string P '

Put p in the AI position, put P ' in the ai+1 position, then only need to determine whether the second half of P is matched to the first half of P '.

Which position of P ' is the same as the tail of p.

KMP find these positions in the set.

And then it's a simple judgment.

#include <stdio.h> #include <string.h> #include <string> #include <math.h> #include <map> #include <vector> #include <set> #include <algorithm> #include <iostream>using namespace std; Template <class t>inline BOOL Rd (T &ret) {char c; int sgn;if (c = GetChar (), c = = EOF) return 0;while (c! = '-') && (c< ' 0 ' | | c> ' 9 ')) C = GetChar (); sgn = (c = = '-')? -1:1;ret = (c = = '-')? 0: (C-' 0 '); while (c = GetChar (), C >= ' 0 ' &&c <= ' 9 ') ret = ret * + (C-' 0 '); ret *= Sgn;return 1;} Template <class t>inline void pt (T x) {if (x <0) {Putchar ('-'); x = x;} if (x>9) pt (X/10);p Utchar (x% 10 + ' 0 ');} typedef long Long Ll;typedef unsigned long long ull;typedef pair<int, int> pii;const int N = 1e6 + 10;const int mod = 1e9 + 7;int f2[n], Len;char s1[n], s2[n];void getfail (int *f, char *p) {f[0] = f[1] = 0;for (int i = 1; i < Len; i++) { Int J = f[i];while (J&&p[i]! = p[j]) j = f[j];f[i + 1] = P[i] = = P[j]? j + 1:0;}} Set<int>s;void KMP (int *f, char *s1, char *s2) {getfail (f, S2); int j = 0, i = 0;while (i <= len) {while (J &&am P S1[i]! = s2[j]) j = f[j];i++, j + +;} for (int i = len; f[i]; i = f[i]) S.insert (Len-f[i]);} int m, N;bool v[n];int main () {while (CIN >> N) {rd (M); scanf ('%s ', S1), while (m--) {int u; RD (U); v[u] = true;} Len = strlen (S1); memcpy (S2, s1, sizeof S1); KMP (F2, S1, S2); ll ans = 1;for (int i = 1, last = -1e7; I <= n; i++) {if (v[i] = False && i-last >= len) {A NS *= 26ll;if (ans >= mod) ans%= mod;} else if (v[i] = True) {if (I-last < len &&!s.count (i-last)) {ans = 0; break;} last = i;}} PT (ANS%MOD); Puts ("");} return 0;}


Codeforces 536B Tavas and Malekas for overlapping positions KMP

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.