其实就是找一个字符串,循环周期长度不是n的因数
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 2e5+5;
int h[100005];
int n, k;
int gcd(int a, int b) {
return b ? gcd(b, a % b) : a;
}
inline void solve(){
int n; cin >> n;
for (int i = 2; i <= n; i++) {
if (n % i) {
int cnt = 0;
for (int j = 0; j < n; j++) {
cout << (char)('a' + cnt);
cnt++;
cnt %= i;
}
cout << '\n';
return;
}
}
int cnt = 0;
for (int i = 1; i <= n; i++) {
cout << (char)('a' + cnt);
cnt++;
cnt %= n;
}
cout << '\n';
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
std::cout.tie(0);
int t = 1;
cin >> t;
while (t--)
solve();
return 0;
}
前缀和,后缀和还有特判
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 2e5+5;
int h[100005];
int n, k;
int gcd(int a, int b) {
return b ? gcd(b, a % b) : a;
}
inline void solve(){
int n; cin >> n;
string s; cin >> s;
vector<int>q(n);
vector<int>h(n);
if (n & 1) {
cout << -1 << '\n';
return ;
}
int cntl = 0, cntr = 0;
int cnt = 1;
int fg1 = 0, fg2 = 0;
for (int i = 0; i < s.size(); i++) {
if (s[i] == '(') {
cntl++;
}
else {
cntr++;
}
}
for (int i = 0; i < s.size(); i++) {
if (s[i] == '(') {
fg1++;
}
else {
fg1--;
}
if (fg1 < 0)
break;
if (fg1 == 0 && i == n - 1) {
cout << 1 << '\n';
for (int i = 0; i < s.size(); i++) {
cout << 1 << ' ';
}
cout << '\n';
return;
}
}
for (int i = 0; i < s.size(); i++) {
if (s[i] == ')') {
fg2++;
}
else {
fg2--;
}
if (fg2 < 0)
break;
if (fg2 == 0 && i == n - 1) {
cout << 1 << '\n';
for (int i = 0; i < s.size(); i++) {
cout << 1 << ' ';
}
cout << '\n';
return;
}
}
if (cntl != cntr) {
cout << -1 << '\n';
return;
}
vector<int>a(s.size());
if (s[0] == '(')
q[0] = 1;
if (s[n - 1] == ')')
h[n - 1] = 0;
for (int i = 1; i < s.size(); i++) {
q[i] = q[i - 1] + (s[i] == '(');
}
for (int i = n - 2; i >= 0; i--) {
h[i] = h[i + 1] + (s[i] == ')');
}
for (int i = 0; i < n; i++) {
if (q[i] == h[i+1]) {
for (int j = 0; j <= i; j++)
if(s[j]=='(')
a[j] = 1;
for (int j = n-1; j >= i+1; j--)
if (s[j] == ')')
a[j] = 1;
}
}
cout << 2 << '\n';
for (int j = 0; j < n; j++) {
if (a[j] == 0)
cout << 2 << ' ';
else
cout << 1 << ' ';
}
cout << '\n';
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
std::cout.tie(0);
int t = 1;
cin >> t;
while (t--)
solve();
return 0;
}