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. 博世传感器调试笔记(二)加速度及陀螺仪传感器BMI160

    一.功能参数简介bosch Sensortec公司推出的最新BMI160惯性测量单元将最顶尖的16位3轴超低重力加速度计和超低功耗3轴陀螺仪集成于单一封装.MI160采用14管脚LGA封装,尺寸为2. ...

  2. Python设计模式(六大)

    1.外观模式(Facade) 一层一层向上封装,灵活性会降低,功能完成度高,和python的模块比较像,但对于封装好了的类,将会变得很简单,简洁. 2.六大设计原则 单一职责原则 (Single Re ...

  3. TOMCAT添加管理用户认证

    添加配置文件 --原配置文件: # tail -5 /usr/local/tomcat/conf/tomcat-users.xml <user username="tomcat&quo ...

  4. grpc-gateway:grpc转换为http协议对外提供服务

    我所在公司的项目是采用基于Restful的微服务架构,随着微服务之间的沟通越来越频繁,就希望可以做成用rpc来做内部的通讯,对外依然用Restful.于是就想到了google的grpc. 使用grpc ...

  5. Centos7系统中安装Nginx1.8.0

    Nginx的安装 tar -zxvf nginx-1.8.0.tar.gz cd nginx-1.8.0 ./configure make make install /usr/local/nginx/ ...

  6. Docker 启动Centos

    docker run -d -e "container=docker" --privileged=true -v /sys/fs/cgroup:/sys/fs/cgroup --n ...

  7. java学习第01天(程序开发体验)

    1.基本写法 class Demo{ public static void main(String[] args){ System.out.print("Hello World") ...

  8. HDU 1176 排列2 全排列

    解题报告:给出四个数,然后要你把这四个数组合成的各不相同的四位数按照从小到大的顺序输出来,然后如果最高位是0的话不能输出来,还有最高位是数字如果一样的话,则放在同一行输出. 本来是个比较简单的生成全排 ...

  9. 13、Math类简介

    Math类概述 在java.lang包下,有个Math类,这个类包含用于执行基本数学运算的方法,如四舍五入,开方等等. package com.sutaoyu.usually_class; publi ...

  10. Linux基础-vim编辑器

    使用vi编辑器编辑文件/1.txt进入编辑模式写入内容“hello world” 命令行模式输入i,进入编辑模式 写入HelloWorld,按ESC进入命令行模式,输入:进入扩展模式输入wq保存退出 ...