QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#241059#6567. Repetitive String InventionMovingUp#WA 106ms6956kbC++172.0kb2023-11-05 22:31:302023-11-05 22:31:30

Judging History

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

  • [2023-11-05 22:31:30]
  • 评测
  • 测评结果:WA
  • 用时:106ms
  • 内存:6956kb
  • [2023-11-05 22:31:30]
  • 提交

answer

#include <algorithm>
#include <iostream>
#include <unordered_map>
#include <vector>

using namespace std;

int fastPow(int x, int pw, int mod) {
  if (pw == 0)
    return 1;
  else if (pw % 2 == 0) {
    int tmp = fastPow(x, pw / 2, mod);
    return ((int64_t)tmp * tmp) % mod;
  } else
    return ((int64_t)x * fastPow(x, pw - 1, mod)) % mod;
}

struct rolling_hash {
  static const int BASE = 43;
  static const int MOD = 1e9 + 7;

  vector<int> multInvs;
  vector<int> sums;

  rolling_hash(const std::string &s) {
    int n = s.size();

    int currPw = 1;
    sums.resize(n);
    for (int i = 0; i < n; i++) {
      sums[i] = ((int64_t)(s[i] - 'a' + 1) * currPw) % MOD;
      if (i != 0)
        sums[i] = (sums[i] + sums[i - 1]) % MOD;

      multInvs.push_back(fastPow(currPw, MOD - 2, MOD));
      currPw = ((int64_t)currPw * BASE) % MOD;
    }
  }

  int getSubhash(int l, int r) {
    int h = sums[r] - (l == 0 ? 0 : sums[l - 1]);
    if (h < 0)
      h += MOD;
    return ((int64_t)h * multInvs[l]) % MOD;
  }
};

int64_t count(const string &s, bool countZeroGreen) {
  rolling_hash rh = rolling_hash(s);

  int n = s.size();
  int64_t ans = 0;
  unordered_map<int, int> cntWithHash;
  for (int lGreen = 0; lGreen < n; lGreen++) {
    for (int lRed = lGreen; lRed < n; lRed++) {
      for (int rRed = lRed; rRed < n; rRed++) {
        int lenGreen = lRed - lGreen;
        if (lenGreen != 0 && rRed + lenGreen < n &&
            rh.getSubhash(lGreen, lRed - 1) !=
                rh.getSubhash(rRed + 1, rRed + lenGreen))
          continue;

        if (rRed + lenGreen >= n)
          continue;

        if (lenGreen == 0 && !countZeroGreen)
          continue;

        ans += cntWithHash[rh.getSubhash(lRed, rRed)];
      }
    }

    for (int i = 0; i <= lGreen; i++) {
      cntWithHash[rh.getSubhash(i, lGreen)]++;
    }
  }

  return ans;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  string s;
  cin >> s;

  int64_t ans = count(s, true);

  reverse(s.begin(), s.end());
  ans += count(s, false);

  cout << ans << endl;
}

详细

Test #1:

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

input:

aaaa

output:

9

result:

ok single line: '9'

Test #2:

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

input:

axabxbcxcdxd

output:

22

result:

ok single line: '22'

Test #3:

score: 0
Accepted
time: 106ms
memory: 3616kb

input:

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...

output:

536006700

result:

ok single line: '536006700'

Test #4:

score: 0
Accepted
time: 81ms
memory: 3640kb

input:

abababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababab...

output:

136016600

result:

ok single line: '136016600'

Test #5:

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

input:

a

output:

0

result:

ok single line: '0'

Test #6:

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

input:

ab

output:

0

result:

ok single line: '0'

Test #7:

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

input:

aa

output:

1

result:

ok single line: '1'

Test #8:

score: 0
Accepted
time: 65ms
memory: 6424kb

input:

bbbbbrrrrbrbrrbrbrbrbbrrbbbbrrbrrbrbrbrrrrrbbbrrbrrbbrrrbbbbbrrrrbbrbbrrrrrbbrrrbbbbbrbbrbbbrbrrbrrrrrrrrbbrbrbbrrrrrrbbbbbrbrrrrbrrbbrbrrrrrbrbrbbrbrrbrrrrbrbbrbrrbrrbbbbrrrrrbrbbrbbrrrrrbrbrbrbbbrrrrrrrbbbbbbrrbrbrrrrrbbbrbrrrrbbbbrrbrrbbbbbbbrbbbbrrrbrbbbrrrrrrbbbbbrbrrbbbbrbrbrrbbrrrbbbrbbbrbbrr...

output:

209015

result:

ok single line: '209015'

Test #9:

score: 0
Accepted
time: 51ms
memory: 6536kb

input:

fkrryithikkpurfezufbthxxrtejxpprhrhurrfziigzgajipayrefarpxuggehkiggeppfjghuyejuagkjpiezhmfizgphkkfffifihrimuhgtgzykfupbprjyrbpbimapjmbkrhubzhgfhhpkrxgjgmkheiiugiyerggyukyfuuxgrehgayafzuxymxxabygpapbjxipkxgbxhzpekypfghaumukguaipphiyhzimzfmpmzahbmaagytygyekzhktbbheetxrbipgytppgbhkpimrybkbeirhrzytfehba...

output:

5696

result:

ok single line: '5696'

Test #10:

score: -100
Wrong Answer
time: 67ms
memory: 6956kb

input:

ayeaecauuueerrcsyyyuayauarecyrruueeucacrrcesercyeasuscucscruaaresaeruurreeursssuyraeysurecacueuacycacsyuyeyusssassaasyuarrcecueayacucuyyrsasrycyrrcececyuccecyreyeccuesaasceasussaraeruyarcauarcecacrucryryseysauaucyaeyurueurerrccrayyyycccsuaaesrecurucerrascyacucucryarycayeesyysusserarseyrauyrryeacauec...

output:

18253

result:

wrong answer 1st lines differ - expected: '18249', found: '18253'