很明显当有一个字母出现次数>1时即合法
$n = 1$的情况需要特判
#include<cstdio>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<set>
#include<map>
#define Pair pair<int, int>
#define fi first
#define se second
#define MP(x, y) make_pair(x, y)
//#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1<<22, stdin), p1 == p2) ? EOF : *p1++)
//char buf[(1 << 22)], *p1 = buf, *p2 = buf;
const int MAXN = 1e6 + 10;
using namespace std;
inline int read() {
char c = getchar(); int x = 0, f = 1;
while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
int N;
char s[MAXN];
int num[MAXN];
int main() {
N = read();
if(N == 1) {
puts("Yes"); return 0;
}
scanf("%s", s + 1);
bool flag = 0;
for(int i = 1; i <= N; i++) {
num[s[i]]++;
if(num[s[i]] >= 2) flag = 1;
}
puts(flag == 1 ? "Yes" : "No");
return 0;
}
/*
*/
我的做法比较naive,直接对第一对数分解质因数,然后依次判断是否可行即可
TM一开始读错题了以为要输出最大的白浪费半个小时
#include<cstdio>
#include<cstdlib>
const int MAXN = 1e6 + 10;
int N, a[MAXN], b[MAXN], prime[MAXN], vis[MAXN * 10], tot = 0;
void check(int val) {
for(int i = 1; i <= N; i++) if((a[i] % val) && (b[i] % val)) return ;
printf("%d", val); exit(0);
}
void work(int x) {
for(int i = 2; i * i <= x; i++)
if(x % i == 0) {check(i); while(x % i == 0) x /= i;}
if(x != 1)check(x);
}
main() {
scanf("%d", &N);
for(int i = 1; i <= N; i++) scanf("%d %d", &a[i], &b[i]);
work(a[1]); work(b[1]);
puts("-1");
return 0;
}
自己手玩一下就可以发现:把字符串从某一位置劈开再旋转无非就是换一种读字符串的方式
那么只要在字符串的所有循环阅读方式中找一个交错序列最长的就行
可以通过拼接一次实现
#include<cstdio>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<set>
#include<map>
#define Pair pair<int, int>
#define fi first
#define se second
#define MP(x, y) make_pair(x, y)
#define int long long
//#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1<<22, stdin), p1 == p2) ? EOF : *p1++)
//char buf[(1 << 22)], *p1 = buf, *p2 = buf;
const int MAXN = 1e6 + 10;
using namespace std;
inline int read() {
char c = getchar(); int x = 0, f = 1;
while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
int N;
char s[MAXN];
main() {
scanf("%s", s + 1);
N = strlen(s + 1);
for(int i = 1; i <= N; i++) s[i + N] = s[i];
int mx = 0, cur = 0;
N <<= 1;
for(int i = 2; i <= N; i++)
if(s[i] == s[i - 1]) mx = max(mx, cur), cur = 0;
else cur++;
mx = max(cur, mx);
printf("%d", min(N / 2, mx + 1));
return 0;
}
/*
bwbwbw
*/
比赛的时候一直以为是xjb贪心,居然忘了大力dp qwq
首先考虑一个很显然的区间dp, $f[l][r][root]$表示$(l, r)$区间内,以$root$为根是否可行
转移的时候需要枚举根,以及左儿子的根,右儿子的根。时间复杂度为$n ^ 4$
考虑我们在转移的时候仅仅需要知道左右儿子是否能拼接起来,因此我们换一种表示方法
$L[l][r]$表示以$r$为根,向左能否扩展到$l$
$R[l][r]$表示以$l$为根,向右能否扩展到$r$
转移的时候仍然需要枚举根节点,但是不需要枚举左右儿子。
时间复杂度$O(n^3)$
#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
const int MAXN = 1001;
int N;
int a[MAXN], can[MAXN][MAXN], L[MAXN][MAXN], R[MAXN][MAXN], ans[MAXN][MAXN];
int main() {
cin >> N;
for(int i = 1; i <= N; i++) cin >> a[i], L[i][i] = R[i][i] = 1;
for(int i = 1; i <= N; i++)
for(int j = i + 1; j <= N; j++)
can[i][j] = can[j][i] = (__gcd(a[i], a[j]) > 1);
for(int len = 1; len <= N; len++)
for(int l = 1; l + len - 1 <= N; l++) {
int r = l + len - 1;
for(int k = l; k <= r; k++)
if(L[l][k] && R[k][r])
ans[l][r] = 1, L[l][r + 1] |= can[k][r + 1], R[l - 1][r] |= can[l - 1][k];
}
puts(ans[1][N] ? "Yes" : "No");
return 0;
}
据说是原题?fateice大爷抄了题解?https://blog.csdn.net/lych_cys/article/details/51000549