C - Tachibana Kanade's Tofu

思路:把 n 个串丢进AC自动机中,然后dp就好啦。 我的代码居然是在CF上跑最快的。。

#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ull unsigned long long using namespace std; const int N = + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + ;
const double eps = 1e-;
const double PI = acos(-); int n, m, len, k, tmp, v[];
vector<int> L, R;
vector<int> s; inline void add(int &a, int b) {
a += b; if(a >= mod) a -= mod;
} struct Ac {
int ch[][], val[], f[], tot, sz;
int dp[][][][], cur, pre;
void init(int _sz) {tot = ; sz = _sz;}
inline int newNode() {
tot++; f[tot] = ; val[tot] = ;
memset(ch[tot], , sizeof(ch[tot]));
return tot;
}
inline int idx(int c) {return c;}
void addStr(vector<int> &s, int cost) {
int u = ;
for(int i = ; i < s.size(); i++) {
int c = idx(s[i]);
if(!ch[u][c]) ch[u][c] = newNode();
u = ch[u][c];
}
val[u] += cost;
}
void build() {
queue<int> que;
for(int c = ; c < sz; c++) {
int v = ch[][c];
if(!v) ch[][c] = ;
else f[v] = , que.push(v);
}
while(!que.empty()) {
int u = que.front(); que.pop();
val[u] += val[f[u]];
for(int c = ; c < sz; c++) {
int v = ch[u][c];
if(!v) ch[u][c] = ch[f[u]][c];
else f[v] = ch[f[u]][c], que.push(v);
}
}
}
int solve(vector<int> &str) {
int n = str.size(), ans = ;
cur = , pre = ;
memset(dp[cur], , sizeof(dp[cur]));
for(int z = ; z <= str[]; z++) {
int v = ch[][z];
if(val[v] <= k) add(dp[cur][v][val[v]][z == str[]], );
}
for(int i = ; i < n; i++) {
swap(cur, pre);
memset(dp[cur], , sizeof(dp[cur]));
for(int z = ; z < m; z++) {
int v = ch[][z];
if(val[v] <= k) add(dp[cur][v][val[v]][], );
}
for(int u = ; u <= tot; u++) {
for(int s = ; s <= k; s++) {
if(dp[pre][u][s][]) {
for(int z = ; z < m; z++) {
int v = ch[u][z];
if(s+val[v] <= k) add(dp[cur][v][val[v]+s][], dp[pre][u][s][]);
}
} if(dp[pre][u][s][]) {
for(int z = ; z <= str[i]; z++) {
int v = ch[u][z];
if(s+val[v] <= k) add(dp[cur][v][val[v]+s][z==str[i]], dp[pre][u][s][]);
}
}
}
}
}
for(int u = ; u <= tot; u++)
for(int j = ; j <= k; j++)
add(ans, dp[cur][u][j][]), add(ans, dp[cur][u][j][]);
return ans;
}
} ac; int main() {
scanf("%d%d%d", &n, &m, &k);
ac.init(m);
scanf("%d", &len); L.resize(len);
for(int i = ; i < len; i++) scanf("%d", &L[i]);
scanf("%d", &len); R.resize(len);
for(int i = ; i < len; i++) scanf("%d", &R[i]);
for(int i = ; i < n; i++) {
scanf("%d", &len); s.resize(len);
for(int j = ; j < len; j++) scanf("%d", &s[j]);
int cost; scanf("%d", &cost);
ac.addStr(s, cost);
for(int p = ; p+s.size() <= L.size(); p++) {
bool flag = true;
for(int q = ; q < s.size(); q++) {
if(s[q] != L[q+p]) {
flag = false;
break;
}
}
if(flag) tmp += cost;
}
}
ac.build();
int ans = (ac.solve(R) - ac.solve(L) + (tmp <= k) + mod) % mod;
printf("%d\n", ans);
return ;
}
/*
*/

