QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#735961#7780. Dark LaTeX vs. Light LaTeXSuhy#RE 0ms0kbC++173.3kb2024-11-11 23:11:382024-11-11 23:11:38

Judging History

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

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

answer

#include <bits/stdc++.h>
#define ll long long

using namespace std;

const int maxn = 5003;
const int b1 = 31;
const int b2 = 37;
const int p1 = 1000000037;
const int p2 = 1000000029;

int n, m;
int s[maxn], t[maxn];
int ps1[maxn], ps2[maxn], pt1[maxn], pt2[maxn];
unordered_map <int, int> mp1, mp2;
int pwb1[maxn], pwb2[maxn];
ll ans;

int mns(int x, int y, int p)
{
    return (x >= y) ? (x - y) : (x + p - y);
}

int f[maxn];

int wok(int *a, int n, int dlt)
{
    f[0] = -1;
    for(int i = 1; i < n; i ++)
    {
        int j = f[i - 1];
        while(j >= 0 && a[j + 1] != a[i])
            j = f[j];
        if(a[j + 1] == a[i])
            j ++;
        f[i] = j;
        while(j >= 0)
        {
            int r = i - j - 1;
            int l = j + 1;
            if(l <= r) 
            {
                int hsh1 = mns(ps1[r + dlt], 1ll * ps1[l - 1 + dlt] * pwb1[(r - l + 1)] % p1, p1);
                int hsh2 = mns(ps2[r + dlt], 1ll * ps2[l - 1 + dlt] * pwb2[(r - l + 1)] % p2, p2);
                ans += min(mp1[hsh1], mp2[hsh2]);
                //cout<<l + dlt<<' '<<r + dlt<<' '<<min(mp1[hsh1], mp2[hsh2])<<endl;
            }
            j = f[j];
        }
    }
}

void work()
{
    mp1.clear();
    mp2.clear();
    for(int i = 1; i <= m; i ++)
    {
        for(int j = 1; j <= i; j ++)
        {
            mp1[mns(pt1[i], 1ll * pt1[j - 1] * pwb1[(i - j + 1)] % p1, p1)] ++;
            mp2[mns(pt2[i], 1ll * pt2[j - 1] * pwb2[(i - j + 1)] % p2, p2)] ++;
            //cout<<"S hsh "<<i<<' '<<j<<' '<<mns(pt1[i], 1ll * pt1[j - 1] * pwb1[(i - j + 1)] % p1, p1)<<endl;
        }
    }
    for(int i = 0; i < n; i ++)
    {
        wok(s + i + 1, n - i, i + 1);
    }
}

signed main()
{
    char c = getchar();
    while('a' <= c && c <= 'z')
        s[++ n] = c - 'a' + 1, c = getchar();
    while(!('a' <= c && c <= 'z'))
        c = getchar();
    while('a' <= c && c <= 'z')
        t[++ m] = c - 'a' + 1, c = getchar();
    for(int i = 1; i <= n; i ++)
    {
        ps1[i] = (1ll * ps1[i - 1] * b1 % p1 + s[i]) % p1;
        ps2[i] = (1ll * ps2[i - 1] * b2 % p2 + s[i]) % p2;
    }
    for(int i = 1; i <= m; i ++)
    {
        pt1[i] = (1ll * pt1[i - 1] * b1 % p1 + t[i]) % p1;
        pt2[i] = (1ll * pt2[i - 1] * b2 % p2 + t[i]) % p2;
    }
    pwb1[0] = pwb2[0] = 1;
    for(int i = 1; i <= max(n, m); i ++)
    {
        pwb1[i] = 1ll * pwb1[i - 1] * b1 % p1;
        pwb2[i] = 1ll * pwb2[i - 1] * b2 % p2;
    }
    work();
    //cout<<ans<<endl;
    //6
    swap(n, m);
    for(int i = 1; i <= max(n, m); i ++)
    {
        swap(s[i], t[i]);
        swap(ps1[i], pt1[i]);
        swap(ps2[i], pt2[i]);
    }
    work();
    //cout<<ans<<endl;
    //12
    for(int i = 1; i <= n; i ++)
    {
        for(int j = 1; j <= i; j ++)
        {
            int hsh1 = mns(ps1[i], 1ll * ps1[j - 1] * pwb1[(i - j + 1)] % p1, p1);
            int hsh2 = mns(ps2[i], 1ll * ps2[j - 1] * pwb2[(i - j + 1)] % p2, p2);
            ans += min(mp1[hsh1], mp2[hsh2]);
            // for(int k = j; k <= i; k ++)
            //     putchar(s[k] + 'a' - 1);
            // putchar(' ');
            //cout<<j<<' '<<i<<' '<<min(mp1[hsh1], mp2[hsh2])<<endl;
        }
    }
    //30
    cout<<ans<<endl;
}

詳細信息

Test #1:

score: 0
Runtime Error

input:

abab
ab

output:


result: