intmain(){ int T; cin >> T; while (T--) { string s; cin >> s; int n = s.length(); int l = 0, r = n - 1; for (char i = n - 1 + 'a'; i >= 'a'; i--) { if (s[l] == i) ++l; elseif (s[r] == i) --r; elsebreak; } if (l > r) cout << "YES\n"; else cout << "NO\n"; } return0; }
intmain(){ int T; cin >> T; while (T--) { int k, n, m; cin >> k >> n >> m; vector<int> a(n), b(m); for (auto& v: a) cin >> v; for (auto& v: b) cin >> v; bool ok = true; int p1 = 0, p2 = 0; vector<int> ans; while (p1 < n || p2 < m) { bool f = false; while (p1 < n && a[p1] <= k) ans.push_back(a[p1]), k += a[p1] == 0, ++p1, f = true; while (p2 < m && b[p2] <= k) ans.push_back(b[p2]), k += b[p2] == 0, ++p2, f = true; if (!f) { ok = false; break; } } if (!ok) cout << "-1\n"; else { for (auto& v: ans) cout << v << " "; cout << "\n"; } } return0; }
#include<bits/stdc++.h> #define ls k<<1 #define rs k<<1|1 #define lson k<<1, l, mid #define rson k<<1|1, mid+1, r usingnamespace std;
constint MAX = 4e5+50; structNode { int l, r, g; }tree[MAX<<2]; int a[MAX], n;
voidbuild(int k, int l, int r){ tree[k].l = l; tree[k].r = r; if (l == r) { tree[k].g = a[l]; return; } int mid = (l + r) >> 1; build(lson); build(rson); tree[k].g = __gcd(tree[ls].g, tree[rs].g); }
intquery(int k, int l, int r){ if (tree[k].l == l && tree[k].r == r) return tree[k].g; int mid = (tree[k].l + tree[k].r) >> 1; if (r <= mid) returnquery(ls, l, r); elseif (l > mid) returnquery(rs, l, r); elsereturn __gcd(query(lson), query(rson)); }
boolcheck(int x){ int val = query(1, 1, x); for (int i = 2; i <= n; i++) { if (val != query(1, i, i + x - 1)) returnfalse; } returntrue; }
intmain(){ int T; scanf("%d", &T); while (T--) { scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); a[i + n] = a[i]; } bool flag = true; for (int i = 2; i <= n; i++) { if (a[i] != a[i - 1]) { flag = false; break; } } if (flag) { puts("0"); continue; } build(1, 1, n * 2); int l = 1, r = n, res = n; while (l <= r) { int mid = (l + r) >> 1; if (check(mid)) res = mid, r = mid - 1; else l = mid + 1; } printf("%d\n", res - 1); } return0; }
constint MAX = 4e5+50; int dfn[MAX], low[MAX], idx, n, m; int stk[MAX], top; int belong[MAX], cnt[MAX], sum; bool instk[MAX], self[MAX]; vector<int> es[MAX]; int ans[MAX];
voidtarjan(int u){ dfn[u] = low[u] = ++idx; stk[++top] = u; instk[u] = true; for (auto& v: es[u]) { if (dfn[v] == 0) { tarjan(v); low[u] = min(low[u], low[v]); } elseif (instk[v]) low[u] = min(low[u], dfn[v]); } if (dfn[u] == low[u]) { int v; belong[u] = ++sum; instk[u] = false; do{ v = stk[top--]; belong[v] = sum; instk[v] = false; ++cnt[sum]; } while (u != v); } }
voiddfs1(int u, int cur){ for (auto& v: es[u]) { if (ans[v] == 0) { ans[v] = cur; dfs1(v, cur); } elseif (ans[v] == 1) { ans[v] = 2; dfs1(v, 2); } } }
voiddfs2(int u){ for (auto& v: es[u]) { if (ans[v] == -1) continue; ans[v] = -1; dfs2(v); } }
intmain(){ int T; scanf("%d", &T); while (T--) { scanf("%d%d", &n, &m); init(); for (int i = 1; i <= m; i++) { int u, v; scanf("%d%d", &u, &v); if (u == v) { self[u] = true; continue; } es[u].push_back(v); } ans[1] = 1; dfs1(1, 1); for (int i = 1; i <= n; i++) if (ans[i] == 2) dfs1(i, 2); tarjan(1); for (int i = 1; i <= n; i++) { if (!dfn[i]) continue; if ((self[i] || cnt[belong[i]] > 1) && ans[i] != -1) ans[i] = -1, dfs2(i); } for (int i = 1; i <= n; i++) printf("%d%c", ans[i], " \n"[i == n]); } return0; }