Codeforces Round #248 (Div. 1) C - Tachibana Kanade's Tofu AC自动机的更多相关文章

  1. Codeforces Round #248 (Div. 2) C. Ryouko's Memory Note

    题目链接:http://codeforces.com/contest/433/problem/C 思路:可以想到,要把某一个数字变成他的相邻中的数字的其中一个,这样总和才会减少,于是我们可以把每个数的 ...

  2. Codeforces Round #248 (Div. 2)C 题

    题目:http://codeforces.com/contest/433/problem/C 没想到做法就各种纠结, 今天做的都快疯掉了, 太弱了, 等题解一出,就各种恍然大悟 不应该不应该 正文: ...

  3. Codeforces Round #248 (Div. 2) (ABCD解决问题的方法)

    比赛链接:http://codeforces.com/contest/433 A. Kitahara Haruki's Gift time limit per test:1 second memory ...

  4. Codeforces Round #248 (Div. 1) B. Nanami's Digital Board 暴力 前缀和

    B. Nanami's Digital Board 题目连接: http://www.codeforces.com/contest/434/problem/B Description Nanami i ...

  5. Codeforces Round #248 (Div. 1) A. Ryouko's Memory Note 水题

    A. Ryouko's Memory Note 题目连接: http://www.codeforces.com/contest/434/problem/A Description Ryouko is ...

  6. Codeforces Round #248 (Div. 2) B称号 【数据结构:树状数组】

    主题链接:http://codeforces.com/contest/433/problem/B 题目大意:给n(1 ≤ n ≤ 105)个数据(1 ≤ vi ≤ 109),当中有m(1 ≤ m ≤  ...

  7. Codeforces Round #248 (Div. 2) B. Kuriyama Mirai's Stones

    题目简单描述就是求数组中[l,r]区间的和 #include <iostream> #include <vector> #include <string> #inc ...

  8. Codeforces Round #248 (Div. 2) A. Kitahara Haruki's Gift

    解决思路是统计100的个数为cnt1,200的个数为cnt2 则 cnt1    cnt2 奇数      奇数 奇数      偶数 偶数      奇数 偶数     偶数 当cnt1为奇数时一定 ...

  9. Codeforces Round #248 (Div. 2) C. Ryouko's Memory Note (vector 替换)

    题目链接 题意:给m个数字, 这些数字都不大于 n,  sum的值为相邻两个数字 差的绝对值.求这n个数字里把一个数字 用 其中另一个数字代替以后, 最小的sum值. 分析:刚开始以为两个for 最坏 ...

随机推荐

  1. 数据分析与展示---Pandas库入门

    简介 一:Pandas库的介绍 二:Pandas库的Series类型 (一)索引 (1)自动索引 (2)自定义索引 (二)Series类型创建 (1)列表创建 (2)标量值创建 (3)字典类型创建(将 ...

  2. 多页面应用 VS 单页面应用

    多页面应用 每一次页面跳转,后端都会返回一个新的HTML文件, 优点:首屏时间快(只经历了一个HTTP请求),SEO效果好 缺点:页面切换慢 单页面应用 进行页面之间跳转时,并不去加载HTML文件,而 ...

  3. oracle表结构和数据导出时的一些勾选项说明

    使用pl/sql developer导出oracle数据库的表结构和表数据时,有一些勾选项供用户选择,需要用户根据实际情况进行勾选或取消. 导出方法如下:一.只导出表结构1.使用pl/sql deve ...

  4. Java并发编程原理与实战二十二:Condition的使用

    Condition的使用 Condition用于实现条件锁,可以唤醒指定的阻塞线程.下面来实现一个多线程顺序打印a,b,c的例子. 先来看用wait和notify的实现: public class D ...

  5. 【leetcode 简单】 第六十四题 翻转二叉树

    翻转一棵二叉树. 示例: 输入: 4 / \ 2 7 / \ / \ 1 3 6 9 输出: 4 / \ 7 2 / \ / \ 9 6 3 1 备注: 这个问题是受到 Max Howell的 原问题 ...

  6. CSV转excel方法

    步骤一:新建excel文件,数据—>自文本,导入文件 步骤二:选择分隔符,下一步 步骤三:勾选分隔符符合,下一步 步骤四:直接下一步,可在预览里看到格式 步骤五:点击确定,等待数据导入

  7. Linux基础-简单的进程操作

    任务:查找一个名为firewall的进程,并且将其强制杀死 首先要使用ps -aux来查询firewall的进程ID(|grep firewall) 这样我们就得到了firewall的进程ID是653 ...

  8. python3学习笔记.4.turtle绘图

    先放上参考 https://docs.python.org/3/library/turtle.html //********************************************** ...

  9. hdu 1251 统计难题(字典树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1251 统计难题 Time Limit: 4000/2000 MS (Java/Others)    M ...

  10. DirectFB简介以及移植[一]【转】

    转自:https://blog.csdn.net/wavemcu/article/details/39251805 ****************************************** ...