zoukankan      html  css  js  c++  java
  • (KMP、dp)Codeforces Educational Codeforces Round 21 G-Anthem of Berland

    Berland has a long and glorious history. To increase awareness about it among younger citizens, King of Berland decided to compose an anthem.

    Though there are lots and lots of victories in history of Berland, there is the one that stand out the most. King wants to mention it in the anthem as many times as possible.

    He has already composed major part of the anthem and now just needs to fill in some letters. King asked you to help him with this work.

    The anthem is the string s of no more than 105 small Latin letters and question marks. The most glorious victory is the string t of no more than 105 small Latin letters. You should replace all the question marks with small Latin letters in such a way that the number of occurrences of string t in string s is maximal.

    Note that the occurrences of string t in s can overlap. Check the third example for clarification.

    Input

    The first line contains string of small Latin letters and question marks s (1 ≤ |s| ≤ 105).

    The second line contains string of small Latin letters t (1 ≤ |t| ≤ 105).

    Product of lengths of strings |s|·|t| won't exceed 107.

    Output

    Output the maximum number of occurrences of string t you can achieve by replacing all the question marks in string s with small Latin letters.

    Example

    Input
    winlose???winl???w??
    win
    Output
    5
    Input
    glo?yto?e??an?
    or
    Output
    3
    Input
    ??c?????
    abcab
    Output
    2

    Note

    In the first example the resulting string s is "winlosewinwinlwinwin"

    In the second example the resulting string s is "glorytoreorand". The last letter of the string can be arbitrary.

    In the third example occurrences of string t are overlapping. String s with maximal number of occurrences of t is "abcabcab".

     1 #include <iostream>
     2 #include <string>
     3 #include <algorithm>
     4 #include <cstring>
     5 #include <cstdio>
     6 #include <cmath>
     7 #include <queue>
     8 #include <set>
     9 #include <map>
    10 #include <list>
    11 #include <vector>
    12 #include <stack>
    13 #define mp make_pair
    14 typedef long long ll;
    15 typedef unsigned long long ull;
    16 const int MAX=1e5+5;
    17 const int INF=1e9+5;
    18 const double M=4e18;
    19 using namespace std;
    20 const int MOD=1e9+7;
    21 typedef pair<int,int> pii;
    22 const double eps=0.000000001;
    23 int f[MAX],dp[MAX],mx[MAX];
    24 bool can[MAX];
    25 int len1,len2;
    26 void getf(char*x,int m)//m为串的长度
    27 {
    28     f[0]=f[1]=0;
    29     for(int i=2,j=0;i<=m;i++)
    30     {
    31         while(j&&x[j+1]!=x[i])
    32             j=f[j];
    33         if(x[j+1]==x[i])
    34             j++;
    35         f[i]=j;
    36 //        printf("~%d %d
    ",i,f[i]);
    37     }
    38 }
    39 //string s,t;
    40 char s[MAX],t[MAX];
    41 int i,j;
    42 int main()
    43 {
    44 //    cin>>s>>t;
    45     scanf("%s",s+1);
    46     scanf("%s",t+1);
    47 //    len1=s.length();
    48 //    len2=t.length();
    49     len1=strlen(s+1);
    50     len2=strlen(t+1);
    51 //    printf("%d %d
    ",len1,len2);
    52     getf(t,len2);
    53     for(i=len2;i<=len1;++i)
    54     {
    55         /*对s的每个位置倒序向前匹配t*/
    56         for(j=0;j<len2;++j)
    57             if(s[i-j]!='?'&&s[i-j]!=t[len2-j])
    58                 break;
    59         if(j==len2)
    60             can[i]=true;
    61     }
    62     for(i=len2;i<=len1;i++)
    63     {
    64         if(can[i])//以此位置为结尾的连续len2个字符组成的串可以完整匹配
    65         {
    66             j=f[len2];
    67             while(j)
    68             {
    69                 dp[i]=max(dp[i],dp[i-(len2-j)]);
    70                 j=f[j];
    71             }
    72             if(i>=len2)
    73                 dp[i]=max(dp[i],mx[i-len2]);
    74             ++dp[i];
    75         }
    76         mx[i]=dp[i];
    77         if(i)
    78             mx[i]=max(mx[i],mx[i-1]);
    79     }
    80     printf("%d
    ",mx[len1]);
    81     return 0;
    82 }
  • 相关阅读:
    Hibernate Annotation (Hibernate 注解)
    org/objectweb/asm/Type异常解决办法
    Spring3 MVC 总结(转)
    Spring mvc 3 在controller和视图之间传递参数
    各种树tree的js控件优缺点
    Spring MVC 的请求参数获取的几种方法
    解决javax.persistence.OneToMany.orphanRemoval()Z异常办法
    ModelAndView返回自己的用法
    javax.persistence.Entity异常解决方法
    调用http://WebXml.com.cn/的webservice获取手机号段信息
  • 原文地址:https://www.cnblogs.com/quintessence/p/6886612.html
Copyright © 2011-2022 走看看