A
这么水吗,建议降黄。
直接二分答案为 ,然后对于每个长度为 的区间,存储他的哈希值,若最多的那个区间哈希值 ,则这个解可行。
。
#include<bits/stdc++.h>
using namespace std;
#define int long long
namespace florr{
#ifdef __linux__
#define gc getchar_unlocked
#define pc putchar_unlocked
#else
#define gc _getchar_nolock
#define pc _putchar_nolock
#endif
#define meow ios_base::sync_with_stdio(false);cin.tie(0)
#define rep(i,l,r,k) for(int i=l;i<=r;i+=k)
#define rrep(i,r,l,k) for(int i=r;i>=l;i-=k)
#define loop(i,st,ed,nxt) for(int i=st;i!=ed;i=nxt)
#define file(s) freopen(s".in","r",stdin),freopen(s".out","w",stdout);
const int inf=0x3f3f3f3f;
const long long infll=0x3f3f3f3f3f3f3f3fll;
const __int128 infi128=(__int128(1)<<126);
mt19937 __rnd(time(0));
template<typename T=int>T rd()
{
T x=0;bool w=0;
char ch=gc();
for(;!isdigit(ch);ch=gc())w|=(ch=='-');
for(;isdigit(ch);ch=gc())x=(x<<3)+(x<<1)+(ch^48);
return w?-x:x;
}
template<typename T=int>void wt(T x)
{
if(x<0)pc('-'),x=-x;
if(x>9)wt(x/10);
pc(x%10+48);
}
__int128 abs(__int128 x){return x<0?-x:x;}
int rnd(int l,int r){return abs((int)(__rnd()))%(r-l+1)+l;}
string rdstr(string charSet)
{
bool mp[256];
memset(mp,0,sizeof mp);
for(auto chars:charSet)mp[chars]=1;
string s;
char ch=gc();
for(;ch!=EOF&&!mp[ch];ch=gc());
for(;mp[ch];ch=gc())s+=ch;
return s;
}
void wtstr(string s,int l,int r){rep(i,l,r,1)pc(s[i]);}
template<typename T,int N>struct Fenwick
{
T tr[N+5];
void add(int x,int v){rep(i,x,N,i&-i)tr[i]+=v;}
int qry(int x){return x<=0?0:tr[x]+qry(x-(x&-x));}
};
}//namespace florr
using namespace florr;
#define ull unsigned long long
const int N=2e5+5;
int n,k,a[N];
ull h[N],pw[N],b[N];
ull qry(int l,int r){return h[r]-h[l-1]*pw[r-l+1];}
map<ull,int>mp;
bool check(int mid)
{
mp.clear();
rep(i,1,n-mid+1,1)
{
++mp[qry(i,i+mid-1)];
if(mp[qry(i,i+mid-1)]>=k)return 1;
}
return 0;
}
void solve()
{
n=rd(),k=rd();pw[0]=1;
rep(i,1,n,1)
{
a[i]=rd();
h[i]=h[i-1]*131+a[i];
pw[i]=pw[i-1]*131;
}
int l=0,r=n+1;
while(l+1<r)
{
int mid=(l+r)>>1;
if(check(mid))l=mid;
else r=mid;
}
wt(l);
}
signed main()
{
// file("");
// meow;
signed T=1;
//cin>>T;
//T=rd<signed>();
rep(i,1,T,1)solve();
}
/*
1. freopen 开了吗?
2. long long 开了吗
3. 数组开的够大吗
4. 对拍过了吗
5. 极限数据测了吗
*/
B
这么水吗,建议降黄。
枚举 和 ,设 为以 开头长度 的回文串数量,则不难发现这一对 和 的贡献是 。
。
#include<bits/stdc++.h>
using namespace std;
#define int long long
namespace florr{
#ifdef __linux__
#define gc getchar_unlocked
#define pc putchar_unlocked
#else
#define gc _getchar_nolock
#define pc _putchar_nolock
#endif
#define meow ios_base::sync_with_stdio(false);cin.tie(0)
#define rep(i,l,r,k) for(int i=l;i<=r;i+=k)
#define rrep(i,r,l,k) for(int i=r;i>=l;i-=k)
#define loop(i,st,ed,nxt) for(int i=st;i!=ed;i=nxt)
#define file(s) freopen(s".in","r",stdin),freopen(s".out","w",stdout);
const int inf=0x3f3f3f3f;
const long long infll=0x3f3f3f3f3f3f3f3fll;
const __int128 infi128=(__int128(1)<<126);
mt19937 __rnd(time(0));
template<typename T=int>T rd()
{
T x=0;bool w=0;
char ch=gc();
for(;!isdigit(ch);ch=gc())w|=(ch=='-');
for(;isdigit(ch);ch=gc())x=(x<<3)+(x<<1)+(ch^48);
return w?-x:x;
}
template<typename T=int>void wt(T x)
{
if(x<0)pc('-'),x=-x;
if(x>9)wt(x/10);
pc(x%10+48);
}
__int128 abs(__int128 x){return x<0?-x:x;}
int rnd(int l,int r){return abs((int)(__rnd()))%(r-l+1)+l;}
string rdstr(string charSet)
{
bool mp[256];
memset(mp,0,sizeof mp);
for(auto chars:charSet)mp[chars]=1;
string s;
char ch=gc();
for(;ch!=EOF&&!mp[ch];ch=gc());
for(;mp[ch];ch=gc())s+=ch;
return s;
}
void wtstr(string s,int l,int r){rep(i,l,r,1)pc(s[i]);}
template<typename T,int N>struct Fenwick
{
T tr[N+5];
void add(int x,int v){rep(i,x,N,i&-i)tr[i]+=v;}
int qry(int x){return x<=0?0:tr[x]+qry(x-(x&-x));}
};
}//namespace florr
using namespace florr;
const int N=5005;
const string charSet="qwertyuiopasdfghjklzxcvbnm";
string s;
int cnt[N][N];
void solve()
{
s=rdstr(charSet);
int n=s.size(),ans=0;
s="#"+s;
rep(i,1,n,1)//
{
int p=1;
++cnt[i-p+1][(p<<1)-1];
while(i-p>0&&i+p<=n&&s[i-p]==s[i+p])
{
++p;
++cnt[i-p+1][(p<<1)-1];
}
if(i==n||s[i]!=s[i+1])continue;
p=1;
++cnt[i-p+1][p<<1];
while(i-p>0&&i+p+1<=n&&s[i-p]==s[i+p+1])
{
++p;
++cnt[i-p+1][p<<1];
}
}
rep(i,1,n,1)rrep(j,n,1,1)
cnt[i][j]+=cnt[i][j+1];
rep(len,2,n,1)rep(i,1,n,1)
{
int j=i+len-1;
ans+=(cnt[i][1]-cnt[i][len])*cnt[j][1];
}
wt(ans);
}
signed main()
{
// file("B");
// meow;
signed T=1;
//cin>>T;
//T=rd<signed>();
rep(i,1,T,1)solve();
}
/*
1. freopen 开了吗?
2. long long 开了吗
3. 数组开的够大吗
4. 对拍过了吗
5. 极限数据测了吗
*/
C
我们发现删掉原串 的最长回文前后缀后答案不会更优。所以我们考虑删掉了最长回文前后缀后的字符串 ,简单枚举开头 然后二分即可。
#include<bits/stdc++.h>
using namespace std;
// #define int long long
namespace florr{
#ifdef __linux__
#define gc getchar_unlocked
#define pc putchar_unlocked
#else
#define gc _getchar_nolock
#define pc _putchar_nolock
#endif
#define meow ios_base::sync_with_stdio(false);cin.tie(0)
#define rep(i,l,r,k) for(int i=l;i<=r;i+=k)
#define rrep(i,r,l,k) for(int i=r;i>=l;i-=k)
#define loop(i,st,ed,nxt) for(int i=st;i!=ed;i=nxt)
#define file(s) freopen(s".in","r",stdin),freopen(s".out","w",stdout);
const int inf=0x3f3f3f3f;
const long long infll=0x3f3f3f3f3f3f3f3fll;
const __int128 infi128=(__int128(1)<<126);
mt19937 __rnd(time(0));
template<typename T=int>T rd()
{
T x=0;bool w=0;
char ch=gc();
for(;!isdigit(ch);ch=gc())w|=(ch=='-');
for(;isdigit(ch);ch=gc())x=(x<<3)+(x<<1)+(ch^48);
return w?-x:x;
}
template<typename T=int>void wt(T x)
{
if(x<0)pc('-'),x=-x;
if(x>9)wt(x/10);
pc(x%10+48);
}
__int128 abs(__int128 x){return x<0?-x:x;}
int rnd(int l,int r){return abs((int)(__rnd()))%(r-l+1)+l;}
string rdstr(string charSet)
{
bool mp[256];
memset(mp,0,sizeof mp);
for(auto chars:charSet)mp[chars]=1;
string s;
char ch=gc();
for(;ch!=EOF&&!mp[ch];ch=gc());
for(;mp[ch];ch=gc())s+=ch;
return s;
}
void wtstr(string s,int l,int r){rep(i,l,r,1)pc(s[i]);}
template<typename T,int N>struct Fenwick
{
T tr[N+5];
void add(int x,int v){rep(i,x,N,i&-i)tr[i]+=v;}
int qry(int x){return x<=0?0:tr[x]+qry(x-(x&-x));}
};
}//namespace florr
using namespace florr;
#define ull unsigned long long
const int N=5e5+5;
const string charSet="qwertyuiopasdfhjklgzxcvbnm";
int n,m;
string s;
ull hh[N],ht[N],pw[N];
ull qry(int op,int l,int r)
{
if(op==1)return hh[r]-hh[l-1]*pw[r-l+1];
return ht[l]-ht[r+1]*pw[r-l+1];
}
int S(int s,int e)
{
int l=0,r=(e-s+1)/2+1;
while(l+1<r)
{
int mid=(l+r)>>1;
if(qry(1,s,s+mid-1)==qry(9178114,e-mid+1,e))
l=mid;
else r=mid;
}
return l;
}
int sol(string t)
{
pw[0]=1;
rep(i,1,m,1)hh[i]=hh[i-1]*131+t[i],pw[i]=pw[i-1]*131;
rrep(i,m,1,1)ht[i]=ht[i+1]*131+t[i];
int res=0;
rep(i,1,m,1)res=max(res,S(i+1,m));
return res;
}
void solve()
{
s=rdstr(charSet);
n=s.size();
s="#"+s+"#";
int l=1,r=n;
for(;l<r;++l,--r)if(s[l]!=s[r])break;
if(l>r)wt(n>>1),exit(0);
int ans1=l-1;
string t;
rep(i,l,r,1)t+=s[i];
m=t.size();
t="#"+t+"#";
int ans2=sol(t);
reverse(t.begin(),t.end());
wt(ans1+max(ans2,sol(t)));
}
signed main()
{
// file("");
// meow;
signed T=1;
//cin>>T;
//T=rd<signed>();
rep(i,1,T,1)solve();
}
/*
1. freopen 开了吗?
2. long long 开了吗
3. 数组开的够大吗
4. 对拍过了吗
5. 极限数据测了吗
*/
:证明:如果删掉了一个前后缀后仍然最优,则新的拼接上的串一定和前后缀之一一样,所以不优。
D
这么水,建议降黄。
对于每一个位置 二分一下然后判一下即可。
#include<bits/stdc++.h>
using namespace std;
#define int long long
namespace florr{
#ifdef __linux__
#define gc getchar_unlocked
#define pc putchar_unlocked
#else
#define gc _getchar_nolock
#define pc _putchar_nolock
#endif
#define meow ios_base::sync_with_stdio(false);cin.tie(0)
#define rep(i,l,r,k) for(int i=l;i<=r;i+=k)
#define rrep(i,r,l,k) for(int i=r;i>=l;i-=k)
#define loop(i,st,ed,nxt) for(int i=st;i!=ed;i=nxt)
#define file(s) freopen(s".in","r",stdin),freopen(s".out","w",stdout);
const int inf=0x3f3f3f3f;
const long long infll=0x3f3f3f3f3f3f3f3fll;
const __int128 infi128=(__int128(1)<<126);
mt19937 __rnd(time(0));
template<typename T=int>T rd()
{
T x=0;bool w=0;
char ch=gc();
for(;!isdigit(ch);ch=gc())w|=(ch=='-');
for(;isdigit(ch);ch=gc())x=(x<<3)+(x<<1)+(ch^48);
return w?-x:x;
}
template<typename T=int>void wt(T x)
{
if(x<0)pc('-'),x=-x;
if(x>9)wt(x/10);
pc(x%10+48);
}
__int128 abs(__int128 x){return x<0?-x:x;}
int rnd(int l,int r){return abs((int)(__rnd()))%(r-l+1)+l;}
string rdstr(string charSet)
{
bool mp[256];
memset(mp,0,sizeof mp);
for(auto chars:charSet)mp[chars]=1;
string s;
char ch=gc();
for(;ch!=EOF&&!mp[ch];ch=gc());
for(;mp[ch];ch=gc())s+=ch;
return s;
}
void wtstr(string s,int l,int r){rep(i,l,r,1)pc(s[i]);}
template<typename T,int N>struct Fenwick
{
T tr[N+5];
void add(int x,int v){rep(i,x,N,i&-i)tr[i]+=v;}
int qry(int x){return x<=0?0:tr[x]+qry(x-(x&-x));}
};
}//namespace florr
using namespace florr;
const int N=5e5+5;
#define ull unsigned long long
ull h[2][N],pw[N];
ull qry(ull *ha,int l,int r){return ha[r]-ha[l-1]*pw[r-l+1];}
void solve()
{
int n=rd(),ans=0;
string s="#"+rdstr("01")+"#";pw[0]=1;
rep(i,1,n,1)h[0][i]=h[0][i-1]*131+s[i],pw[i]=pw[i-1]*131;
reverse(s.begin(),s.end());
rep(i,1,n,1)h[1][i]=h[1][i-1]*131+(s[i]=='1'?'0':'1');
rep(i,1,n,1)
{
int l=0,r=n+1;
while(l+1<r)
{
int mid=(l+r)>>1,L=i-mid+1,R=i+mid-1;
if(L>0&&R<=n&&qry(h[0],L,R)==qry(h[1],n-R+1,n-L+1))l=mid;
else r=mid;
}
ans+=l;
l=0,r=n+1;
while(l+1<r)
{
int mid=(l+r)>>1,L=i-mid+1,R=i+mid;
if(L>0&&R<=n&&qry(h[0],L,R)==qry(h[1],n-R+1,n-L+1))l=mid;
else r=mid;
}
ans+=l;
}
wt(ans);
}
signed main()
{
// file("");
// meow;
signed T=1;
//cin>>T;
//T=rd<signed>();
rep(i,1,T,1)solve();
}
/*
1. freopen 开了吗?
2. long long 开了吗
3. 数组开的够大吗
4. 对拍过了吗
5. 极限数据测了吗
*/