QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#591529#7780. Dark LaTeX vs. Light LaTeXsolar_express#RE 8ms101500kbC++233.5kb2024-09-26 16:18:412024-09-26 16:18:41

Judging History

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

  • [2024-11-25 20:53:52]
  • hack成功,自动添加数据
  • (/hack/1258)
  • [2024-09-26 16:18:41]
  • 评测
  • 测评结果:RE
  • 用时:8ms
  • 内存:101500kb
  • [2024-09-26 16:18:41]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
string s, t;
template<uint32_t base, uint32_t mod>
struct str_hash {
  vector<uint32_t> pw, _H;
  str_hash() = default;
  str_hash(const string &s) : pw(s.size()), _H(s.size()) {
    assert(!s.empty());
    pw[0] = 1;
    _H[1] = s[0];
    for (size_t i = 1; i < s.size(); ++i) {
      pw[i] = 1ll*pw[i-1]*base%mod;
      _H[i+1] = (1ll*_H[i]*base+s[i])%mod;
    }
  }
  uint32_t operator()(int l, int r) {
    // --l, --r;
    // if (!l) return _H[r];
    return (_H[r]-1ll*_H[l-1]*pw[r-l+1]%mod+mod)%mod;
  }
};

struct HH {
  str_hash<11451, 2021165639> A;
  str_hash<11451, 1000000007> B;
  HH(const string &s) : A(s), B(s) {};
  unsigned long operator()(int l, int r) {
    return long(A(l,r))<<32|B(l,r);
  }
};

const int N = 5005;
int lcp[N];
#include <bits/extc++.h>
using namespace __gnu_pbds;
gp_hash_table<long, int> cnt;
int c[N][N];

int main() {
  // freopen("D.in","r",stdin);
  // freopen("D.out","w",stdout);
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> s >> t;
  HH S(s), T(t);
  int64_t ans = 0;
  for (int i = 1; i <= t.size(); ++i) for (int j=i;j<=t.size();++j) ++cnt[T(i,j)];

//   for (int i = 1; i <= s.size(); ++i) for (int j=i;j<=s.size();++j) ans+=cnt[S(i,j)];
  for (int l = 1; l <= s.size(); ++l) {
    int n = s.size()-l+1;
    int mx=1, pt=1; lcp[l]=n;
    for (int i = 2; i <= n; ++i) {
      if (i<=mx) lcp[i+l-1]=min(lcp[i-pt+l],mx-i+1);
    //   cerr<<i<<" "<<mx<<" "<<i+lcp[i+l-1]<<" "<<s[i+lcp[i+l-1]+l-1]<<" "<<s[l+lcp[i+l-1]]<<endl;
      while(i+lcp[i+l-1]<=n && s[i+lcp[i+l-1]+l-2]==s[l+lcp[i+l-1]-1]) ++lcp[i+l-1];
      if(i+lcp[i+l-1]-1>mx) pt=i, mx=i+lcp[i+l-1]-1;
    }
    // ++c[l][l];
    ++c[l][(int)s.size()];
    --c[l+1][(int)s.size()];
    for (int r = l+1; r<=s.size(); ++r) {
      ++c[l][r-1];
      --c[l+1][r-1];
      if(lcp[r]>0){
        // cerr<<l<<" "<<r<<" "<<lcp[r]<<endl;
        int rm=min(r-1,l+lcp[r]);
        if(rm>=l+1){
          ++c[l+1][r-1];
          --c[rm+1][r-1];
        }
      }
    }
    memset(lcp,0,sizeof(lcp));
    // cerr<<endl;
  }
  for (int r = 1; r <= s.size(); ++r) {
    long C = 0;
    for (int l = 1; l <= r; ++l) {
      C += c[l][r];
    //   cerr<<l<<" "<<r<<" "<<C<<" "<<cnt[S(l,r)]<<'\n';
    //   cerr<<ans<<endl;
      ans += C * cnt[S(l,r)];
    //   cerr<<ans<<endl;
    }
  }
//   cerr<<ans<<endl;
  memset(c, 0, sizeof(c));
  cnt.clear();
  swap(s, t);
  swap(S, T);
  for (int i = 1; i <= t.size(); ++i) for (int j=i;j<=t.size();++j) ++cnt[T(i,j)];
  for (int l = 1; l <= s.size(); ++l) {
    int n = s.size()-l+1;
    int mx=1, pt=1; lcp[l]=n;
    for (int i = 2; i <= n; ++i) {
      if (i<=mx) lcp[i+l-1]=min(lcp[i-pt+l],mx-i+1);
    //   cerr<<i<<" "<<mx<<" "<<i+lcp[i+l-1]<<" "<<s[i+lcp[i+l-1]+l-1]<<" "<<s[l+lcp[i+l-1]]<<endl;
      while(i+lcp[i+l-1]<=n && s[i+lcp[i+l-1]+l-2]==s[l+lcp[i+l-1]-1]) ++lcp[i+l-1];
      if(i+lcp[i+l-1]-1>mx) pt=i, mx=i+lcp[i+l-1]-1;
    }
    for (int r = l+1; r<=s.size(); ++r) {
    //   cerr<<l<<" "<<r<<" "<<lcp[r]<<'\n';
      if(lcp[r]>0){
        int rm=min(r-1,l+lcp[r]);
        if(rm>=l+1){
          ++c[l+1][r-1];
          --c[rm+1][r-1];
        }
      }
    }
    memset(lcp,0,sizeof(lcp));
    // cerr<<endl;
  }
  for (int r = 1; r <= s.size(); ++r) {
    long C = 0;
    for (int l = 1; l <= r; ++l) {
      C += c[l][r];
      if(C)ans += C * cnt[S(l,r)];
    //   cerr<<l<<" "<<r<<" "<<C<<" "<<cnt[S(l,r)]<<'\n';
    }
  }
  cout << ans << '\n';
  return 0;
}
/*
abab
abaaab
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 8ms
memory: 101500kb

input:

abab
ab

output:

8

result:

ok 1 number(s): "8"

Test #2:

score: -100
Runtime Error

input:

abab
abaaab

output:


result: