传送门:点击打开链接
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
{a
1
,a
2
,…,a
n
}
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 sequence B
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 example A={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 B
For example A={2,4,8,16},B={1,2}
Input
There are multiple test cases. The first line of input contains an integer
T (1≤T≤30)
, indicating the number of test cases. For each test case:
The first line contains two integer n
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 are
n
integers,
a
1
,a
2
,…,a
n![]()
, indicating Alice's sequence. In the following
m
lines, each starts with an integer
k
i
(1≤k
i
≤300000)
- the size of the sequence. Then
k
i![]()
space separated positive integers follow, indicating the sequence.
The total sum of k
i![]()
is less than or equal to 1000000. Other integers are between 1 and 10000, inclusive.
The first line contains two integer n
The total sum of k
Output
For each test case, output a single line with a single integer, indicating the total number of occurrences.
Sample Input
2 4 1 2 4 8 16 2 1 2 5 3 2 4 2 4 6 3 1 2 1 1 5 2 16 8
Sample Output
3 7HintFor 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
题意:给出一个长度为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 long
using 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;
}