D. Diane(构造字符串)
You are given an integer n. Find any string ss of length n consisting only of English lowercase letters such that each non-empty substring of s occurs in s an odd number of times. If there are multiple such strings, output any. It can be shown that such string always exists under the given constraints.
A string a is a substring of a string b if aa can be obtained from b by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.
The first line contains a single integer tt (1≤t≤500) — the number of test cases.
The first line of each test case contains a single integer nn (1≤n≤10^5).
It is guaranteed that the sum of nn over all test cases doesn't exceed 3⋅10^5
For each test case, print a single line containing the string ss. If there are multiple such strings, output any. It can be shown that such string always exists under the given constraints.
这个题就是你要构造一个字符串,使得每一个字串出现的次数为奇数
#include<bits/stdc++.h> using namespace std; int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); int t; cin >> t; while (t--) { int n; cin >> n; if (n == 1) { cout << "a\n"; } else { cout << string(n / 2, 'a') + (n & 1 ? "bc" : "b") + string(n / 2 - 1, 'a') << '\n'; } } return 0; }
这个 checker:
#include<bits/stdc++.h> #include "testlib.h" using namespace std; // len -> largest string length of the corresponding endpos-equivalent class // link -> longest suffix that is another endpos-equivalent class. // firstpos -> 1 indexed end position of the first occurrence of the largest string of that node // minlen(v) -> smallest string of node v = len(link(v)) + 1 // terminal nodes -> store the suffixes struct SuffixAutomaton { struct node { int len, link, firstpos; map<char, int> nxt; }; int sz, last; vector<node> t; vector<int> terminal; vector<int> dp; vector<vector<int>> g; SuffixAutomaton() {} SuffixAutomaton(int n) { t.resize(2 * n); terminal.resize(2 * n, 0); dp.resize(2 * n, -1); sz = 1; last = 0; g.resize(2 * n); t[0].len = 0; t[0].link = -1; t[0].firstpos = 0; } void extend(char c) { int p = last; int cur = sz++; t[cur].len = t[last].len + 1; t[cur].firstpos = t[cur].len; p = last; while (p != -1 && !t[p].nxt.count(c)) { t[p].nxt[c] = cur; p = t[p].link; } if (p == -1) t[cur].link = 0; else { int q = t[p].nxt[c]; if (t[p].len + 1 == t[q].len) t[cur].link = q; else { int clone = sz++; t[clone] = t[q]; t[clone].len = t[p].len + 1; while (p != -1 && t[p].nxt[c] == q) { t[p].nxt[c] = clone; p = t[p].link; } t[q].link = t[cur].link = clone; } } last = cur; } void build_tree() { for (int i = 1; i < sz; i++) g[t[i].link].push_back(i); } void build(string &s) { for (auto x: s) { extend(x); terminal[last] = 1; } build_tree(); } int cnt(int i) { // number of times i-th node occurs in the string if (dp[i] != -1) return dp[i]; int ret = terminal[i]; for (auto &x: g[i]) ret += cnt(x); return dp[i] = ret; } }; pair<int, int> ok(string s) { int n = s.size(); SuffixAutomaton sa(n); sa.build(s); for (int i = 1; i < sa.sz; i++) { if (sa.cnt(i) % 2 == 0) { return {sa.t[i].firstpos - sa.t[i].len, sa.t[i].firstpos - 1}; } } return {-1, -1}; } int main(int argc, char* argv[]) { registerTestlibCmd(argc, argv); int t = inf.readInt(); inf.readEoln(); for (int test = 1; test <= t; test++) { setTestCase(test); int n = inf.readInt(); inf.readEoln(); string s = ouf.readToken(); if (s.size() != n) { quitf(_wa, "the length of s should be exactly %d", n); } for (int i = 0; i < n; i++) { if (!(s[i] >= 'a' and s[i] <= 'z')) { quitf(_wa, "s contains %c which is not an English lowercase character", s[i]); } } auto p = ok(s); if (p.first != -1) { quitf(_wa, "the substring s[%d, %d] (0-indexed) occurs even number of times in s :\"(", p.first, p.second); } } quitf(_ok, "you are the best problem solver ever UwU"); return 0; }