2022/1/17


2022/1/17

[ D. Period ]( Problem - D - Codeforces )

思路

分析可知要找到后缀和前缀匹配的串。

kmp的next[i]数组表示以i为结尾的非前缀字串与字符串的前缀匹配的最大长度。

那么我们求出next[n]后。

1next[n],1[next[next[n]]],1~[next[next[next[n]]]]· · ·

都是与后缀匹配的前缀串。

用ans[x]记录周期长度>x合法的周期个数。

参考代码

#include
#define ll  long long
#define pii pair
#define se second
#define pb push_back
#define pf push_front
#define si size()
#define db double
#define ls (p<<1)
#define rs (p<<1|1)

#define fi first
#define se second
using namespace std;
ll read(){ll x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-') f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}return x*f;}
inline void Prin(ll x){if(x < 0){putchar('-');x = -x;}if(x > 9) Prin(x / 10);putchar(x % 10 + '0');}

const int qs=1e6+7;
const ll mod=998244353;

string s;
int nex[qs],ans[qs],n;
int main(){
	cin>>s; 
	int sz=s.size();
	s=" "+s;
	nex[1]=0;
	for(int i=2,j=0;i<=sz;++i){
		while(j>0&&s[i]!=s[j+1]) j=nex[j];
		if(s[i]==s[j+1]) j++;
		nex[i]=j;
	}
	int k=nex[sz];
	while(k){
		ans[k]++;
		k=nex[k];
	}
	for(int i=1;i<=sz;++i) ans[i]+=ans[i-1];
	n=read();
	while(n--){
		int q;
		q=read();
		q=min(q-1,sz-q);
		cout<

[ D. Martial Arts Tournament ]( Problem - D - Codeforces (Unofficial mirror site, accelerated for Chinese users) )

思路

枚举中间的块的大小,在此块大小的基础上枚举中间块区间左端点,二分右端点,找到一块区间。划分左边的块和右边的块,计算最小值即可。

参考代码

#include
#define ll  long long
#define pii pair
#define se second
#define pb push_back
#define pf push_front
#define si size()
#define db double
#define ls (p<<1)
#define rs (p<<1|1)
 
#define fi first
#define se second
using namespace std;
ll read(){ll x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-') f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}return x*f;}
inline void Prin(ll x){if(x < 0){putchar('-');x = -x;}if(x > 9) Prin(x / 10);putchar(x % 10 + '0');}
 
const int qs=2e5+7;
const ll mod=998244353;
 
 
ll T,n,a[qs];
ll f[3];
ll cal(ll x,ll y){
	f[0]=a[x-1];
	f[1]=a[n]-a[y-1];
	f[2]=n-f[1]-f[0];
	ll ans=0;
	for(int i=0;i<3;++i){
		for(int j=0;j<=30;++j){
			ll p=(1<