码迷,mamicode.com
首页 > 其他好文 > 详细

(KMP、dp)Codeforces Educational Codeforces Round 21 G-Anthem of Berland

时间:2017-05-22 00:11:28      阅读:320      评论:0      收藏:0      [点我收藏+]

标签:using   long   logs   color   mod   clu   print   sed   int   

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\n",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\n",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\n",mx[len1]);
81     return 0;
82 }

 

(KMP、dp)Codeforces Educational Codeforces Round 21 G-Anthem of Berland

标签:using   long   logs   color   mod   clu   print   sed   int   

原文地址:http://www.cnblogs.com/quintessence/p/6886612.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!