题解:【ARC227】C - Follow the Letters
题解:ARC227 - C
题面:Follow the Letters。
解题思路
这道题应该比较适合先猜后证,反正我是这样做的。
题面中给了一个关键信息:
-
总存在一个长度不超过
10^6 的操作序列,使操作结束后有人岛屿的数量等于K 。 -
数据范围:
1 \leq N \leq 1000 。
于是很容易猜到操作序列的长度为
题目中求的
反过来想,一定存在不管怎么处理最终不会处在同一个岛屿上的人。
当两个岛屿位置
因为:
-
答案和
S 中的最小周期有关,而最小周期最大时就是S 本身; -
每执行一次
S 作为操作序列,一定会让同一周期中的一部分人合并到一个岛上; -
一共有
N 个人,只要有S_x \neq S_y 这种情况,那么每次S 作为操作都会至少让一个人到另一个人所处岛屿上。
所以操作序列
时间复杂度为
代码实现
#include <iostream>
#include <vector>
#include <algorithm>
#include <climits>
#include <atcoder/all>
#define endl '\n'
using namespace std;
typedef long long ll;
int main(int argc, char* argv[]) {
// freopen(".in", "r", stdin);
// freopen(".out", "w", stdout);
ios_base::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
int n; string s;
cin >> n >> s;
int k; string x;
for (int i = 1; i <= n; i++) { // 枚举 i 作为周期长度
if (n % i != 0) {
continue;
}
bool fg = true;
for (int j = 0; j < n; j++) { // 判断是否存在周期长度 i
if (s[j] != s[(i + j) % n]) {
fg = false;
break;
}
}
if (fg) {
k = n / i; // 最小周期个数
break;
}
}
cout << k << endl;
cout << n * n << endl; // 操作序列长度
for (int i = 0; i < n; i++) {
x += s; // 操作序列
}
cout << x << endl;
return 0;
}