#include<bits/stdc++.h> #define N 200007 usingnamespacestd; typedeflonglong ll;
inlineintrd(){ int x = 0; bool f = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) f |= (c == '-'); for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); return f ? -x : x; }
voiddfs(int u){ ++cnt[col[u]]; for (int i = hd[u], v; i; i = e[i].nxt) { v = e[i].to; if (col[v] != -1) { if (col[v] != (col[u] ^ e[i].w)) {fl = 1; return;} continue; } col[v] = (col[u] ^ e[i].w); dfs(v); if(fl) return; } }
inlineboolwork(int p){ tot = 0; for (int i = 1; i <= n; ++i) hd[i] = 0, col[i] = -1; for (int i = 1; i <= m; ++i) add(c[i].a, c[i].b, ((c[i].w & (1 << p)) > 0)); for (int i = 1; i <= n; ++i) if (col[i] == -1) { cnt[0] = 0; cnt[1] = 0; col[i] = 0; dfs(i); if (fl) return1; ans += 1ll * (1 << p) * min(cnt[0], cnt[1]); } return0; }
intmain(){ n = rd(); m = rd(); for (int i = 1; i <= m; ++i) { c[i].a = rd(); c[i].b = rd(); c[i].w = rd(); } for (int i = 30; i >= 0; --i) if (work(i)) {puts("-1"); return0;} printf("%lld\n", ans); return0; }
D - Cross the Maze
E - Edward Gaming, the
Champion
给定一个串,数串里有多少个 edgnb
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include<bits/stdc++.h> #define N 200007 usingnamespacestd;
int ans; char s[N];
intmain(){ scanf("%s", s); for (int i = 0; s[i]; ++i) if (!strncmp(s + i, "edgnb", 5)) ++ans; printf("%d\n", ans); return0; }
#include<bits/stdc++.h> #define N 1007 usingnamespacestd;
inlineintrd(){ int x = 0; bool f = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) f |= (c == '-'); for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); return f ? -x : x; }
char S[N];
map<char, int> f;
structnode { int len; char a[N]; }s[N];
inlineboolcmp(node a, node b){ int tot = min(a.len, b.len); for (int i = 1; i <= tot; ++i) if (a.a[i] != b.a[i]) return a.a[i] < b.a[i]; return a.len < b.len; }
inlinevoidwork(int p){ int cnt = 0; f.clear(); s[p].len = p; for (int i = p; i; --i) { if (!f[S[i]]) f[S[i]] = ++cnt; s[p].a[i] ='a' - 1 + f[S[i]]; } }
intmain(){ int n = rd(); scanf("%s", S + 1); for (int i = 1; i <= n; ++i) work(i); sort(s + 1, s + 1 + n, cmp); for (int i = 1; i <= s[n].len; ++i) putchar(s[n].a[i]); puts(""); return0; }
#include<bits/stdc++.h> #define N 200007 usingnamespacestd; typedeflonglong ll;
inlineintrd(){ int x = 0; bool f = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) f |= (c == '-'); for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); return f ? -x : x; }
inlineintrd(){ int x = 0; bool f = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) f |= (c == '-'); for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); return f ? -x : x; }
inline C rdc(){ int x = rd(), y = rd(); return C(x, y); }
inlineintrd(){ int x = 0; bool f = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) f |= (c == '-'); for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); return f ? -x : x; }
inlineboolgetmin(int &a, int b){return (a > b ? (a = b, true) : false);} inlineboolgetmax(int &a, int b){return (a < b ? (a = b, true) : false);}
#define N 10000
queue<int> q;
int dis[N];
intmain(){ memset(dis, 0x3f, sizeof(dis)); q.push(0); dis[0] = 0; while(!q.empty()) { int u = q.front(); q.pop(); int a[4] = {u / 1000, u % 1000 / 100, u % 100 / 10, u % 10}; for (int x = 1; x <= 4; ++x) { for (int l = 0; l <= 4 - x; ++l) { // up int v = 0; for (int i = 0; i < 4; ++i) if (i >= l && i < l + x) v = v * 10 + (a[i] + 1) % 10; else v = v * 10 + a[i]; if (dis[v] > dis[u] + 1) {dis[v] = dis[u] + 1; q.push(v);} v = 0; for (int i = 0; i < 4; ++i) if (i >= l && i < l + x) v = v * 10 + (a[i] + 9) % 10; else v = v * 10 + a[i]; if (dis[v] > dis[u] + 1) {dis[v] = dis[u] + 1; q.push(v);} } } } for (int t = rd(); t; --t) { int u = rd(), v = rd(); int a[4] = {u / 1000, u % 1000 / 100, u % 100 / 10, u % 10}; int b[4] = {v / 1000, v % 1000 / 100, v % 100 / 10, v % 10}; int dlt = 0; for (int i = 0; i < 4; ++i) dlt = dlt * 10 + (b[i] - a[i] + 10) % 10; printf("%d\n", dis[dlt]); } return0; }