zoj 3891 K-hash(后缀自动机)
来源:互联网 发布:mac地址ip地质作用 编辑:程序博客网 时间:2024/06/06 09:54
题目链接:zoj 3891 K-hash
代码
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;typedef long long ll;const int maxn = 50005;const int maxm = 35;const int SIGMA_SIZE = 10;struct SAM { int sz, last; int g[maxn<<1][SIGMA_SIZE], pre[maxn<<1], step[maxn<<1]; int pos[maxn<<1], right[maxn<<1], cnt[maxn<<1]; void newNode(int s) { step[++sz] = s; pre[sz] = 0; memset(g[sz], 0, sizeof(g[sz])); } int idx(char ch) { return ch -'0'; } void init() { sz = 0, last = 1; newNode(0); } void insert(char ch); void get_tuopu(); void get_right(char* str); int dp[maxn<<1][maxm]; void solve (int k) { memset(dp, 0, sizeof(dp)); dp[1][0] = 1; for (int i = 1; i <= sz; i++) { int u = pos[i]; for (int j = 0; j < SIGMA_SIZE; j++) { if (u == 1 && j == 0) continue; int v = g[u][j]; if (v == 0) continue; for (int x = 0; x < k; x++) { int y = (x * 10 + j) % k; dp[v][y] += dp[u][x]; } } } int ans[maxm]; memset(ans, 0, sizeof(ans)); for (int i = 2; i <= sz; i++) { for (int j = 0; j < k; j++) ans[j] += dp[i][j]; } if (g[1][0]) ans[0]++; for (int i = 0; i < k; i++) printf("%d%c", ans[i], i == k-1 ? '\n' : ' '); }}SA;int K;char str[maxn];int main () { while (scanf("%s%d", str, &K) == 2) { int n = strlen(str); SA.init(); for (int i = 0; i < n; i++) SA.insert(str[i]); SA.get_tuopu(); SA.solve(K); } return 0;}void SAM::insert(char ch) { newNode(step[last] + 1); int v = idx(ch), p = last, np = sz; while (p && !g[p][v]) { g[p][v] = np; p = pre[p]; } if (p) { int q = g[p][v]; if (step[q] == step[p] + 1) pre[np] = q; else { newNode(step[p] + 1); int nq = sz; for (int j = 0; j < SIGMA_SIZE; j++) g[nq][j] = g[q][j]; pre[nq] = pre[q]; pre[np] = pre[q] = nq; while (p && g[p][v] == q) { g[p][v] = nq; p = pre[p]; } } } else pre[np] = 1; last = np;}void SAM::get_tuopu() { for (int i = 0; i <= sz; i++) cnt[i] = 0; for (int i = 1; i <= sz; i++) cnt[step[i]]++; for (int i = 1; i <= sz; i++) cnt[i] += cnt[i-1]; for (int i = 1; i <= sz; i++) pos[cnt[step[i]]--] = i; }void SAM::get_right(char* str) { int p = 1, n = strlen(str); for (int i = 0; i <= sz; i++) right[i] = 0; for (int i = 0; i < n; i++) { int v = idx(str[i]); p = g[p][v]; right[p]++; } for (int i = sz; i; i--) { int u = pos[i]; right[pre[u]] += right[u]; }}
0 0
- zoj 3891 K-hash(后缀自动机)
- ZOJ 3891 K-hash 后缀自动机
- ZOJ3891 K-hash 后缀自动机SAM
- hdu4641-K-string(后缀自动机)
- hdu 4641 K-string(后缀自动机 + 并查集)
- ZOJ Monthly, July 2015 K-hash 后缀数组去重做法
- 【后缀自动机】[HDU 4641]K-string
- 【后缀自动机】【SAM】【自动机】【数据结构】后缀自动机理解(入门)
- [后缀自动机 模板题 || 字符串Hash] HDU 4622 Reincarnation
- HDU 6208 ac自动机 hash sam 后缀数组
- 后缀自动机(输出字典序为K的子串)spoj7258
- SPOJ 题目7528 SUBLEX - Lexicographical Substring Search(后缀自动机求排名k的子串)
- 后缀自动机(SAM)学习指南
- HDU 4622(后缀自动机)
- HDU 4436(后缀自动机)
- POJ 3415(后缀自动机)
- SPOJ LCS(后缀自动机)
- SPOJ LCS2(后缀自动机)
- [hadoop2.7.1]I/O之SequenceFile最新API编程实例(写入、读取)
- Xcode快捷键
- hdu--4455+ Substrings+2012杭州区域赛C题+DP
- Swift-- 方法
- [TwistedFate]属性property
- zoj 3891 K-hash(后缀自动机)
- hdoj2053(switch game
- Intent 传数据
- POJ 3009 dfs暴搜
- 外键约束和级联操作
- Java学习笔记(1):重载方法
- 【编程开发】opencv实现对Mat中某一列或某一行的元素进行normalization
- READING NOTE: Two-Stream Convolutional Networks for Action Recognition in Videos
- 用于Android ART虚拟机JNI调用的NativeBridge介绍