QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#630559#7780. Dark LaTeX vs. Light LaTeXAkoasm_XWA 0ms4076kbC++203.5kb2024-10-11 19:13:552024-10-11 19:14:01

Judging History

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

  • [2024-11-25 20:53:52]
  • hack成功,自动添加数据
  • (/hack/1258)
  • [2024-10-11 19:14:01]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:4076kb
  • [2024-10-11 19:13:55]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
typedef long long LL;

inline int read(){
	int x = 0 , f = 1 ; char c = getchar() ;
    while( c < '0' || c > '9' ) { if( c == '-' ) f = -1 ; c = getchar() ; } 
    while( c >= '0' && c <= '9' ) { x = x * 10 + c - '0' ; c = getchar() ; } 
    return x * f ;
}

const int maxn = 5020;
LL Ans;
int n,m,cnt;
int z[maxn],s[maxn][maxn],S[maxn];
int nxt[maxn];
char A[maxn],B[maxn];

void Clear(){
    for(int i=0;i<=n;i++)
        for(int j=0;j<=n;j++)
            s[i][j] = 0;
    cnt = 0;
}


void getlcp(char ss[],int id){
    int N = strlen(ss);
    for(int i=0;i<=n;i++) z[i] = 0;
    for (int i = 1, l = 0, r = 0; i < N; ++i) {
        if (i <= r && z[i - l + id] < r - i + 1) {
            z[i + id] = z[i - l + id];
        } 
        else {
            z[i + id] = max(0, r - i + 1);
            while (i + z[i + id] < n && ss[z[i + id]] == ss[i + z[i + id]]) ++z[i + id];
        }
        if (i + z[i + id] - 1 > r) l = i, r = i + z[i + id] - 1;
    }
    int i = id;
    for(int j=i+2;j<=n;j++){
        int t = z[j];
        t = min(t,j - 1 - i);
        s[i+1][j-1]++;
        s[i+t+1][j-1]--;
    }
}

void bf(char ss[],int id){
    int pos = -1;
    int N = strlen(ss);
    for(int i=0;i < N;i++) nxt[i] = -1;
    for(int i = 1;i < N;i++){
        while(~pos && ss[pos+1] != ss[i]) pos = nxt[pos];
        if(ss[pos+1]==ss[i]) pos++;
        nxt[i] = pos;
    }
    pos = -1;
    for(int i=0;i <= n;i++) S[i] = 0;
    for(int i=1;i<=m;i++){
        while(~pos && ss[pos+1] != B[i]){
            pos = nxt[pos];
            if(~pos){
                S[id]++;
                S[id+pos]--;
            }
        }
        if(ss[pos+1]==B[i]) pos++;
        if(~pos){
            S[id+pos]++;
            S[id+pos+1]--;
        }
    }
    for(int i=1;i<=n;i++) S[i] += S[i-1];
    for(int i=id;i<=n;i++){
        Ans += S[i] * s[id][i];
    }
}

void BF(char ss[],int id){
    int pos = -1;
    int N = strlen(ss);
    for(int i=0;i < N;i++) nxt[i] = -1;
    for(int i = 1;i < N;i++){
        while(~pos && ss[pos+1] != ss[i]) pos = nxt[pos];
        if(ss[pos+1]==ss[i]) pos++;
        nxt[i] = pos;
    }
    pos = -1;
    for(int i=0;i <= n;i++) S[i] = 0;
    for(int i=1;i<=m;i++){
        while(~pos && ss[pos+1] != B[i]){
            pos = nxt[pos];
            if(~pos){
                S[id]++;
                S[id+pos+1]--;
            }
        }
        if(ss[pos+1]==B[i]) pos++;
        if(~pos){
            S[id+pos]++;
            S[id+pos+1]--;
        }
    }
    for(int i=1;i<=n;i++) S[i] += S[i-1];
    for(int i=id;i<=n;i++){
        Ans += S[i];
    }
    // cout<<Ans<<endl;
}

void solve(){
    scanf("%s",A+1);n = strlen(A+1);
    scanf("%s",B+1);m = strlen(B+1);
    for(int i=1;i<=n;i++) getlcp(A+i,i);
    for(int j=1;j<=n;j++){
        for(int i=1;i<=n;i++){
            s[i][j] += s[i-1][j];
        }
    }
    for(int i=1;i<=n;i++) bf(A+i,i);
    Clear();
    swap(A,B);
    swap(n,m);
    // build();
    for(int i=1;i<=n;i++) getlcp(A+i,i);
    for(int j=1;j<=n;j++){
        for(int i=1;i<=n;i++){
            s[i][j] += s[i-1][j];
        }
    } 
    for(int i=1;i<=n;i++) bf(A+i,i);
    for(int i=1;i<=n;i++) BF(A+i,i);
    printf("%lld\n",Ans);
}

int main(){
	// freopen("1.txt","r",stdin);
	int T = 1;
	// T = read();
	while(T--) solve();
	return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 3804kb

input:

abab
ab

output:

8

result:

ok 1 number(s): "8"

Test #2:

score: 0
Accepted
time: 0ms
memory: 3952kb

input:

abab
abaaab

output:

29

result:

ok 1 number(s): "29"

Test #3:

score: 0
Accepted
time: 0ms
memory: 3940kb

input:

abcd
abcde

output:

10

result:

ok 1 number(s): "10"

Test #4:

score: 0
Accepted
time: 0ms
memory: 3940kb

input:

aaba
ba

output:

6

result:

ok 1 number(s): "6"

Test #5:

score: -100
Wrong Answer
time: 0ms
memory: 4076kb

input:

babababaaabbaabababbbaabbbababbaaaaa
aaaabbaababbab

output:

981

result:

wrong answer 1st numbers differ - expected: '1161', found: '981'