【求助】请问为什么会WA呢?
查看原帖
【求助】请问为什么会WA呢?
121479
聪明的猪楼主2020/12/27 17:55

RT,萌新奔溃ing

#include <cstring>

#include <iostream>
#include <string>

using namespace std;

const int kMaxN(1000000);

int kNext[kMaxN + 5];

void Kmp(const string&, int);
void GetFail(const string&, int);

int main(void) {
  int n(0), case_cnt(1);
  string s;
  ios::sync_with_stdio(false);

  while (cin >> n && n) {
    memset(kNext, 0, sizeof(kNext));
    cin >> s;

    if (case_cnt != 1)
      cout << endl;

    cout << "Test case #" << case_cnt++ << endl;
    Kmp(s, n);
  }

  return 0;
}

void Kmp(const string& str, int n) {
  GetFail(str, n);

  for (int i = 2; i < n + 1; ++i) {
    int len(i - kNext[i]);

    if (kNext[i] && !(i % len))
      cout << i << ' ' << i / len << endl;
  }
}

void GetFail(const string& str, int str_len) {
  for (int i = 1; i < str_len; ++i) {
    int j(kNext[i]);

    while (j && str[i] != str[j])
      j = kNext[j];

    kNext[i + 1] = str[i] == str[j] ? j + 1 : 0;
  }
}
2020/12/27 17:55
加载中...