(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\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 }

 

转载于:https://www.cnblogs.com/quintessence/p/6886612.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值