cf244D. Match & Catch 字符串hash (模板)或 后缀数组。。。
能够用各种方法做。字符串hash。后缀数组,dp。拓展kmp,字典树。。
。
字符串hash(模板)
http://blog.csdn.net/gdujian0119/article/details/6777239
- BKDR Hash Function :
- // BKDR Hash Function
- unsigned int BKDRHash(char *str)
- {
- unsigned int seed = 131; // 31 131 1313 13131 131313 etc..
- unsigned int hash = 0;
- while (*str)
- {
- hash = hash * seed + (*str++);
- }
- return (hash & 0x7FFFFFFF);
- }
本题hash解法 n^2
- #define ULL unsigned long long
- const int maxn = 5111 * 2;
- const int HASH = 10007;
- struct HashMap{
- int head[HASH];
- int next[maxn];
- ULL state[maxn];
- int num_s1[maxn];
- int num_s2[maxn];
- int sz;
- void init()
- {
- sz = 0;
- memset(head, -1, sizeof(head));
- }
- int insert(ULL val, bool info)
- {
- int h = val % HASH;
- for (int i = head[h]; i != -1; i = next[i])
- {
- if (val == state[i])
- {
- if (info) num_s1[i]++;
- else num_s2[i]++;
- return 1;///存在
- }
- }
- num_s1[sz] = 0;
- num_s2[sz] = 0;
- state[sz] = val;
- next[sz] = head[h];
- if (info) num_s1[sz]++;
- else num_s2[sz]++;
- head[h] = sz++;
- return 0;///不存在
- }
- bool check()
- {
- for (int i = 0; i < sz; i++)
- {
- if (num_s1[i] == num_s2[i] && num_s1[i] == 1)
- return true;
- }
- return false;
- }
- }H;
- const int SEED = 13331;
- ULL P[maxn];
- ULL s1[maxn], s2[maxn];
- char sa[maxn], sb[maxn];
- void hash_pre(ULL P[])
- {
- P[0] = 1;
- for (int i = 1; i <= maxn; i++)
- P[i] = P[i - 1] * SEED;
- }
- void hash_init(ULL s1[], char sa[])///sa[],下标从0開始;相应是s1[]的值得下标从1開始
- {
- int n = strlen(sa);
- s1[0] = 0;
- for (int i = 1; i <= n; i++)
- s1[i] = s1[i - 1] * SEED + sa[i - 1];
- }
- ULL getSeg(ULL s1[], int l, int r)///求s1[]的下标区间的hash值
- {
- return s1[r] - s1[l - 1] * P[r - l + 1];
- }
- int main()
- {
- hash_pre(P);
- RS(sa); RS(sb);
- int n = strlen(sa), m = strlen(sb);
- hash_init(s1, sa);
- hash_init(s2, sb);
- int fla = 0;
- int mn = min(n, m);
- for (int i = 1; i <= mn; i++)
- {
- H.init();
- for (int j = i; j <= n; j++)
- {
- H.insert(getSeg(s1, j - i + 1, j), 0);
- }
- for (int j = i; j <= m; j++)
- {
- H.insert(getSeg(s2, j - i + 1, j), 1);
- }
- if (H.check())
- {
- printf("%d\n", i);
- fla = 1;
- break;
- }
- }
- if (!fla) puts("-1");
- return 0;
- }
后缀数组:
- const int MAXN = 5111 * 2;
- const int INF = 0x3f3f3f3f;
- int wa[MAXN], wb[MAXN], wv[MAXN], wn[MAXN];
- //char r[MAXN];
- int a[MAXN], sa[MAXN], rank[MAXN], height[MAXN];
- int cmp(int *r, int a,int b, int k)
- {
- return r[a] == r[b] && r[a + k] == r[b + k];
- }
- void build_sa(int *r, int *sa, int n,int m)
- {
- int i,j, p;
- int *x = wa, *y = wb, *t;
- for (int i= 0; i < m; i++) wn[i] = 0;
- for (int i= 0; i < n; i++) wn[x[i] = r[i]]++;
- for (int i = 1; i < m; i++) wn[i] += wn[i - 1];
- for (int i = n - 1; i >= 0; i--) sa[--wn[x[i]]] = i;
- for (p = 1, j = 1; p < n; j <<= 1, m = p)
- {
- for (p = 0, i = n - j; i < n; i++) y[p++] = i;
- for (i = 0; i < n; i++) if (sa[i] >= j) y[p++] = sa[i] - j;
- for (i = 0; i < m; i++) wn[i] = 0;
- for (i = 0; i < n; i++) wn[wv[i] = x[y[i]]]++;
- for (i = 1; i < m; i++) wn[i] += wn[i - 1];
- for (i = n - 1; i >= 0; i--) sa[--wn[wv[i]]] = y[i];
- t = x; x = y; y = t;
- x[sa[0]] = 0; p = 1;
- for (i = 1; i < n; i++)
- x[sa[i]] = cmp(y, sa[i - 1], sa[i], j) ? p - 1 : p++;
- }
- }
- void getHeight(int *r, int *sa, int n)
- {
- int i, j, k = 0;
- for (i = 1; i <= n; i++)
- {
- rank[sa[i]] = i;
- height[i] = 0;
- }
- for (i = 0;i < n; i++)
- {
- if (k) k--;
- j = sa[rank[i] - 1];
- while (r[i + k] == r[j + k]) k++;
- height[rank[i]] = k;
- }
- }
- char ca[MAXN], cb[MAXN];
- void solve(int n)
- {
- height[n + 1] = 0;
- int ans = INF;
- int l = strlen(ca);
- for (int i = 1; i <= n; i++)
- {
- if (!height[i]) continue;
- if (sa[i] < l && sa[i - 1] < l) continue;
- if (sa[i] >= l && sa[i - 1] >= l) continue;
- int pre = height[i - 1] + 1;
- int next = height[i + 1] + 1;
- if (height[i] >= max(pre, next))
- {
- ans = min(ans, max(pre, next));
- }
- }
- if (ans == INF) puts("-1");
- else printf("%d\n", ans);
- }
- int main()
- {
- int t, n, m;
- ///n, m
- n = 0;
- m = 256;
- RS(ca);
- int l = strlen(ca);
- REP(j, l) a[n++] = (int)ca[j];
- a[n++] = m++;
- RS(cb);
- l = strlen(cb);
- REP(j, l) a[n++] = (int)cb[j];
- a[n++] = m++;
- a[--n] = 0; --m;
- build_sa(a, sa, n + 1, m);
- getHeight(a, sa, n);
- solve(n);
- return 0;
- }
- /*
- 0 8 0
- 1 6 0
- 2 4 2
- 3 2 4
- 4 0 6
- 5 7 0
- 6 5 1
- 7 3 3
- 8 1 5
- ^^^^^^^^^^^^^^
- 0 4
- 1 8
- 2 3
- 3 7
- 4 2
- 5 6
- 6 1
- 7 5
- 8 0
- ^^^^^^^^^^^^^^
- */
cf244D. Match & Catch 字符串hash (模板)或 后缀数组。。。的更多相关文章
- AcWing:139. 回文子串的最大长度(字符串Hash + 前缀和 + 后缀和 + 二分)
如果一个字符串正着读和倒着读是一样的,则称它是回文的. 给定一个长度为N的字符串S,求他的最长回文子串的长度是多少. 输入格式 输入将包含最多30个测试用例,每个测试用例占一行,以最多1000000个 ...
- hdu 4622 Reincarnation 字符串hash 模板题
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4622 题意:给定一个长度不超过2000的字符串,之后有不超过1e5次的区间查询,输出每次查询区间中不同 ...
- acdream1116 Gao the string!(hash二分 or 后缀数组)
问题套了一个斐波那契数,归根结底就是要求对于所有后缀s[i...n-1],所有前缀在其中出现的总次数.我一开始做的时候想了好久,后来看了别人的解法才恍然大悟.对于一个后缀来说 s[i...n-1]来说 ...
- 字符串HASH模板
//注意MAXN是最大不同的HASH个数,一般HASHN是MAXN的两倍左右,MAXLEN表示字符串的最大长度 //K表示正确率,越大正确率越高,当时也越费空间,费时间. //使用前注意初始化hash ...
- hdu-4080 Stammering Aliens 字符串hash 模板题
http://acm.hdu.edu.cn/showproblem.php?pid=4080 求出现次数大于等于n的最长串. #include<iostream> #include< ...
- 字符串hash 模板
typedef long long ll; typedef unsigned long long ull; #define maxn 1005 struct My_Hash { ull ; ull p ...
- 字符串(马拉车算法,后缀数组,稀疏表):BZOJ 3676 [Apio2014]回文串
Description 考虑一个只包含小写拉丁字母的字符串s.我们定义s的一个子串t的“出 现值”为t在s中的出现次数乘以t的长度.请你求出s的所有回文子串中的最 大出现值. Input 输入只有一行 ...
- 字符串HASH 学习总结 && 模板
一.字符串HASH模板 取自挑战程序设计竞赛(第2版) </pre><pre code_snippet_id="446698" snippet_file_nam ...
- poj 3461 字符串单串匹配--KMP或者字符串HASH
http://poj.org/problem?id=3461 先来一发KMP算法: #include <cstdio> #include <cstring> #include ...
随机推荐
- @NotNull、@NotEmpty、@NotBlank的区别
Spring中@NotNull.@NotEmpty.@NotBlank的区别@NotNull:用于基本数据类型@NotEmpty:用于集合类@NotBlank:用于String上面
- React之小知识点总结
总结react中常常被忽略的小知识点 1)即使state里设置成和之前的值一样,render也会重新渲染 2)父组件传给子组件的属性(props是只读的,在子组件中已在this.state里将属性赋值 ...
- 活字格Web应用平台学习笔记1 - 下载安装,ready go
今年有一个很重要的职业目标,就是好好学习活字格这个神器,争取在这两个月拿到活字格初级工程师的认证证书.给自己加个油,今天开始好好学习,好好做笔记. 第一步,下载安装 先去活字格官网:http://ww ...
- Linux CentOS 6.5 + Apache + Mariadb + PHP环境搭建
Web自动化测试-服务端测试环境部署 by:授客 目录 一. 二. 三. 四. 五. 六. 七. 八. 九. 十. 操作系统环境:CentOS 6.5-x86_64 下载地址:http://www.c ...
- Java 装饰器模式详解
转载请注明出处:http://blog.csdn.net/zhaoyanjun6/article/details/56488020 前言 在上面的几篇文章中,着重介绍了Java 中常见的 IO 相关知 ...
- LeetCode题解之Binary Tree Tilt
1.题目描述 2.分析 利用递归实现. 3.代码 int findTilt(TreeNode* root) { if (root == NULL) ; ; nodesTilt(root,ans); r ...
- mysql中删除同一行会经常出现死锁?太可怕了
之前有一个同事问到我,为什么多个线程同时去做删除同一行数据的操作,老是报死锁,在线上已经出现好多次了,我问了他几个问题: 1. 是不是在一个事务中做了好几件事情? 答:不是,只做一个删除 ...
- MySQL 支持utf8mb4
utf8mb4 utf8mb3 utf8 Refer to The utf8mb4 Character Set The utf8 Character Set (Alias for utf8mb3) M ...
- od 转储 二进制文件常用命令
od : NAME od - dump files in octal and other formats 常用命令: ➜ Downloads od -t x1 -Ax /etc/ld.so.cach ...
- tikv性能参数调优
tiKV 最底层使用的是 RocksDB(tidb3.0版本中将使用tian存储引擎) 做为持久化存储,所以 TiKV 的很多性能相关的参数都是与 RocksDB 相关的.TiKV 使用了两个 Roc ...