Codeforces 494B B. Obsessive String (DP)

Source: Internet
Author: User

Topic Links:

Codeforces 494B

Main topic:

Gives two strings, asking how many methods the first string extracts to make the substrings contain T-pattern strings.

Topic Analysis:
  • Define state Dp[i] represents the number of methods by which the first I-character gets the string group that meets the requirements.
  • d P[I]=d P[I?1]+ ∑ j = 0 l? 1 d P[J]+L
  • Explain:
    • First Dp[i-1] represents the case that does not re-construct a new substring, then the front if selected, then the current bit must be selected, the previous vacancy, then the current position must be vacant, so the number of cases is equal to dp[i-1]
    • After considering L is to ensure that the current paragraph contains the maximum number of T, fixed the right position, and then enumerate the left position, because it will be more than the previous situation of the case of only the string itself, so it is dp[j]+1, and then extracted to become the final plus L.
AC Code:
#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>#define MAX 100007using namespace STD;CharS[max],t[max];intMark[max];Const intMoD =1e9+7;intDp[max];intSum[max];voidGet_next (CharP[],intNext []) {inti =0, k =-1, Len =strlen(p); next[0] = -1; while(I < Len)if(k = =-1|| P[i] = = P[k]) i++,k++,next[i]=k;Elsek = Next[k];}voidMatch (CharS[],CharP[]) {memset(Mark,0,sizeof(Mark));intNext[max]; Get_next (P, next);intLen1 =strlen(s);intLen2 =strlen(p);inti =0, j =0; while(I < LEN1) {if(j = =-1|| S[i] = = P[j]) i++, j + +;Elsej = Next[j];if(j = = len2) Mark[i] = i-len2+1; }}intMain () { while( ~scanf('%s ', s)) {scanf('%s ', t);        Match (S, T); sum[0] = dp[0] =0;intn =strlen(s); for(inti =1; I <= N; i++)if(!mark[i]) mark[i] = mark[i-1]; for(inti =1; I <= N; i++) {Dp[i] = dp[i-1];intL = mark[i];if(!l)Continue; Dp[i] + = (sum[l-1]+L)%mod;            Dp[i]%= mod; Sum[i] = sum[i-1] + dp[i];        Sum[i]%= mod; }printf("%d\n", Dp[n]); }}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Codeforces 494B B. Obsessive String (DP)

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.