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. vue-router 实践

    1. vue-router2学习实践笔记 2. router.push() 3. 使用vue-router跳转页面

  2. CSS3实战-文字篇

    text-shadow的大作用 多颜色阴影效果,用逗号分隔text-shaodow即可. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Tr ...

  3. git提示error setting certificate verify locations解决办法

    先打开git bash窗口 执行命令: git config --system http.sslcainfo "C:\Program Files (x86)\git\bin\curl-ca- ...

  4. 【Swift】UIAlertController使用

    func clickButton1(){ 创建uialertcontroller var alertCtl : UIAlertController = UIAlertController(title: ...

  5. 【BZOJ】2208 [Jsoi2010]连通数

    [题意]给定n个点的有向图,求可达点对数(互相可达算两对,含自身).n<=2000. [算法]强连通分量(tarjan)+拓扑排序+状态压缩(bitset) [题解]这题可以说非常经典了. 1. ...

  6. js 禁用右键菜单、拖拽、选中、复制

    //禁用拖拽 document.ondragstart = function () { return false; }; /** * 禁用右键菜单 */ document.oncontextmenu ...

  7. PHP内存溢出 Allowed memory size of 解决办法

    PHP出现如下错误:Allowed memory size of  xxx bytes exhausted at xxx:xxx (tried to allocate xxx bytes)    关于 ...

  8. Strusts2笔记4--类型转换器

    类型转换器: Struts2默认情况下可以将表单中输入的文本数据转换为相应的基本数据类型.这个功能的实现,主要是由于Struts2内置了类型转换器.这些转换器在struts-default.xml中可 ...

  9. 查看Oracle数据库中的所有用户名

    select username from dba_users"

  10. C++面试常见问题

    转载:https://zhuanlan.zhihu.com/p/34016871?utm_source=qq&utm_medium=social 1.在C++ 程序中调用被C 编译器编译后的函 ...