QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#557641#8668. 回文路径zhulexuan0 8ms11976kbC++143.6kb2024-09-11 10:40:222024-09-11 10:40:22

Judging History

你现在查看的是最新测评结果

  • [2024-09-11 10:40:22]
  • 评测
  • 测评结果:0
  • 用时:8ms
  • 内存:11976kb
  • [2024-09-11 10:40:22]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define inf INT_MAX
#define fr(i,l,r) for (i=(l); i<=(r); i++)
#define rfr(i,l,r) for (i=(l); i>=(r); i--)
template<typename T>inline void read(T &n){
    T w=1; n=0; char ch=getchar();
    while (!isdigit(ch) && ch!=EOF){ if (ch=='-') w=-1; ch=getchar(); }
    while (isdigit(ch) && ch!=EOF){ n=(n<<3)+(n<<1)+(ch&15); ch=getchar(); }
    n*=w;
}
template<typename T>inline void write(T x){
    if (x==0){ putchar('0'); return ; }
    T tmp;
    if (x>0) tmp=x;
    else tmp=-x;
    if (x<0) putchar('-');
    char F[105];
    long long cnt=0;
    while (tmp){
        F[++cnt]=tmp%10+48;
        tmp/=10;
    }
    while (cnt) putchar(F[cnt--]);
}
#define Min(x,y) x = min(x,y)
#define Max(x,y) x = max(x,y)
//基础配置=================================================================================
const ll N = 200005, p = 629883559, bs = 487174091;
ll n;
char c1[N],c2[N];
ll f[N],g[N],len[N];
ll fv[N],gv[N],pw[N];
void manacher(char ch[]){
    ll i,j;
    char c[N];
    memset(len,0,sizeof(len));
    fr(i,1,n+n+1){
        if (i%2==0) c[i] = ch[i/2];
        else c[i] = '#';
    }
    len[1] = 1; ll pos = 1;
    fr(i,2,n+n+1){
        ll st;
        if (pos+len[pos]-1<i) st = i;
        else {
            st = i+len[pos+pos-i]-1;
            Min(st,pos+len[pos]-1);
        }
        while (st+1<=n+n+1 && i+i-st-1>=1 && c[st+1]==c[i+i-st-1]) st++;
        len[i] = st-i+1;
        if (i+len[i]>pos+len[pos]) pos = i;
    }
}
void hash_init(){
    ll i;
    fr(i,1,n) fv[i] = ( fv[i-1]*bs+c1[i] ) %p;
    rfr(i,n,1) gv[i] = ( gv[i+1]*bs+c2[i] ) %p;
    pw[0] = 1; fr(i,1,n) pw[i] = pw[i-1]*bs%p;
}
ll calc1(ll l,ll r){
    return ( fv[r] - fv[l-1]*pw[r-l+1]%p + p ) %p;
}
ll calc2(ll l,ll r){
    return ( gv[l] - gv[r+1]*pw[r-l+1]%p + p ) %p;
}
ll qry(ll x,ll y){
    // printf("\n qry %lld %lld\n",x,y);
    if (c1[x]!=c2[y]) return 0;
    ll l = 1, r = min(x,n-y+1);
    // printf("l = %lld , r = %lld\n",l,r);
    while (l<=r){
        ll mid = (l+r)>>1;
        // printf("calc1 %lld ~ %lld = %lld\n",x-mid+1,x,calc1(x-mid+1,x));
        // printf("calc2 %lld ~ %lld = %lld\n",y,y+mid-1,calc1(y,y+mid-1));
        if (calc1(x-mid+1,x)==calc2(y,y+mid-1)) l = mid+1;
        else r = mid-1;
    }
    return r;
}
ll solve1(ll x,ll y){
    ll lf,rh;
    if (x==y){
        lf = ( (2*x) - f[2*x] ) /2 + 1;
        rh = ( (2*x) - f[2*x] ) /2 - 1;
    }
    else {
        lf = ( (2*x+1) - f[2*x+1] ) /2 + 1;
        rh = ( (2*x+1) + f[2*x+1] ) /2 - 1;
    }
    return rh-lf+1 + 2*qry(lf-1,rh);
}
ll solve2(ll x,ll y){
    ll lf,rh;
    if (x==y){
        lf = ( (2*x) - g[2*x] ) /2 + 1;
        rh = ( (2*x) - g[2*x] ) /2 - 1;
    }
    else {
        lf = ( (2*x+1) - g[2*x+1] ) /2 + 1;
        rh = ( (2*x+1) + g[2*x+1] ) /2 - 1;
    }
    return rh-lf+1 + 2*qry(lf,rh+1);
}
int main(){
	// freopen("a.in","r",stdin);
//	freopen(".out","w",stdout);
    ll i,j;
    read(n);
    scanf("%s",c1+1);
    scanf("%s",c2+1);
    manacher(c1); fr(i,1,n+n+1) f[i] = len[i];
    manacher(c2); fr(i,1,n+n+1) g[i] = len[i];
    // fr(i,1,n+n+1) printf("%lld : f = %lld , g = %lld\n",i,f[i],g[i]);
    hash_init();
    ll ans = 0;
    fr(i,1,n){
        Max(ans,2*qry(i,i));
        Max(ans,solve1(i,i));
        if (i<n && c1[i]==c1[i+1]) Max(ans,solve1(i,i+1));
        Max(ans,solve2(i,i));
        if (i<n && c2[i]==c2[i+1]) Max(ans,solve2(i,i+1));
    }
    write(ans);
    return 0;
}
//g++ a.cpp -o a -Wall -Wl,--stack=512000000 -std=c++11 -O2

