QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#685157 | #9522. A Simple String Problem | Crysfly | AC ✓ | 512ms | 184136kb | C++14 | 5.5kb | 2024-10-28 17:58:58 | 2024-11-10 22:37:22 |
Judging History
你现在查看的是最新测评结果
- [2024-11-10 22:36:11]
- hack成功,自动添加数据
- (/hack/1163)
- [2024-11-06 21:48:00]
- hack成功,自动添加数据
- (/hack/1142)
- [2024-10-28 17:58:58]
- 提交
answer
#include<bits/stdc++.h>
#define For(i,a,b) for(int i=(a);i<=(b);++i)
#define Rep(i,a,b) for(int i=(a);i>=(b);--i)
#define int long long
using namespace std;
inline int read()
{
int x;cin>>x;return x;
}
#define mod 1000000007
struct modint{
unsigned int x;
modint(int o=0){x=o;}
modint &operator = (int o){return x=o,*this;}
modint &operator +=(modint o){return x=x+o.x>=mod?x+o.x-mod:x+o.x,*this;}
modint &operator -=(modint o){return x=x<o.x?x-o.x+mod:x-o.x,*this;}
modint &operator *=(modint o){return x=1ll*x*o.x%mod,*this;}
modint &operator ^=(int b){
modint a=*this,c=1;
for(;b;b>>=1,a*=a)if(b&1)c*=a;
return x=c.x,*this;
}
modint &operator /=(modint o){return *this *=o^=mod-2;}
friend modint operator +(modint a,modint b){return a+=b;}
friend modint operator -(modint a,modint b){return a-=b;}
friend modint operator *(modint a,modint b){return a*=b;}
friend modint operator /(modint a,modint b){return a/=b;}
friend modint operator ^(modint a,int b){return a^=b;}
friend bool operator ==(modint a,modint b){return a.x==b.x;}
friend bool operator !=(modint a,modint b){return a.x!=b.x;}
bool operator ! () {return !x;}
modint operator - () {return x?mod-x:0;}
bool operator <(const modint&b)const{return x<b.x;}
};
inline modint qpow(modint x,int y){return x^y;}
vector<modint> fac,ifac,iv;
inline void initC(int n)
{
if(iv.empty())fac=ifac=iv=vector<modint>(2,1);
int m=iv.size(); ++n;
if(m>=n)return;
iv.resize(n),fac.resize(n),ifac.resize(n);
For(i,m,n-1){
iv[i]=iv[mod%i]*(mod-mod/i);
fac[i]=fac[i-1]*i,ifac[i]=ifac[i-1]*iv[i];
}
}
inline modint C(int n,int m){
if(m<0||n<m)return 0;
return initC(n),fac[n]*ifac[m]*ifac[n-m];
}
inline modint sign(int n){return (n&1)?(mod-1):(1);}
#define fi first
#define se second
#define pb push_back
#define mkp make_pair
typedef pair<int,int>pii;
typedef vector<int>vi;
#define maxn 500055
#define inf 0x3f3f3f3f
struct SA{
int n,m,sa[maxn],x[maxn],y[maxn],c[maxn],h[maxn];
char s[maxn];
int st[21][maxn];
void getsa()
{
m=127;
For(i,1,n)c[x[i]=s[i]]++;
For(i,2,m)c[i]+=c[i-1];
Rep(i,n,1)sa[c[x[i]]--]=i;
for(int k=1;k<=n;k<<=1)
{
int num=0;
For(i,n-k+1,n)y[++num]=i;
For(i,1,n)if(sa[i]>k)y[++num]=sa[i]-k;
For(i,1,m)c[i]=0;
For(i,1,n)c[x[i]]++;
For(i,2,m)c[i]+=c[i-1];
Rep(i,n,1)sa[c[x[y[i]]]--]=y[i],y[i]=0;
swap(x,y);
x[sa[1]]=num=1;
For(i,2,n)
x[sa[i]]=(y[sa[i]]==y[sa[i-1]] && y[sa[i]+k]==y[sa[i-1]+k])?num:++num;
if(num==n)break;
m=num;
}
}
void geth(){
int k=0;
For(i,1,n)x[sa[i]]=i;
For(i,1,n)
{
if(k)k--;
int j=sa[x[i]-1];
while(j+k<=n&&i+k<=n&&s[j+k]==s[i+k])k++;
h[x[i]]=k;
}
}
void build(){
getsa(),geth();
For(i,1,n)st[0][i]=h[i];
For(j,1,20)
For(i,1,n+1-(1<<j))
st[j][i]=min(st[j-1][i],st[j-1][i+(1<<(j-1))]);
}
inline int lcp(int a,int b){
if(a==b)return n-a+1;
int l=x[a],r=x[b];
if(l>r)swap(l,r);
l++;int k=__lg(r-l+1);
return min(st[k][l],st[k][r-(1<<k)+1]);
}
void rever(){
reverse(s+1,s+n+1);
}
}sa,sa2;
int n;
char s[3][200005];
void GC(char&ch){
while(!isalpha(ch=getchar()));
}
int id[3][200005];
int lcp(int x,int y,int xx,int yy){
int u=id[x][y],v=id[xx][yy];
if(!u||!v)return 0;
int res=sa.lcp(u,v);
res=min(res,min(n+(x-1)-y+1,n+(xx-1)-yy+1));
return res;
}
int lcs(int x,int y,int xx,int yy){
int u=id[x][y],v=id[xx][yy];
if(!u||!v)return 0;
// cout<<"u,v "<<u<<" "<<v<<"\n";
u=sa.n+1-u;
v=sa.n+1-v;
// cout<<"idlcs: u,v: "<<u<<" "<<v<<"\n";
// For(i,1,sa2.n) cout<<sa2.s[i]<<" "; cout<<" sa2\n";
int res=sa2.lcp(u,v);
res=min(res,min(y-(x-1),yy-(xx-1)));
return res;
}
void work()
{
n=read();
For(i,1,n) GC(s[1][i]),sa.s[++sa.n]=s[1][i],id[1][i]=sa.n;
For(i,1,n) GC(s[2][i+1]),sa.s[++sa.n]=s[2][i+1],id[2][i+1]=sa.n;
sa2=sa;
sa2.rever();
sa.build();
sa2.build();
/* For(j,1,2){
For(i,1,n+1) cout<<id[j][i]<<" ";
cout<<"\n";
}
For(i,1,sa.n) cout<<sa.s[i]<<" "; cout<<"\n";*/
int res=0;
Rep(w,(n+1)/2,1){
for(int i=w;i+w<=n+1;i+=w){
int l=i,r=i+w;
// cout<<"l,r "<<l<<" "<<r<<"\n";
{
//case up
int ls=lcs(1,l,1,r);
int lp=lcp(1,l,1,r);
lp+=lcp(1,l+lp,2,r+lp);
// cout<<"ls,lp "<<ls<<" "<<lp<<"\n";
if(ls+lp>=w+1) res=max(res,w);
}
{
// case dw
int lp=lcp(2,l,2,r);
int ls=lcs(2,l,2,r);
ls+=lcs(1,l-ls,2,r-ls);
// cout<<"LS,LP "<<ls<<" "<<lp<<"\n";
if(ls+lp>=w+1) res=max(res,w);
}
{
// case up dw
int ls1=lcs(1,l,2,r);
int ls2=lcs(1,l-ls1,1,r-ls1);
int lp1=lcp(1,l,2,r);
int lp2=lcp(2,l+lp1,2,r+lp1);
// cout<<"ls1,ls2 lp1,lp2 "<<ls1<<" "<<ls2<<" "<<lp1<<" "<<lp2<<"\n";
if((ls1+lp1+lp2)>=w+1 || (ls2+ls1+lp1)>=w+1){
res=max(res,w);
}
}
}
if(res>0){
cout<<res*2<<"\n";
exit(0);
}
}
cout<<0<<"\n";
}
signed main()
{
int T=1;
while(T--)work();
return 0;
}
/*
6
aabbab
aabbaa
*/
这程序好像有点Bug,我给组数据试试?
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 21ms
memory: 118936kb
input:
5 abcab acabc
output:
6
result:
ok single line: '6'
Test #2:
score: 0
Accepted
time: 19ms
memory: 120128kb
input:
6 babbaa babaaa
output:
6
result:
ok single line: '6'
Test #3:
score: 0
Accepted
time: 22ms
memory: 120580kb
input:
2 ne fu
output:
0
result:
ok single line: '0'
Test #4:
score: 0
Accepted
time: 15ms
memory: 119764kb
input:
6 baabba baabab
output:
4
result:
ok single line: '4'
Test #5:
score: 0
Accepted
time: 19ms
memory: 120332kb
input:
6 aaabba aabbba
output:
2
result:
ok single line: '2'
Test #6:
score: 0
Accepted
time: 21ms
memory: 120752kb
input:
6 abaabb bbbaba
output:
4
result:
ok single line: '4'
Test #7:
score: 0
Accepted
time: 271ms
memory: 183200kb
input:
200000 wvblpxtatzytphgshchrevqqpnbljlorfoqubygysrivtemegmgrepemnlcbfqalpqqpvuockkbbvjnouhcerxqibevbipsxuscjejlcdtxoxipwrfrjwnriubvdgdgzlydwwiueovtrzljqxwfegejukncmahbcbsraxboukdesrzbwfvbpxpdntauidrybccwaocfwntohdkhxkfqhnoccyaylvvfebckmslrthubxrxvjoqcredanofbmgtsnswgszwhjckqeiddzvpnxphjkrwlsbthdvhzgn...
output:
200000
result:
ok single line: '200000'
Test #8:
score: 0
Accepted
time: 253ms
memory: 182712kb
input:
199999 klwumjcvuqinrkcsyvgfixhwvrwbmazkclblcnlpyxtlmmpkllkpukmxaurpukvgibcsuigcoqnnreivrlrwfdqofqcwubpolnnxcansyaevdjvnhnzvjeoejktaodusltockhtuqrohqfsrezdzurowghmcateztzlltkzlqynxpgbqsvgqtpukmfgdxopynqaegmjxvjixyzjrhbgahxwkswgpanhrdgpyvtvcpsihdvmtixfskuiprfqrfknohthfblkzlrcyqewdycoljgwrhjkmoxqmtogmg...
output:
200000
result:
ok single line: '200000'
Test #9:
score: 0
Accepted
time: 280ms
memory: 184136kb
input:
200000 yagcbqaxecsgdijvsdsqlemrrhyyuysvlbkkgultlmrapzokempkzmyyvgabgtqifgqhwyudzbkbjncsuixvyffpvtuczjakknocyskvqaohfvxomjhzirykpdwisgkmyomejaxbzamrnfcxjwjouskvjfuriurzknmhfvpvbdqunfckdmxhndvffhuybezncgohzwxvwfscltbhwvgvbrrejtuixsdwetkdxlogepphsvhyzybisqixsledftgegzbslkqsalhoifysrxsbjxvpojjxkqqoumlkj...
output:
114514
result:
ok single line: '114514'
Test #10:
score: 0
Accepted
time: 512ms
memory: 183784kb
input:
200000 cbacabcbabcacbacabcacbabcbacabcbabcacbabcbacabcacbacabcbabcacbacabcacbabcbacabcacbacabcbabcacbabcbacabcbabcacbacabcacbabcbacabcbabcacbabcbacabcacbacabcbabcacbabcbacabcbabcacbacabcacbabcbacabcacbacabcbabcacbacabcacbabcbacabcbabcacbabcbacabcacbacabcbabcacbacabcacbabcbacabcacbacabcbabcacbabcbaca...
output:
0
result:
ok single line: '0'
Test #11:
score: 0
Accepted
time: 213ms
memory: 184016kb
input:
200000 bbababbabbababbababbabbababbabbababbababbabbababbababbabbababbabbababbababbabbababbababbabbababbabbababbababbabbababbabbababbababbabbababbababbabbababbabbababbababbabbababbabbababbababbabbababbababbabbababbabbababbababbabbababbababbabbababbabbababbababbabbababbabbababbababbabbababbababbabbaba...
output:
150050
result:
ok single line: '150050'
Test #12:
score: 0
Accepted
time: 227ms
memory: 181980kb
input:
200000 babababbaaaaaaabbbaaabbbbaaabbaaaabbaababbabbbbbababbbabbbaaabaaabaabbaabbaabbabbaaaaaababbbbbbaaaabaababbabaaabbbbbbabaaaabaaabaaaababaaabbbbbbaaabaabaaabbabbbabbbabaaaabbbbbbabbaabbaaaaaaaaabababbabbbbbaabaabaaabababbaabbbbbbabbaaaaababaabbaaaabbbbaaababbabbaabaabaabaaabbabaaaaabbabababbbaa...
output:
131072
result:
ok single line: '131072'
Test #13:
score: 0
Accepted
time: 452ms
memory: 183328kb
input:
200000 rhqtphowacdrsxqisrqjhcsmxvwqtbmsvawxxxujgibnowkeyzhnjihsvsuklueukevgvlfqnrhalhglqlknerjwzizhxxszwjtnroubsjdhbnekbolwxaigyeypumuncdhmqqeljoyewehkhsqfoirchdnwazypwtefdyvtockpluejsftmffgbgdcotjnnkimawpflzwurdwrmpeudobzhpoyktufkgyvxpbfhuzswkrmnfzultfxdefoffvrfmuoufylyfvexnxgwgqfbiqvwpyenoqxncisuz...
output:
8
result:
ok single line: '8'
Test #14:
score: 0
Accepted
time: 19ms
memory: 120748kb
input:
10 aaabaabaaa aabbbaaaba
output:
6
result:
ok single line: '6'
Test #15:
score: 0
Accepted
time: 23ms
memory: 119068kb
input:
17 ababbbaabbbaaaaba abbabbbbbaabaabba
output:
10
result:
ok single line: '10'
Test #16:
score: 0
Accepted
time: 21ms
memory: 119244kb
input:
22 aaabbbaaaababbabbbbbbb bbaabbbbbaaabbbabaaaaa
output:
8
result:
ok single line: '8'
Test #17:
score: 0
Accepted
time: 16ms
memory: 119408kb
input:
15 abaabaaaaabbbab abbbabbbabababa
output:
8
result:
ok single line: '8'
Test #18:
score: 0
Accepted
time: 16ms
memory: 120636kb
input:
5 aabba baaba
output:
6
result:
ok single line: '6'
Test #19:
score: 0
Accepted
time: 16ms
memory: 120200kb
input:
1 a a
output:
2
result:
ok single line: '2'
Test #20:
score: 0
Accepted
time: 16ms
memory: 120220kb
input:
100 baabaaaabaabaabaaabaabbbabbbbbaababbabbbabbbbababaaabbabbaaaabbbaaabaabbbaaaaaababaababbbbbaaabbaabb abbbbbbbbaaaaababbabbabbbbbaaaabbabbbaaaaabababbbabbababbbabbaabbabbabbbaaababbabbabbabbbabbbababaaa
output:
20
result:
ok single line: '20'
Test #21:
score: 0
Accepted
time: 12ms
memory: 120612kb
input:
32 babbbbbabbababaabbbaaaaabbaababa abbbaaababaabababbaaabaaabbaaaab
output:
18
result:
ok single line: '18'
Extra Test:
score: 0
Extra Test Passed