版本号排序

不知道什么傻逼原因,就是过不了

#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 << << endl; for(int i = ; i < ; i++) cout << << ' '; fclose(stdout);
} VI a[];
bool ok(int x, int y) {
int lx = a[x].size(), ly = a[y].size();
int l = min(lx, ly); for(int i = ; 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(), cin.tie();
int n;
cin >> n; for(int i = ; i < n; i++) {
int tmp;
cin >> tmp;
a[i].push_back(tmp); while(getchar() == '.') {
cin >> tmp;
a[i].push_back(tmp);
}
} for(int i = ; i < n; i++) {
for(int j = i + ; j < n; j++) {
if(!ok(i, j)) swap(a[i], a[j]);
}
} for(int i = ; i < n; i++) {
cout << a[i][]; for(int j = ; j < a[i].size(); j++) cout << '.' << a[i][j]; cout << endl;
} return ;
}

自底向上遍历二叉树

叶节点从左到右的顺序就是它们在树的从上到下的遍历中的顺序,按遍历中的顺序不断向上找

#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 << << endl; for(int i = ; i < ; i++) cout << << ' '; fclose(stdout);
} VI ch[];
bool f[];
int father[];
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() == ) traverse(x);
else for(int i = ; 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(), cin.tie();
int n;
cin >> n;
memset(father, , sizeof(father)); for(int i = ; i < n; i++) {
int u, v;
cin >> u >> v;
father[v] = u;
ch[u].push_back(v);
} for(int i = ; i <= n; i++) {
if(ch[i].size() != ) continue; if(ch[i][] > ch[i][]) {
int tmp = ch[i][];
ch[i][] = ch[i][];
ch[i][] = tmp;
}
} int root = ; while(father[root]) root = father[root]; memset(f, false, sizeof(f));
dfs(root);
return ;
}

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 << << endl; for(int i = ; i < ; i++) cout << << ' '; fclose(stdout);
} lint L[][];
const lint INF = (1LL << ); char dfs(string s, int n, lint k) {
char ch = s[];
int len = s.size();
string ss; if(n == ) {
if(k > len) return ;
else return s[k - ];
} if(L[ch][n] >= k) {
if(ch == 'h') ss = "hio"; if(ch == 'i') ss = "hi"; if(ch == 'o') ss = "ho"; return dfs(ss, n - , k);
} else {
k -= L[ch][n];
ss = s.substr(, len - );
return dfs(ss, n, k);
}
} int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
#endif
//makedata();
std::ios::sync_with_stdio(), cin.tie();
L['h'][] = L['i'][] = L['o'][] = ; for(int i = ; i <= ; i++) {
L['h'][i] = L['h'][i - ] + L['i'][i - ] + L['o'][i - ];
L['i'][i] = L['h'][i - ] + L['i'][i - ];
L['o'][i] = L['h'][i - ] + L['o'][i - ]; 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 - , k) << endl;;
} return ;
}

最长多数子串

一开始想的是二分+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][], fa[maxn], maxlen[maxn], Last, sz;
int root, nxt[maxn], size[maxn];
void init() {
sz = ;
root = ++sz;
memset(size, , sizeof(size));
memset(ch[], , sizeof(ch[]));
memset(nxt, , sizeof(nxt));
}
void add(int x) {
int np = ++sz, p = Last;
Last = np;
memset(ch[np], , sizeof(ch[np]));
maxlen[np] = maxlen[p] + ;
while (p && !ch[p][x]) ch[p][x] = np, p = fa[p];
if (!p) fa[np] = ;
else {
int q = ch[p][x];
if (maxlen[p] + == 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] + ;
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 = ; i <= n; i++) {
scanf("%s", s + );
Sam.Last = Sam.root;
len = strlen(s + );
now = i;
for (int j = ; j <= len; j++) Sam.add(s[j] - 'a');
}
Max = ;
ans = ;
for (int i = ; i <= Sam.sz; i++)
if (Sam.size[i] >= m && Sam.maxlen[i] > ans) {
Max = i;
ans = Sam.maxlen[i];
}
printf("%d\n", ans);
}
return ;
}
05-22 15:18