(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 }
时间: 2024-08-27 01:36:13

(KMP、dp)Codeforces Educational Codeforces Round 21 G-Anthem of Berland的相关文章

Educational Codeforces Round 21 G. Anthem of Berland(dp+kmp)

题目链接:Educational Codeforces Round 21 G. Anthem of Berland 题意: 给你两个字符串,第一个字符串包含问号,问号可以变成任意字符串. 问你第一个字符串最多包含多少个第二个字符串. 题解: 考虑dp[i][j],表示当前考虑到第一个串的第i位,已经匹配到第二个字符串的第j位. 这样的话复杂度为26*n*m*O(fail). fail可以用kmp进行预处理,将26个字母全部处理出来,这样复杂度就变成了26*n*m. 状态转移看代码(就是一个kmp

Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes

Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes 题目连接: http://codeforces.com/contest/985/problem/E Description Mishka received a gift of multicolored pencils for his birthday! Unfortunately he lives in a monochrome w

Codeforces Educational Codeforces Round 15 C. Cellular Network

C. Cellular Network time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standard output You are given n points on the straight line — the positions (x-coordinates) of the cities and m points on the same line

codeforces Educational Codeforces Round 5 A. Comparing Two Long Integers

题目链接:http://codeforces.com/problemset/problem/616/A 题目意思:顾名思义,就是比较两个长度不超过 1e6 的字符串的大小 模拟即可.提供两个版本,数组版本 & 指针版本. (1)数组版本(短的字符串从高位处补0,直到跟长的字符串长度相同) 1 #include <iostream> 2 #include <cstdio> 3 #include <cstdlib> 4 #include <cstring>

(最小生成树)Codeforces Educational Codeforces Round 9 Magic Matrix

You're given a matrix A of size n?×?n. Let's call the matrix with nonnegative elements magic if it is symmetric (so aij?=?aji), aii?=?0 and aij?≤?max(aik,?ajk) for all triples i,?j,?k. Note that i,?j,?k do not need to be distinct. Determine if the ma

Codeforces Educational Codeforces Round 57 题解

传送门 Div 2的比赛,前四题还有那么多人过,应该是SB题,就不讲了. 这场比赛一堆计数题,很舒服.(虽然我没打) E. The Top Scorer 其实这题也不难,不知道为什么这么少人过. 考虑枚举那人的分数和有多少人和他同分,推一下就会发现我们只需要知道\(calc(sum,n,top)\)表示\(sum\)分,分给\(n\)个人,分数小于\(top\),的方案数. 好像不是很好直接搞,考虑容斥,枚举一下至少有几个人不满足条件即可. #include<bits/stdc++.h> na

codeforces Educational Codeforces Round 2 C Make Palindrome

C. Make Palindrome A string is called palindrome if it reads the same from left to right and from right to left. For example "kazak", "oo", "r" and "mikhailrubinchikkihcniburliahkim" are palindroms, but strings &quo

codeforces Educational Codeforces Round 9 E - Thief in a Shop

E - Thief in a Shop 题目大意:给你n ( n <= 1000)个物品每个物品的价值为ai (ai <= 1000),你只能恰好取k个物品,问你能组成哪些价值. 思路:我们很容易能够想到dp[ i ][ j ]表示取i次j是否存在,但是复杂度1e12肯定不行. 我们将ai排序,每个值都减去a[1]然后再用dp[ i ]表示到达i这个值最少需要取几次,只需要1e9就能完成, 我们扫一遍dp数组,如果dp[ i ]  <= k 则说明 i + k * a[1]是能取到的.

Codeforces Educational Codeforces Round 54 题解

题目链接:https://codeforc.es/contest/1076 A. Minimizing the String 题意:给出一个字符串,最多删掉一个字母,输出操作后字典序最小的字符串. 题解:若存在一个位置 i 满足 a[i] > a[i+1],若不删除 a[i] 则后续操作不可能更优. 1 #include <bits/stdc++.h> 2 using namespace std; 3 #define ll long long 4 #define ull unsigned