From 59821f2755b7b892037e492e45fd46ac60c7839a Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Tue, 12 Apr 2022 13:32:39 +0800 Subject: [PATCH] =?UTF-8?q?141.=20=E5=91=A8=E6=9C=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://www.acwing.com/problem/content/submission/code_detail/13336130/ --- AcWing/141/141.cpp | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 AcWing/141/141.cpp diff --git a/AcWing/141/141.cpp b/AcWing/141/141.cpp new file mode 100644 index 00000000..c29f84c9 --- /dev/null +++ b/AcWing/141/141.cpp @@ -0,0 +1,41 @@ +#include +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int N = 1e6 + 5; + +int t, n, next[N]; +std::string s; + +int main() { + std::ios::sync_with_stdio(false); + + while (cin >> n, n) { + cout << "Test case #" << ++t << endl; + + cin >> s; + + s = ' ' + s; + + next[1] = 0; + for (int i = 2, j = 0; i <= n; i++) { + while (j && s[i] != s[j + 1]) j = next[j]; + if (s[i] == s[j + 1]) j++; + next[i] = j; + } + + for (int i = 2; i <= n; i++) { + int len = i - next[i]; + if (i % len == 0 && i / len > 1) { + cout << i << ' ' << i / len << endl; + } + } + + cout << endl; + } + + return 0; +}