QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#630442#7780. Dark LaTeX vs. Light LaTeXAkoasm_XML 0ms0kbC++202.8kb2024-10-11 18:33:392024-10-11 18:33:50

Judging History

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

  • [2024-11-25 20:53:52]
  • hack成功,自动添加数据
  • (/hack/1258)
  • [2024-10-11 18:33:50]
  • 评测
  • 测评结果:ML
  • 用时:0ms
  • 内存:0kb
  • [2024-10-11 18:33:39]
  • 提交

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][maxn],s[maxn][maxn];
char A[maxn],B[maxn];
int sz[maxn * maxn];
unordered_map<char,int> son[maxn*maxn];

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

void build(){
    for(int i=1;i<=m;i++){
        int now = 0;
        for(int j=0;i+j<=m;j++){
            int &t = son[now][B[i+j]];
            if(!t) t = ++cnt;
            sz[t]++;
            // cout<<son[now][B[i+j]]<<endl;
            now = t;
        }
    }
}

void getlcp(char s[],int id){
    // int N = (int)s.length();
    int N = strlen(s);
    // vector<int> z(n);
    for (int i = 1, l = 0, r = 0; i < N; ++i) {
        if (i <= r && z[id][i - l + id] < r - i + 1) {
            z[id][i + id] = z[id][i - l + id];
        } 
        else {
            z[id][i + id] = max(0, r - i + 1);
            while (i + z[id][i + id] < n && s[z[id][i + id]] == s[i + z[id][i + id]]) ++z[id][i + id];
        }
        if (i + z[id][i + id] - 1 > r) l = i, r = i + z[id][i + id] - 1;
        // cout<<id<<" "<<i + id<<" "<<z[id][i + id]<<endl;
    }
}

void bf(){
    for(int i=1;i<=n;i++){
        for(int j=i+2;j<=n;j++){
            int t = z[i][j];
            t = min(t,j - 1 - i);
            s[i+1][j-1]++;
            s[i+t+1][j-1]--;
        }
    }
    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++){
        int now = 0;
        for(int j=i;j<=n;j++){
            int t = son[now][A[j]];
            if(!t) break;
            now = t;
            Ans += s[i][j] * sz[now];
        }
    }
}

void BF(){
    for(int i=1;i<=n;i++){
        int now = 0;
        for(int j=i;j<=n;j++){
            int t = son[now][A[j]];
            if(!t) break;
            now = t;
            Ans += sz[now];
        }
    }
}

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

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

详细

Test #1:

score: 0
Memory Limit Exceeded

input:

abab
ab

output:

8

result: