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<bits/stdc++.h>
#define ll long long
#define pii pair<long long , long long >
#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<<ans[q]<<"\n";
}
return 0;
}
/*
*/
[ D. Martial Arts Tournament ]( Problem - D - Codeforces (Unofficial mirror site, accelerated for Chinese users) )
思路
枚举中间的块的大小,在此块大小的基础上枚举中间块区间左端点,二分右端点,找到一块区间。划分左边的块和右边的块,计算最小值即可。
参考代码
#include<bits/stdc++.h>
#define ll long long
#define pii pair<long long , long long >
#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<<j);
if(f[i]<=p){
ans=ans+p-f[i];
break;
}
}
}
return ans;
}
ll ck(ll sum,ll x,ll y){
ll ret=a[y-1]-a[x-1];
return ret<=sum;
}
int main(){
T=read();
while(T--){
n=read();
for(int i=1;i<=n+1;++i) a[i]=0;
for(int i=1;i<=n;++i) {
ll x; x=read();
a[x]++;
}
for(int i=1;i<=n+1;++i) a[i]+=a[i-1];
ll ans=1e16;
for(ll i=-1;i<=30;++i){
ll sum;
if(i==-1) sum=0;
else sum=(1<<i);
for(ll j=0;j<=n+1;++j){
ll l=j+1,r=n+1,ret=-1;
while(l<=r){
ll md=(l+r)/2;
if(ck(sum,j,md)) ret=md,l=md+1;
else r=md-1;
}
if(ret==-1) continue;
ans=min(ans,cal(j,ret));
}
}
cout<<ans<<"\n";
}
return 0;
}
/*
*/