代码:
#includeusing namespace std;const int maxn = 1e5 + 10;int N, cnt = 0;int a[maxn], vis[maxn];int main() { scanf("%d", &N); for(int i = 1; i <= N; i ++) scanf("%d", &a[i]); int l = 1, r = 1; while(l <= N && r <= N) { while(a[l] == a[r] && r <= N) vis[cnt] ++, r ++; cnt ++; l = r; } int maxx = 0; for(int i = 0; i < cnt - 1; i ++) { int t = min(vis[i], vis[i + 1]); maxx = max(t * 2, maxx); } printf("%d\n", maxx); return 0;}
代码:
#includeusing namespace std;const int maxn = 1e5 + 10;int N;string s, t;int A, B ,C, D;int a, b, c, d;int main() { scanf("%d", &N); cin >> s >> t; for(int i = 0; i < N; i ++) { if(s[i] == '0') { if(t[i] == '0') A ++; else B ++; } else { if(t[i] == '0') C ++; else D ++; } } bool flag = false; for(a = 0; a <= A && a <= N / 2; a ++) { int t1 = a + B + D - N / 2; if(t1 >= 0 && t1 <= N / 2 && t1 <= D) { d = t1; for(b = 0; b <= B && b <= N / 2; b ++) { int t2 = N / 2 - a - b - d; if(t2 >= 0 && t2 <= C && t2 <= N / 2) { c = t2; flag = true; break; } } if(flag) break; } } if(flag) { vector ans; for(int i = 0; i < N; i ++) { if(s[i] == '0') { if(a && t[i] == '0') { ans.push_back(i + 1); a --; } else if(b && t[i] == '1') { ans.push_back(i + 1); b --; } } else { if(c && t[i] == '0') { ans.push_back(i + 1); c --; } else if(d && t[i] == '1') { ans.push_back(i + 1); d --; } } } for(int i = 0; i < ans.size(); i ++) printf("%d%s", ans[i], i != ans.size() - 1 ? " " : "\n"); } else printf("-1\n"); return 0;}
D 很简单的 KMP 求出来 nx[t.length()] 然后求出来最小循环节 先把最小循环节前面的部分输出然后输出最小循环节最后把剩下的输出 我是。。真滴不想写了