版本号排序
不知道什么傻逼原因,就是过不了
#pragma comment(linker, "/STACK:102400000,102400000") #include<stdio.h> #include<string.h> #include<stdlib.h> #include<vector> #include<algorithm> #include<iostream> #include<map> #include<queue> #include<stack> #include<string> #include<functional> #include<math.h> //#include<bits/stdc++.h> using namespace std; typedef long long lint; typedef vector<int> VI; typedef pair<int, int> PII; void makedata() { freopen("input.txt", "w", stdout); cout << 200000 << endl; for(int i = 0; i < 200000; i++) cout << 1000000000 << ' '; fclose(stdout); } VI a[200]; bool ok(int x, int y) { int lx = a[x].size(), ly = a[y].size(); int l = min(lx, ly); for(int i = 0; i < l; i++) { if(a[x][i] < a[y][i]) return true; if(a[x][i] > a[y][i]) return false; } if(lx <= ly) return true; else return false; } int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); #endif //makedata(); std::ios::sync_with_stdio(0), cin.tie(0); int n; cin >> n; for(int i = 0; i < n; i++) { int tmp; cin >> tmp; a[i].push_back(tmp); while(getchar() == '.') { cin >> tmp; a[i].push_back(tmp); } } for(int i = 0; i < n; i++) { for(int j = i + 1; j < n; j++) { if(!ok(i, j)) swap(a[i], a[j]); } } for(int i = 0; i < n; i++) { cout << a[i][0]; for(int j = 1; j < a[i].size(); j++) cout << '.' << a[i][j]; cout << endl; } return 0; }
自底向上遍历二叉树
叶节点从左到右的顺序就是它们在树的从上到下的遍历中的顺序,按遍历中的顺序不断向上找
#pragma comment(linker, "/STACK:102400000,102400000") #include<stdio.h> #include<string.h> #include<stdlib.h> #include<vector> #include<algorithm> #include<iostream> #include<map> #include<queue> #include<stack> #include<string> #include<functional> #include<math.h> //#include<bits/stdc++.h> using namespace std; typedef long long lint; typedef vector<int> VI; typedef pair<int, int> PII; void makedata() { freopen("input.txt", "w", stdout); cout << 200000 << endl; for(int i = 0; i < 200000; i++) cout << 1000000000 << ' '; fclose(stdout); } VI ch[110000]; bool f[110000]; int father[110000]; void traverse(int x) { while(f[x] == false && x) { cout << x << endl; f[x] = true; x = father[x]; } } void dfs(int x) { if(ch[x].size() == 0) traverse(x); else for(int i = 0; i < ch[x].size(); i++) dfs(ch[x][i]); } int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); #endif //makedata(); std::ios::sync_with_stdio(0), cin.tie(0); int n; cin >> n; memset(father, 0, sizeof(father)); for(int i = 1; i < n; i++) { int u, v; cin >> u >> v; father[v] = u; ch[u].push_back(v); } for(int i = 1; i <= n; i++) { if(ch[i].size() != 2) continue; if(ch[i][0] > ch[i][1]) { int tmp = ch[i][0]; ch[i][0] = ch[i][1]; ch[i][1] = tmp; } } int root = 1; while(father[root]) root = father[root]; memset(f, false, sizeof(f)); dfs(root); return 0; }
hiho字符串2
根据长度dfs,注意一开始是第1代而不是第0代。
#pragma comment(linker, "/STACK:102400000,102400000") #include<stdio.h> #include<string.h> #include<stdlib.h> #include<vector> #include<algorithm> #include<iostream> #include<map> #include<queue> #include<stack> #include<string> #include<functional> #include<math.h> //#include<bits/stdc++.h> using namespace std; typedef long long lint; typedef vector<int> VI; typedef pair<int, int> PII; void makedata() { freopen("input.txt", "w", stdout); cout << 200000 << endl; for(int i = 0; i < 200000; i++) cout << 1000000000 << ' '; fclose(stdout); } lint L[256][200]; const lint INF = (1LL << 40); char dfs(string s, int n, lint k) { char ch = s[0]; int len = s.size(); string ss; if(n == 0) { if(k > len) return 0; else return s[k - 1]; } if(L[ch][n] >= k) { if(ch == 'h') ss = "hio"; if(ch == 'i') ss = "hi"; if(ch == 'o') ss = "ho"; return dfs(ss, n - 1, k); } else { k -= L[ch][n]; ss = s.substr(1, len - 1); return dfs(ss, n, k); } } int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); #endif //makedata(); std::ios::sync_with_stdio(0), cin.tie(0); L['h'][0] = L['i'][0] = L['o'][0] = 1; for(int i = 1; i <= 150; i++) { L['h'][i] = L['h'][i - 1] + L['i'][i - 1] + L['o'][i - 1]; L['i'][i] = L['h'][i - 1] + L['i'][i - 1]; L['o'][i] = L['h'][i - 1] + L['o'][i - 1]; if(L['h'][i] > INF) L['h'][i] = INF; if(L['i'][i] > INF) L['i'][i] = INF; if(L['o'][i] > INF) L['o'][i] = INF; } int t, n; lint k; cin >> t; string s = "hiho"; while(t--) { cin >> n >> k; cout << dfs(s, n - 1, k) << endl;; } return 0; }
最长多数子串
一开始想的是二分+hash,没过。别人用的SAM,想破了脑袋也想不清楚。以前看过SAM,当时看完就觉得:“好叼啊,可是有什么用啊?”,怎么也想不通在哪里能用上,这里别人用了,怎么也想不通到底怎么过的。
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #include<memory> #include<cmath> #define maxn 2000003 using namespace std; int n, m, len, ans, Max, now; char s[maxn], cap[maxn]; struct SAM { int ch[maxn][26], fa[maxn], maxlen[maxn], Last, sz; int root, nxt[maxn], size[maxn]; void init() { sz = 0; root = ++sz; memset(size, 0, sizeof(size)); memset(ch[1], 0, sizeof(ch[1])); memset(nxt, 0, sizeof(nxt)); } void add(int x) { int np = ++sz, p = Last; Last = np; memset(ch[np], 0, sizeof(ch[np])); maxlen[np] = maxlen[p] + 1; while (p && !ch[p][x]) ch[p][x] = np, p = fa[p]; if (!p) fa[np] = 1; else { int q = ch[p][x]; if (maxlen[p] + 1 == maxlen[q]) fa[np] = q; else { int nq = ++sz; memcpy(ch[nq], ch[q], sizeof(ch[q])); size[nq] = size[q]; nxt[nq] = nxt[q]; maxlen[nq] = maxlen[p] + 1; fa[nq] = fa[q]; fa[q] = fa[np] = nq; while (p && ch[p][x] == q) ch[p][x] = nq, p = fa[p]; } } for (; np; np = fa[np]) if (nxt[np] != now) { size[np]++; nxt[np] = now; } else break; } }; SAM Sam; int main() { while (~scanf("%d%d", &n, &m) && n) { Sam.init(); for (int i = 1; i <= n; i++) { scanf("%s", s + 1); Sam.Last = Sam.root; len = strlen(s + 1); now = i; for (int j = 1; j <= len; j++) Sam.add(s[j] - 'a'); } Max = 0; ans = 0; for (int i = 1; i <= Sam.sz; i++) if (Sam.size[i] >= m && Sam.maxlen[i] > ans) { Max = i; ans = Sam.maxlen[i]; } printf("%d ", ans); } return 0; }