HDU 5164 Matching on Array AC自动机套map
来源:互联网 发布:淘宝网店logo 编辑:程序博客网 时间:2024/05/01 16:11
传送门:点击打开链接
Matching on Array
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 259 Accepted Submission(s): 62
Problem Description
Alice has a sequence {a1,a2,…,an} with n positive integers. Bob has some positive integer sequences with different size. Alice wants to know the total occurrences of every sequence Bob has in Alice's sequence (the occurrences are allowed to overlap).
We say one sequenceB occurs in another sequence A if there is a contiguous subsequence of A that is the same as B after scaled by a positive real factor.
For exampleA={2,4,8,16},B={1,2} then B occurs three times in A . The occurrences are {2,4} ,{4,8} and {8,16} . And the factor is 0.5, 0.25 and 0.125.
We say one sequence
For example
Input
There are multiple test cases. The first line of input contains an integerT (1≤T≤30) , indicating the number of test cases. For each test case:
The first line contains two integern and m (1≤n,m≤100000) , indicating the size of Alice's sequence and the number of sequences Bob has. In the next line, there aren integers, a1,a2,…,an , indicating Alice's sequence. In the following m lines, each starts with an integer ki(1≤ki≤300000) - the size of the sequence. Then ki space separated positive integers follow, indicating the sequence.
The total sum ofki is less than or equal to 1000000. Other integers are between 1 and 10000, inclusive.
The first line contains two integer
The total sum of
Output
For each test case, output a single line with a single integer, indicating the total number of occurrences.
Sample Input
24 12 4 8 162 1 25 32 4 2 4 63 1 2 11 52 16 8
Sample Output
37HintFor sample 1, please refer to the problem description. For sample 2, {1, 2, 1} occurs only once, {5} occurs five times and {16, 8} occurs only once.
Source
BestCoder Round #27
题意:给出一个长度为n数组a。再给出m个长度为ki的数组bi。问每个b数组在a数组中出现的次数的和。定义b数组在a数组中出现为:b数组的每一个元素乘以一个系数之和,为a数组的一个子串。
思路:将数串转化为分数串,如A[I]=a[I+1]/a[I];再对每个b数组进行同样的操作,记为Bk。由于B数组的数目很多,用KMP对A和Bk会超时的。所以将Bk建成一个AC自动机,用map来映射分数到整数。
第一次写AC自动机,就用这题开刀……
代码:
#include<cstdio>#include<cstring>#define maxn 500000#include<queue>#include<map>#include<vector>#include<iostream>#define LL long longusing namespace std;struct fraction{int a, b;bool operator <(const fraction x) const{return a*x.b<x.a*b;}int gcd(int x, int y){if (y == 0) return x;else return gcd(y, x%y);}void simple(){int GCD = gcd(a, b);a = a / GCD;b = b / GCD;}};struct node{map<fraction, int> m;int val;int f, last;void init(){m.clear();val = f = last = 0;}}t[1200005];int cnt;void insert(fraction a[], int len){int u = 0;for (int i = 0; i<len; i++){map<fraction, int>::iterator it = t[u].m.find(a[i]);if (it == t[u].m.end()){t[u].m[a[i]] = ++cnt;t[cnt].init();}u = t[u].m[a[i]];}t[u].val++;}LL find(fraction T[], int n){LL ans = 0;int j = 0;for (int i = 0; i<n; i++){fraction c = T[i];while (j&&t[j].m.find(c) == t[j].m.end()) j = t[j].f;j = t[j].m[c];if (t[j].val) ans += t[j].val;int tmp = t[j].last;while (tmp){ans += t[tmp].val;tmp = t[tmp].last;}}return ans;}void getFail(){queue<int>q;t[0].f = 0;map<fraction, int>::iterator it;for (it = t[0].m.begin(); it != t[0].m.end(); it++){int u = it->second;t[u].f = 0;q.push(u);t[u].last = 0;}while (!q.empty()){int r = q.front();q.pop();for (it = t[r].m.begin(); it != t[r].m.end(); it++){int u = it->second;q.push(u);int v = t[r].f;while (v&&t[v].m.find(it->first) == t[v].m.end()) v = t[v].f;t[u].f = t[v].m[it->first];t[u].last = t[t[u].f].val ? t[u].f : t[t[u].f].last;}}}int a[100005], kk[3000005];fraction b[3000005], c[100005];int main(){int T;scanf("%d", &T);while (T--){cnt = 0;t[0].init();int n, m;scanf("%d %d", &n, &m);for (int i = 1; i <= n; i++){scanf("%d", &a[i]);}for (int i = 2; i <= n; i++){c[i - 1].a = a[i];c[i - 1].b = a[i - 1];c[i - 1].simple();}LL ans = 0;for (int i = 1; i <= m; i++){int k;scanf("%d", &k);if (k == 1){ans += n;scanf("%d", &k);continue;}for (int i = 1; i <= k; i++){scanf("%d", &kk[i]);}for (int i = 2; i <= k; i++){b[i - 1].a = kk[i];b[i - 1].b = kk[i - 1];b[i - 1].simple();}insert(b + 1, k - 1);}getFail();ans += find(c + 1, n - 1);printf("%I64d\n", ans);}return 0;}
0 0
- HDU 5164 Matching on Array AC自动机套map
- 【HDU】5164 Matching on Array 【AC自动机】
- hdu 5164 Matching on Array AC自动机
- hdu 5164 Matching on Array (用map实现的ac自动机)
- hdu 5164 Matching on Array (奇葩版ac自动机)
- hdu5164 Matching on Array map实现ac自动机
- UVA 11468 Substring AC 自动机套DP
- 【AC自动机】 HDOJ 5164 Matching on Arrayy
- HDU--3407[String-Matching Automata] AC自动机或kmp
- hdu—自动AC机(c++)
- hdu—自动AC机(python)
- Generating a string with elements from an array based on pattern matching
- HDU 自动刷题机 Auto AC (轻轻松松进入HDU首页)
- 【HDU】病毒侵袭持续中(AC自动机+map)
- HDU 3695 Computer Virus on Planet Pandora(AC自动机)
- HDU 3695 Computer Virus on Planet Pandora(AC自动机)
- hdu 3695 Computer Virus on Planet Pandora AC自动机
- hdu 3695 Computer Virus on Planet Pandora ac自动机
- 计算机图形学资料整合
- Unity3D技术之优化图形性能渲染统计信息窗口浅析
- PHP入门基础学习篇(二)
- 原发性癫痫的治疗方法 022uubgp
- linux模块编程(一)——加载你的模块
- HDU 5164 Matching on Array AC自动机套map
- Unity 5.0 中酷炫的新动画功能详解
- H264解码深度解析——DM8168 OMX从H264文件读取一帧数据(do chunking of h264)
- http://busumen.com/jryl/27846.html 023zcnds
- Unity 5中的全局光照使用详解
- android自定义listview滚动条的样式
- 退火车票票不收手续费的好方法
- 南京 024dokzy
- 2014年 总结