题目链接:
A:直接找满足的人数,然后整除3就是答案
B:开一个vis数组记录每一个衣服的主场和客场出现次数。然后输出的时候主场数量加上反复的,客场数量减掉反复的
C:原来是YY乱搞的。原来是哥德巴赫猜想,一个合数能够表示为3个质数相加,然后就先打个素数表,然后从最小的数字一个个模拟往前放就可以。放的时候走的步数直接拆成都是质数就可以
D:KMP算法,利用next数组性质求前缀和后缀匹配,然后在利用累加求和求出每一个串相应的出现次数
代码:
A:
#includeB:#include #include using namespace std;int n, k, num, i;int main() { scanf("%d%d", &n, &k); int ans = 0; for (i = 0; i < n; i++) { scanf("%d", &num); if (5 - num >= k) ans++; } printf("%d\n", ans / 3); return 0;}
#includeC:#include const int N = 100005;int vis[N][2];int n, x[N], y[N], i;int main() { scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%d%d", &x[i], &y[i]); vis[x[i]][0]++; vis[y[i]][1]++; } for (i = 0; i < n; i++) { printf("%d %d\n", (n - 1) + vis[y[i]][0], (n - 1) - vis[y[i]][0]); } return 0;}
#includeD:#include #include using namespace std;const int N = 100005;int pri[N], ans[5 * N][2], ansn = 0;void init() { int vis[N]; memset(vis, 0, sizeof(vis)); for (int i = 2; i < N; i++) { if (vis[i]) continue; pri[i] = 1; for (int j = i; j < N; j += i) vis[j] = 1; }}int n, num[N], v[N], i, snum[N];void swap(int &a, int &b) { a ^= b; b ^= a; a ^= b;}int main() { init(); scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%d", &num[i]); snum[i] = num[i]; v[num[i]] = i; } sort(snum, snum + n); i = 0; while (i < n) { while (v[snum[i]] != i) { for (int j = i; ;j++) { if (pri[v[snum[i]] - j + 1]) { ans[ansn][0] = j + 1; ans[ansn][1] = v[snum[i]] + 1; ansn++; int t = v[snum[i]]; v[snum[i]] = j; v[num[j]] = t; swap(num[j], num[t]); break; } } } i++; } printf("%d\n", ansn); for (i = 0; i < ansn; i++) printf("%d %d\n", ans[i][0], ans[i][1]); return 0;}
#include#include #define INF 0x3f3f3f3fconst int N = 100005;char s[N];int next[N], n, ans[N], ansn = 0;void get_next(char *seq, int m) { next[1] = 0; int j = next[1]; for (int i = 2; i <= m; i++) { while (j && seq[i] != seq[j + 1]) j = next[j]; if (seq[i] == seq[j + 1]) j++; next[i] = j; }}int vis[N];int main() { int i = 0; scanf("%s", s + 1); n = strlen(s + 1); get_next(s, n); int t = next[n]; while (t) { ans[ansn++] = t; t = next[t]; } for (i = n; i > 0; i--) vis[next[i]]++; for (i = n; i > 0; i--) vis[next[i]] += vis[i]; printf("%d\n", ansn + 1); for (i = ansn - 1; i >= 0; i--) printf("%d %d\n", ans[i], vis[ans[i]] + 1); printf("%d %d\n", n, vis[n] + 1); return 0;}