Codeforces 808G. Anthem of Berland

G. Anthem of Berland

题意:给两串S,T。S由"a-z","?"组成,T由"a-z"组成。你可以钦定这些"?",询问 T 在 S 中最多出现次数。

想法:考虑Dp,需要记录的是匹配到 T 多长的前缀。于是设$F[i][j]$表示 S 前 j 位现匹配 T 前i位的最多完整匹配次数。转移需要求出$nx[i][v]$表示 T 前i位后面接上v能匹配的最长前缀,用KMP求一下。

Code

#include < cstdio >
#include < cstring >

#define gec getchar
#define FILE(F) freopen(F".in","r",stdin),freopen(F".out","w",stdout)
#define DEBUG fprintf(stderr,"Passing [%s] in Line (%d)\n",__FUNCTION__,__LINE__);

typedef long long ll;
template
inline void read(T&x)
{
	x=0;bool f=0;char c=gec();
	for(;c<‘0‘||c>‘9‘;c=gec())f=(c==‘-‘);
	for(;c>=‘0‘&&c<=‘9‘;c=gec())x=x*10+c-‘0‘;
	x=f?-x:x;
}

const int MAXN(100010);
int lengs,lengt,Ans;
char t[MAXN],s[MAXN];
namespace KMP
{
	int next[MAXN],nx[MAXN][26];
	void Kmp()
	{
		int j=0;next[1]=0;
		for(int i=2;i<=lengt;i++)
		{
			while(j&&(t[i]!=t[j+1]||j==lengt))j=next[j];
			if(t[i]==t[j+1])j++;
			next[i]=j;
		}
		for(int i=0;i<=lengt;i++)
		{
			for(int v=0;v<26;v++)
			{
				int j=i;
				while(j&&(v!=t[j+1]-‘a‘||j==lengt))j=next[j];
				if(v==t[j+1]-‘a‘)j++;
				nx[i][v]=j;
			}
		}
	}

}using namespace KMP;

namespace DP
{
	int N,M;
	int max(int a,int b){return a>b?a:b;}
	void Dp()
	{
		N=lengt+10; M=lengs+10;
		int F[N][M]; memset(F,128,sizeof(F));
		F[0][0]=0;
		for(int j=0,Nex;j<lengs;j++)
		{
			for(int i=0;i<=lengt;i++)
			{
				if(F[i][j]<0)continue;
				if(s[j+1]==‘?‘)
				for(int v=0;v<26;v++)
				{
					Nex=nx[i][v];
					F[Nex][j+1]=max(F[Nex][j+1],F[i][j]+(Nex==lengt));
				} else
				{
					Nex=nx[i][s[j+1]-‘a‘];
					F[Nex][j+1]=max(F[Nex][j+1],F[i][j]+(Nex==lengt));
				}
			}
		}
		for(int i=0;i<=lengt;i++)Ans=max(Ans,F[i][lengs]);
	}

}using namespace DP;

int main()
{
#ifndef ONLINE_JUDGE
	FILE("C");
#endif
	scanf("%s",s+1);lengs=strlen(s+1);
	scanf("%s",t+1);lengt=strlen(t+1);
	if(lengt>lengs){printf("0\n");return 0;}
	Kmp(); 	Dp();
	printf("%d\n",Ans);
	return 0;
}

突然迷恋 namespace

时间: 2024-10-18 18:01:07

Codeforces 808G. Anthem of Berland的相关文章

Codeforces 808G Anthem of Berland(KMP+基础DP)

题意 给定一个字符串 \(s\) ,一个字符串 \(t\) ,其中 \(s\) 包含小写字母和 "?" ,\(t\) 只包含小写字母,现在把 \(s\) 中的问号替换成任意的小写字母,求 \(t\) 最多在 \(s\) 中出现多少次,\(t\) 可以互相覆盖. \(1 \leq |s| \leq 10^5\) \(1 \leq |t| \leq 10^5\) \(1 \leq |s|\cdot|t| \leq 10^7\) 思路 由于 \(|s|\cdot|t| \leq 10^7\)

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 691A Fashion in Berland

题目链接: http://codeforces.com/problemset/problem/691/A 题目大意: n个数0或1,要求恰好n-1个1,如果n为1则那个数一定要是1 题目思路: [模拟] 水题一道.看错题目两次.. 1 // 2 //by coolxxx 3 //#include<bits/stdc++.h> 4 #include<iostream> 5 #include<algorithm> 6 #include<string> 7 #in

CodeForces 723D Lakes in Berland (dfs搜索)

题意:给定一个n*m的矩阵,*表示陆地, . 表示水,一些连通的水且不在边界表示湖,让你填最少的陆地使得图中湖剩下恰好为k. 析:很简单的一个搜索题,搜两次,第一次把每个湖的位置和连通块的数量记下来,第二次去填陆地,选少的进行填. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdli

CodeForces 723D Lakes in Berland

连通块. 求出连通块,排序即可. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<vector> #include<map> #include<set> #include<qu

codeforces 589 I - Lottery(水)

I - Lottery Time Limit:2000MS     Memory Limit:524288KB     64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 589I Description Today Berland holds a lottery with a prize — a huge sum of money! There are k persons, who attend the lottery

2019/7/19 CF808(div2)

A.Lucky Year 对于只存在一个不为0的位数,那么先把位数求出来,若只有一个,那么是1-9,任意+1都满足,所以为1,若不是1,那么就是最高位+1 ,后面全是0,减去原来的数字. 1 #include<bits/stdc++.h> 2 using namespace std; 3 int main() 4 { 5 int y,count=0,x,d; 6 cin>>y; 7 d=y; 8 while(y!=0) 9 { 10 count++; 11 if((y/10)==0

(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.

CodeForces 567B Berland National Library

Description Berland National Library has recently been built in the capital of Berland. In addition, in the library you can take any of the collected works of Berland leaders, the library has a reading room. Today was the pilot launch of an automated