项链:
#include <bits/stdc++.h> using namespace std; #define rep(i,a,b) for (int i = a , _ = b ; i <= _ ; i ++) #define per(i,a,b) for (int i = a , _ = b ; i >= _ ; i --) #define cr(x) memset(x , 0 , sizeof x) inline int rd() { char c = getchar(); while (!isdigit(c)) c = getchar() ; int x = c - '0'; while (isdigit(c = getchar())) x = x * 10 + c - '0'; return x; } const int maxn = 800007; char str[maxn] , st[maxn]; int n , m; void input() { scanf("%s" , str + 1); n = strlen(str + 1) , m = n + n; str[n + n + 1] = '\0'; rep (i , 1 , n) str[i + n] = str[i]; cr(st); st[m + 1] = '#'; rep (i , 1 , m) st[i] = st[m + m + 2 - i] = str[i]; } struct SAM { int len[maxn] , fa[maxn] , go[maxn][26] , pre[maxn][21] , pos[maxn] , cnt[maxn] , tp[maxn] , dep[maxn]; int tot , ed; inline void clear() { rep (i , 1 , tot) len[i] = 0 , fa[i] = 0 , dep[i] = 0 , tp[i] = 0 , cr(go[i]); tot = ed = 1; } inline void add(int c , int id) { int p = ed , np = ed = ++ tot; pos[id] = np , len[np] = len[p] + 1; while (p && !go[p][c]) go[p][c] = np , p = fa[p]; if (!p) { fa[np] = 1 ; return ; } int q = go[p][c]; if (len[q] == len[p] + 1) { fa[np] = q ; return ; } int r = ++ tot; len[r] = len[p] + 1 , fa[r] = fa[q] , fa[q] = fa[np] = r; memcpy(go[r] , go[q] , sizeof go[q]); while (p && go[p][c] == q) go[p][c] = r , p = fa[p]; } inline void init() { rep (i , 0 , tot) cnt[i] = 0; rep (i , 1 , tot) cnt[len[i]] ++; rep (i , 1 , tot) cnt[i] += cnt[i - 1]; per (i , tot , 1) tp[cnt[len[i]] --] = i; rep (i , 1 , tot) { int u = tp[i]; pre[u][0] = fa[u]; dep[u] = dep[fa[u]] + 1; rep (j , 1 , 20) if (pre[u][j - 1]) pre[u][j] = pre[pre[u][j - 1]][j - 1]; else break; } } int lcp(int i , int j) { int u = pos[i] , v = pos[j]; if (dep[u] < dep[v]) swap(u , v); int d = dep[u] - dep[v]; rep (i , 0 , 20) if (d >> i & 1) u = pre[u][i]; if (u == v) return len[u]; per (i , 20 , 0) if (pre[u][i] != pre[v][i]) u = pre[u][i] , v = pre[v][i]; return len[fa[u]]; } }sam; void solve() { sam.clear(); m = m + m + 1; per (i , m , 1) sam.add(st[i] - 'a' , i); sam.init(); rep (i , 1 , n) { int lcp1 = sam.lcp(i , m - n - i + 1) , lcp2 = sam.lcp(i , m - n - i + 2); if (lcp1 >= n || lcp2 >= n) { puts("YES"); return; } } puts("NO"); } int main() { #ifndef ONLINE_JUDGE freopen("A.txt" , "r" , stdin); #endif for (int T = rd();T;T --) { input(); solve(); } return 0; }
#include <bits/stdc++.h> #define rep(i,a,b) for (int i = a , _ = b ; i <= _ ; i ++) #define per(i,a,b) for (int i = a , _ = b ; i >= _ ; i --) #define fore(i,u) for (int i = head[u] ; i ; i = nxt[i]) #define cr(x) memset(x , 0 , sizeof x); inline int rd() { char c = getchar(); while (!isdigit(c)) c = getchar() ; int x = c - '0'; while (isdigit(c = getchar())) x = x * 10 + c - '0'; return x; } typedef long long ll; const int maxi = 107; const int maxn = 42007; const int maxm = 160007; const int inf = 0x7fffffff; const int fx[] = {0 , 0 , 1 , -1 , 1 , -1 , 1 , -1}; const int fy[] = {1 , -1 , 0 , 0 , 1 , -1 , -1 , 1}; int v[maxi][maxi] , idx[256] , L[maxi][maxi] , R[maxi][maxi]; char mat[maxi][maxi] , str[maxi]; int n , m , tot , S , T , l; void input() { n = rd() , m = rd(); cr(mat); rep (i , 1 , n) scanf("%s" , mat[i] + 1); rep (i , 1 , n) rep (j , 1 , m) v[i][j] = rd(); scanf("%s" , str + 1); l = strlen(str + 1); cr(idx); rep (i , 1 , l) idx[str[i]] = i; } namespace dinic { typedef int arr[maxn]; typedef int adj[maxm]; std::queue<int> Q; arr head , cur , dis; adj to , nxt , cap , flow; int ett , S , T; inline void ins(int u , int v , int c) { to[++ ett] = v , nxt[ett] = head[u] , cap[ett] = c , head[u] = ett; to[++ ett] = u , nxt[ett] = head[v] , cap[ett] = 0 , head[v] = ett; } inline void clear() { rep (i , 1 , ett) flow[i] = cap[i] = 0; rep (i , 1 , (n * m + 1) * 2 + 1) head[i] = 0; ett = 1; } inline bool bfs() { rep (i , 1 , tot) dis[i] = 0; dis[S] = 1 , Q.push(S); while (!Q.empty()) { int u = Q.front() ; Q.pop(); fore (i , u) if (cap[i] > flow[i]) { int v = to[i]; if (!dis[v]) { dis[v] = dis[u] + 1; Q.push(v); } } } return dis[T] > 0; } int dfs(int u , int a) { if (u == T || !a) return a; int ret = 0 , f; for (int&i = cur[u] ; i ; i = nxt[i]) { int v = to[i]; if (dis[v] == dis[u] + 1 && (f = dfs(v , std::min(a , cap[i] - flow[i])))) { flow[i] += f , flow[i ^ 1] -= f , a -= f , ret += f; if (!a) break; } } return ret; } inline ll max_flow(int _s , int _t) { S = _s , T = _t; ll ret = 0; while (bfs()) { rep (i , 1 , tot) cur[i] = head[i]; ret += dfs(S , inf); } return ret; } } inline void init_id() { tot = 0; rep (i , 1 , n) rep (j , 1 , m) L[i][j] = ++ tot , R[i][j] = ++ tot , dinic::ins(L[i][j] , R[i][j] , v[i][j]); S = ++ tot , T = ++ tot; } void solve() { dinic::clear(); init_id(); rep (i , 1 , n) rep (j , 1 , m) { if (mat[i][j] == str[1]) dinic::ins(S , L[i][j] , inf); else if (mat[i][j] == str[l]) dinic::ins(R[i][j] , T , inf); rep (d , 0 , 7) { int x = i + fx[d] , y = j + fy[d]; if (!mat[x][y]) continue; if (idx[mat[i][j]] + 1 != idx[mat[x][y]]) continue; // printf("%d %d to %d %d\n" , i , j , x , y); dinic::ins(R[i][j] , L[x][y] , inf); } } ll ans = dinic::max_flow(S , T); printf("%lld\n" , ans); } int main() { #ifndef ONLINE_JUDGE freopen("B.txt" , "r" , stdin); #endif rep (i , 1 , rd()) { input(); printf("Case #%d: " , i); solve(); } return 0; }
#include <bits/stdc++.h> #define rep(i,a,b) for (int i = a , _ = b ; i <= _ ; i ++) #define per(i,a,b) for (int i = a , _ = b ; i >= _ ; i --) inline int rd() { char c = getchar(); while (!isdigit(c)) c = getchar() ; int x = c - '0'; while (isdigit(c = getchar())) x = x * 10 + c - '0'; return x; } int n , vis[10001] , L[10001] , R[10001]; std::vector<int> ans; void input() { n = rd(); } void dfs(int u) { if (!vis[R[u]]) { vis[R[u]] = 1; dfs(R[u] % (n / 2)); ans.push_back(R[u] % n); } if (!vis[L[u]]) { vis[L[u]] = 1; dfs(L[u] % (n / 2)); ans.push_back(L[u] % n); } } void solve() { if (n & 1) { puts("-1"); return; } rep (i , 0 , n / 2) L[i] = i * 2 , R[i] = i * 2 + 1; dfs(0); printf("0 "); per (i , n - 1 , 0) printf("%d%c" , ans[i] , i == n ? '\n' : ' '); } int main() { #ifndef ONLINE_JUDGE // freopen("C.txt" , "r" , stdin); #endif input(); solve(); return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i,a,b) for (int i = a , _ = b ; i <= _ ; i ++) #define per(i,a,b) for (int i = a , _ = b ; i >= _ ; i --) #define fore(i,u) for (int i = head[u] ; i ; i = nxt[i]) inline int rd() { char c = getchar(); while (!isdigit(c) && c != '-') c = getchar() ; int x = 0 , f = 1; if (c == '-') f = -1; else x = c - '0'; while (isdigit(c = getchar())) x = x * 10 + c - '0'; return x * f; } inline char rd_cmd() { char c = getchar(); while (!isalpha(c)) c = getchar(); while (isalpha(getchar())); return c; } inline int rnd() { static int rand_seed = 18230742 ; rand_seed += (rand_seed << 1 | 1); return rand_seed; } inline void upmax(int&a , int b) { if (a < b) a = b ; } inline void upmin(int&a , int b) { if (a > b) a = b ; } const int maxn = 50007; const int maxm = 100007; const int maxs = 600007; typedef int arr[maxn]; typedef int adj[maxm]; arr head , dep , sz , pos , idx , fa , son , top , val , col; adj to , nxt; int n , m , ett , mxc , dfs_clock; inline void ins(int u , int v) { to[++ ett] = v , nxt[ett] = head[u] , head[u] = ett; to[++ ett] = u , nxt[ett] = head[v] , head[v] = ett; } void dfs1(int u) { sz[u] = 1; fore (i , u) { int v = to[i]; if (dep[v]) continue; dep[v] = dep[u] + 1 , fa[v] = u; dfs1(v); sz[u] += sz[v]; if (sz[v] > sz[son[u]]) son[u] = v; } } void dfs2(int u , int p) { pos[u] = ++ dfs_clock , idx[dfs_clock] = u; top[u] = p; if (son[u]) dfs2(son[u] , p); fore (i , u) { int v = to[i]; if (v == son[u] || v == fa[u]) continue; dfs2(v , v); } } struct node { node *l , *r; int sum , sz; int key , pri; node (int key = 0): key(key) , sum(key) , pri(rnd()) , l(NULL) , r(NULL) { } inline void upd() { sz = 1 , sum = key; if (l) sz += l->sz , sum += l->sum; if (r) sz += r->sz , sum += r->sum; } }mem_pool[maxn * 11]; int mem_top; inline node *newnode(int key) { node *u = &mem_pool[mem_top ++]; *u = node(key); return u; } inline int Gsize(node *u) { return u == NULL ? 0 : u->sz; } inline int Gsum(node *u) { return u == NULL ? 0 : u->sum; } struct Droot { node *x , *y; Droot (node *x = NULL , node *y = NULL): x(x) , y(y) { } }; struct Troot { node *x , *y , *z; Troot (node *x = NULL , node *y = NULL , node *z = NULL): x(x) , y(y) , z(z) { } }; struct treap { node *rt; void build(int c) { static node *sta[maxn] , *pre , *u; int top = 0; sta[1] = NULL; rep (i , 1 , n) { u = newnode(col[idx[i]] == c ? val[idx[i]] : 0); pre = NULL; while (top && sta[top]->pri > u->pri) { sta[top]->upd(); pre = sta[top --]; } if (top) sta[top]->r = u; u->l = pre; sta[++ top] = u; } while (top) sta[top --]->upd(); rt = sta[1]; } node *join(node *u , node *v) { if (!u) return v; if (!v) return u; if (u->pri < v->pri) { u->r = join(u->r , v); u->upd(); return u; } else { v->l = join(u , v->l); v->upd(); return v; } } Droot split(node *u , int k) { if (!u) return Droot(); Droot t; if (Gsize(u->l) + 1 <= k) { t = split(u->r , k - Gsize(u->l) - 1); u->r = t.x , t.x = u; } else { t = split(u->l , k); u->l = t.y , t.y = u; } u->upd(); return t; } void modi(node *u , int k , int v) { int t = Gsize(u->l) + 1; if (k == t) u->key = v; else if (k < t) modi(u->l , k , v); else modi(u->r , k - t , v); u->upd(); } Troot Split(int l , int r) { Droot t1 = split(rt , r) , t2 = split(t1.x , l - 1); return Troot(t2.x , t2.y , t1.y); } inline void Merge(Troot t) { rt = join(join(t.x , t.y) , t.z); } void Set(int k , int v) { modi(rt , k , v); } int query(int l , int r) { Troot t = Split(l , r); int ret = Gsum(t.y); Merge(t); return ret; } }path[10]; void input() { n = rd(); rep (i , 1 , n) col[i] = rd() , upmax(mxc , col[i]); rep (i , 1 , n) val[i] = rd(); rep (i , 2 , n) ins(rd() + 1 , rd() + 1); dep[1] = 1; dfs1(1) , dfs2(1 , 1); rep (c , 0 , 9) path[c].build(c); } inline void Change() { int u = rd() + 1 , x = rd() , y = rd(); Troot t1 = path[x].Split(pos[u] , pos[u] + sz[u] - 1); Troot t2 = path[y].Split(pos[u] , pos[u] + sz[u] - 1); swap(t1.y , t2.y); path[x].Merge(t1) , path[y].Merge(t2); } #define t1 top[u] #define t2 top[v] inline void Query() { int u = rd() + 1 , v = rd() + 1 , c = rd(); int ans = 0; while (t1 != t2) { if (dep[t1] < dep[t2]) swap(u , v); ans += path[c].query(pos[t1] , pos[u]); u = fa[t1]; } if (dep[u] > dep[v]) swap(u , v); ans += path[c].query(pos[u] , pos[v]); printf("%d\n" , ans); } #undef t1 #undef t2 inline void Set() { int u = rd() + 1 , c = rd() , v = rd(); rep (i , 0 , mxc) if (i != c) path[i].Set(pos[u] , 0); else path[i].Set(pos[u] , v); } void solve() { m = rd(); rep (i , 1 , m) { char c = rd_cmd(); if (c == 'C') Change(); else if (c == 'A') Query(); else Set(); } } int main() { #ifndef ONLINE_JUDGE freopen("data.txt" , "r" , stdin); #endif input(); solve(); return 0; }