詳細信息

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 30
Accepted
time: 0ms
memory: 7976kb

input:

1000
mfmlkrasfiudkzrjzyrlbpitvzfrlmjdzsurtdcmnqpsyfgobbstvplqylvciloomaljyssxtrrccywyirfvlcnchwkfwbdaoxzpfpvlruptganojnfxmnlqetptmlmoyluxvaipghtlaszoozscdmjomobyzboqqmvqjpbfjsoczhkwrlcauzjceqikbaeuiqahnldpqmohfjfzgkbfdbqnoxispkejvpncwsyelebqumapgbfdrjvuaxaphnkciwzkruijmanwslkwosgyfhwbnsthhtxrhrzlgtt...

output:

6

result:

ok single line: '6'

Test #2:

score: 0
Wrong Answer
time: 1ms
memory: 8112kb

input:

1000
wvitzoxwlmhexjuqvoxksetoupgkhattucdzfevqorkdlsymjuvhobdrjsodtipwpfhipsdnyvqtsbbasrrvyybijzmpwseckztnpnkqswgkaeivflhwevhxcchjsnelqcixexkntwiuolsditpdwypgerzijziyrgqkwuucnqaehuwkpyrmwewjitvsaebyytznbtnkulnepceeloyjpfhcdpqfqhvzsmkcynjwztmkbnqaxnikfuiutocahdfbvsgdskgwqmzizzjlbqxnngftdohetabpjzpqzyc...

output:

4

result:

wrong answer 1st lines differ - expected: '7', found: '4'

Subtask #2:

score: 0
Wrong Answer

Test #11:

score: 0
Wrong Answer
time: 8ms
memory: 11976kb

input:

100000
ibhqhodaqcwqggmckoemulhkgbfidceskhefhsonccepfodalabaqgobpgcnaervbccadkbtsdigsoqochklocgbjjqcdhwrlacamprsoilyhiwkkjalicedhbxajrkhjgivjhnfdibkdwtexnnriegejazmohlfijbeigfmpngncokxhifjfuwuogccdfglfbxobnarmgfhgpnjjewicgfhcmfbbnjbbjjtbprnagpchcihcihfhcikeaecefdgeegtzlfdolhfieahiehdfcaflmndmcojceblf...

output:

8

result:

wrong answer 1st lines differ - expected: '9', found: '8'

Subtask #3:

score: 0
Skipped

Dependency #1:

0%