真•普及板子
写在前面
按此题单顺序整理
今天不知道为啥就想打普及板子,可能因为我是个普及选手
只有代码,没有解析
真•普及板子
P3371 【模板】单源最短路径(弱化版)
/*
Name: P3371 【模板】单源最短路径(弱化版)
Author: Loceaner
Date: 26/08/20 07:24
Description:
Debug: 建有向边
*/
#include <queue>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int A = 5e5 + 11;
const int B = 1e6 + 11;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
inline int read() {
char c = getchar();
int x = 0, f = 1;
for ( ; !isdigit(c); c = getchar()) if (c == '-') f = -1;
for ( ; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48);
return x * f;
}
int n, m, s, dis[A], inq[A], head[A], cnt;
struct node { int to, nxt, val; } e[A << 1];
inline void add(int from, int to, int val) {
e[++cnt].to = to;
e[cnt].val = val;
e[cnt].nxt = head[from];
head[from] = cnt;
}
void SPFA() {
queue <int> Q;
memset(dis, inf, sizeof(dis));
Q.push(s), dis[s] = 0, inq[s] = 1;
while (!Q.empty()) {
int x = Q.front(); Q.pop(), inq[x] = 0;
for (int i = head[x]; i; i = e[i].nxt) {
int to = e[i].to;
if (dis[to] > dis[x] + e[i].val) {
dis[to] = dis[x] + e[i].val;
if (!inq[to]) Q.push(to), inq[to] = 1;
}
}
}
}
int main() {
n = read(), m = read(), s = read();
for (int i = 1; i <= m; i++) {
int x = read(), y = read(), val = read();
add(x, y, val);
}
SPFA();
for (int i = 1; i <= n; i++)
printf("%d ", dis[i] == inf ? 2147483647 : dis[i]);
return 0;
}
P3367 【模板】并查集
/*
Name: P3367 【模板】并查集
Author: Loceaner
Date: 26/08/20 08:03
Description: 并查集
Debug:
*/
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int A = 1e5 + 11;
const int B = 1e6 + 11;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
inline int read() {
char c = getchar();
int x = 0, f = 1;
for ( ; !isdigit(c); c = getchar()) if (c == '-') f = -1;
for ( ; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48);
return x * f;
}
int n, m, fa[A];
int find(int x) {
return fa[x] == x ? x : fa[x] = find(fa[x]);
}
int main() {
n = read(), m = read();
for (int i = 1; i <= n; i++) fa[i] = i;
for (int i = 1; i <= m; i++) {
int opt = read(), x = read(), y = read();
int dx = find(x), dy = find(y);
if (opt == 1) {
fa[dx] = dy;
}
else {
if (dx == dy) puts("Y");
else puts("N");
}
}
}
P1226 【模板】快速幂||取余运算
/*
Name: P1226 【模板】快速幂||取余运算
Author: dsrdsr
Date: 26/08/20 08:07
Description:
Debug:
*/
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define int long long
using namespace std;
const int A = 1e5 + 11;
const int B = 1e6 + 11;
const int inf = 0x3f3f3f3f;
inline int read() {
char c = getchar();
int x = 0, f = 1;
for ( ; !isdigit(c); c = getchar()) if (c == '-') f = -1;
for ( ; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48);
return x * f;
}
inline int power(int a, int b, int mod) {
int res = 1;
while (b > 0) {
if (b & 1) res = res * a % mod;
a = a * a % mod, b >>= 1;
}
return res % mod;
}
signed main() {
int a = read(), b = read(), k = read();
printf("%lld^%lld mod %lld=%lld\n", a, b, k, power(a, b, k));
return 0;
}
P1177 【模板】快速排序
基数排序
/*
Name: P1177 【模板】快速排序
Author: Loceaner
Date: 26/08/20 08:14
Description:
Debug: 基数排序
*/
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int A = 1e5 + 11;
const int B = 1e6 + 11;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
inline int read() {
char c = getchar();
int x = 0, f = 1;
for ( ; !isdigit(c); c = getchar()) if (c == '-') f = -1;
for ( ; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48);
return x * f;
}
int n, a[A], maxn, base = 1, cnt[A], b[A];
void radixsort() {
memset(cnt, 0, sizeof(cnt));
for (int i = 1; i <= n; i++) cnt[(a[i] / base) % 10]++;
for (int i = 1; i <= 10; i++) cnt[i] += cnt[i - 1];
for (int i = n; i >= 1; i--) b[cnt[(a[i] / base) % 10]--] = a[i];
swap(a, b);
}
int main() {
n = read();
for (int i = 1; i <= n; i++) a[i] = read(), maxn = max(a[i], maxn);
while (maxn / base) {
radixsort();
base *= 10;
}
for (int i = 1; i <= n; i++) cout << a[i] << " ";
}
归并排序
/*
Name: P1177 【模板】快速排序
Author: Loceaner
Date: 26/08/20 08:35
Description:
Debug:
*/
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int A = 1e5 + 11;
const int B = 1e6 + 11;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
inline int read() {
char c = getchar();
int x = 0, f = 1;
for ( ; !isdigit(c); c = getchar()) if (c == '-') f = -1;
for ( ; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48);
return x * f;
}
int n, a[A], b[A];
void solve(int l, int r) {
if (l >= r) return;
int mid = (l + r) >> 1, i = l, j = mid + 1, now = l;
solve(l, mid), solve(mid + 1, r);
//先处理两个子区间,保证两个区间排好序了,然后再处理大区间
while (i <= mid && j <= r) {
if (a[i] <= a[j]) b[now++] = a[i++];
else b[now++] = a[j++];
}
while (i <= mid) b[now++] = a[i++];
while (j <= r) b[now++] = a[j++];
for (int i = l; i <= r; i++) a[i] = b[i];
}
int main() {
n = read();
for (int i = 1; i <= n; i++) a[i] = read();
solve(1, n);
for (int i = 1; i <= n; i++) cout << a[i] << " ";
return 0;
}
sort
必然没有
P3370 【模板】字符串哈希
/*
Name: P3370 【模板】字符串哈希
Author: Loceaner
Date: 26/08/20 09:28
Description: Hash
Debug:
*/
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define ull unsigned long long
using namespace std;
const int A = 1e5 + 11;
const int B = 1e6 + 11;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
inline int read() {
char c = getchar();
int x = 0, f = 1;
for ( ; !isdigit(c); c = getchar()) if (c == '-') f = -1;
for ( ; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48);
return x * f;
}
char s[A];
ull h[A];
int n, m, ans[A];
inline int work(char s[A]) {
int len = strlen(s + 1);
for (int i = 1; i <= len; i++)
h[i] = h[i - 1] * 233 + s[i] - 'a' + 1;
return h[len];
}
int main() {
n = read();
for (int i = 1; i <= n; i++) {
scanf(" %s", s + 1);
ans[i] = work(s);
}
sort(ans + 1, ans + 1 + n);
int sum = 1;
for (int i = 2; i <= n; i++)
if (ans[i] != ans[i - 1]) sum++;
cout << sum << '\n';
return 0;
}
P3378 【模板】堆
/*
Name: P3378 【模板】堆
Author: Loceaner
Date: 26/08/20 08:54
Description:
Debug: 操作 2 不需弹堆顶
*/
#include <queue>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int A = 1e5 + 11;
const int B = 1e6 + 11;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
inline int read() {
char c = getchar();
int x = 0, f = 1;
for ( ; !isdigit(c); c = getchar()) if (c == '-') f = -1;
for ( ; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48);
return x * f;
}
priority_queue <int, vector <int>, greater <int> > Q;
int main() {
int n = read();
for (int i = 1; i <= n; i++) {
int opt = read(), x;
if (opt == 1) {
x = read();
Q.push(x);
}
else if (opt == 2) cout << Q.top() << '\n';
else Q.pop();
}
}
P3383 【模板】线性筛素数
/*
Name: P3383 【模板】线性筛素数
Author: Loceaner
Date: 忘记了
Description:
Debug:
*/
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int A = 1e5 + 11;
const int B = 1e8 + 11;
inline int read() {
char c = getchar(); int x = 0, f = 1;
for( ; !isdigit(c); c = getchar()) if(c == '-') f = -1;
for( ; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + (c ^ 48);
return x * f;
}
int n, m, vis[B], prime[B], cnt;
//线性筛
void prepare() {
vis[0] = 1, vis[1] = 1;
for(int i = 2; i <= n; i++) {
if(!vis[i]) prime[++cnt] = i;
for(int j = 1; j <= cnt && i * prime[j] <= n; j++) {
vis[i * prime[j]] = 1;
if(i % prime[j] == 0) break;
}
}
}
int main() {
n = read(), m = read();
prepare();
while(m--) cout << prime[read()] << '\n';
return 0;
}
P3366 【模板】最小生成树
/*
Name: 最小生成树
Author: Loceaner
Date:
Description:
Debug: 估计再过几年肯定会发的
*/
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int A = 5e5 + 11;
const int B = 1e6 + 11;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
inline int read() {
char c = getchar();
int x = 0, f = 1;
for ( ; !isdigit(c); c = getchar()) if (c == '-') f = -1;
for ( ; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48);
return x * f;
}
int n, m, fa[A];
struct node { int x, y, val; } a[A];
bool cmp(node a, node b) {
return a.val < b.val;
}
int find(int x) {
return fa[x] == x ? x : fa[x] = find(fa[x]);
}
int main() {
n = read(), m = read();
for (int i = 1; i <= n; i++) fa[i] = i;
for (int i = 1; i <= m; i++)
a[i].x = read(), a[i].y = read(), a[i].val = read();
sort(a + 1, a + 1 + m, cmp);
int cnt = 0, ans = 0;
for (int i = 1; i <= m; i++) {
int x = a[i].x, y = a[i].y, val = a[i].val;
int dx = find(x), dy = find(y);
if (dx != dy) {
fa[dx] = dy;
cnt++;
ans += val;
}
if (cnt == n - 1) break;
}
cout << ans << '\n';
return 0;
}
P4779 【模板】单源最短路径(标准版)
/*
Name: P4779 【模板】单源最短路径(标准版)
Author: Loceaner
Date: 26/08/20 16:27
Description:
Debug: 有向图!!!
*/
#include <queue>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define int long long
using namespace std;
const int A = 5e5 + 11;
const int B = 1e6 + 11;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f3f3f3f3f;
inline int read() {
char c = getchar();
int x = 0, f = 1;
for ( ; !isdigit(c); c = getchar()) if (c == '-') f = -1;
for ( ; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48);
return x * f;
}
int n, m, s, dis[A], head[A], cnt, inq[A];
struct edge { int to, nxt, val; } e[A << 1];
inline void add(int from, int to, int val) {
e[++cnt].to = to;
e[cnt].val = val;
e[cnt].nxt = head[from];
head[from] = cnt;
}
struct node {
int x, y;
bool operator < (const node &b) const { /*这里的const不要忘记加*/
return y > b.y;
}
};
priority_queue <node> Q;
inline void Dij() {
memset(dis, inf, sizeof(dis));
dis[s] = 0, Q.push((node){s, 0}), inq[s] = 1;
while (!Q.empty()) {
int x = Q.top().x; Q.pop(), inq[x] = 0;
for (int i = head[x]; i; i = e[i].nxt) {
int to = e[i].to;
if (dis[to] > dis[x] + e[i].val) {
dis[to] = dis[x] + e[i].val;
if (!inq[to]) inq[to] = 1, Q.push((node){to, dis[to]});
}
}
}
return;
}
signed main() {
n = read(), m = read(), s = read();
for (int i = 1; i <= m; i++) {
int x = read(), y = read(), val = read();
add(x, y, val);
}
Dij();
for (int i = 1; i <= n; i++) cout << dis[i] << " ";
return 0;
}
P3390 【模板】矩阵快速幂
/*
Name: P3390 【模板】矩阵快速幂
Author: Loceaner
Date:
Description:
Debug: 不要在结构体里开数组开的太大
矩阵记得初始化
*/
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define int long long
using namespace std;
const int A = 1e2 + 11;
const int B = 1e6 + 11;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
inline int read() {
char c = getchar();
int x = 0, f = 1;
for ( ; !isdigit(c); c = getchar()) if (c == '-') f = -1;
for ( ; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48);
return x * f;
}
int n, k;
struct matrix {
int a[A][A];
matrix() { memset(a, 0, sizeof(a)); }
void init() { for (int i = 1; i <= n; i++) a[i][i] = 1; }
} a, ans;
matrix operator * (const matrix &x, const matrix &y) {
matrix ans;
for (int k = 1; k <= n; k++)
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
ans.a[i][j] = (ans.a[i][j] + x.a[i][k] * y.a[k][j] % mod) % mod;
return ans;
}
inline void power() {
while (k) {
if (k & 1) ans = ans * a;
a = a * a, k >>= 1;
}
}
signed main() {
n = read(), k = read();
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
a.a[i][j] = read();
ans.init();
power();
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++)
cout << ans.a[i][j] << " ";
puts("");
}
return 0;
}
P3375 【模板】KMP字符串匹配
/*
Name: P3375 【模板】KMP字符串匹配
Author: Loceaner
Date: 26/08/20 15:30
Description:
Debug:
*/
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int A = 1e6 + 11;
const int B = 1e6 + 11;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
inline int read() {
char c = getchar();
int x = 0, f = 1;
for ( ; !isdigit(c); c = getchar()) if (c == '-') f = -1;
for ( ; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48);
return x * f;
}
char t[A], p[A];
int lent, lenp, nxt[A];
int main() {
scanf("%s", t + 1), scanf("%s", p + 1);
lent = strlen(t + 1), lenp = strlen(p + 1);
for (int i = 2, j = 0; i <= lenp; i++) {
while (j && p[i] != p[j + 1]) j = nxt[j];
if (p[i] == p[j + 1]) j++;
nxt[i] = j;
}
for (int i = 1, j = 0; i <= lent; i++) {
while (j && t[i] != p[j + 1]) j = nxt[j];
if (t[i] == p[j + 1]) j++;
if (j == lenp) {
cout << i - lenp + 1 << "\n";
j = nxt[j];
}
}
for (int i = 1; i <= lenp; i++) cout << nxt[i] << " ";
}
转载不必联系作者,但请声明出处