bzoj 1559 AC自动机 + dp
思路:直接在状态图上跑dp,最后枚举一下42种一下的。。 这个枚举有点恶心。
#include<bits/stdc++.h>
#define LL long long
#define ll long long
#define fi first
#define se second
#define mk make_pair
#define PII pair<int, int>
#define y1 skldjfskldjg
#define y2 skldfjsklejg using namespace std; const int N = + ;
const int M = 1e7 + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = ; int n, m, up, top, id[N], bord[N][N];
string s[], t[];
bool vis[N]; bool cmp(const string &a, const string &b) {
return a.size() > b.size();
}
bool check(const string &a, const string &b) {
for(int i = ; i < a.size(); i++) {
if(a.substr(i, b.size()) == b) return true;
}
return false;
} int cal(const string &a, const string &b) {
int l1 = a.size(), l2 = b.size();
for(int i = min(l1, l2) - ; i >= ; i--) {
if(a.substr(l1 - i, i) == b.substr(, i)) return i;
}
return ;
} struct Ac {
int ch[N][], val[N], f[N], tot, sz, depth[N];
LL dp[][][ << ];
Ac(int sz) {this->sz = sz;}
void init() {tot = ;};
int newNode() {
tot++; f[tot] = ; val[tot] = ;
memset(ch[tot], , sizeof(ch[tot]));
return tot;
}
inline int idx(char c) {return c - 'a';}
void addStr(string &s, int id) {
int u = ;
for(int i = ; i < s.size(); i++) {
int c = idx(s[i]);
if(!ch[u][c]) ch[u][c] = newNode(), depth[ch[u][c]] = depth[u] + ;
u = ch[u][c];
}
val[u] |= << id;
}
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);
}
}
} void solve() {
dp[][][] = ;
for(int k = ; k < n; k++) {
for(int u = ; u <= tot; u++) {
for(int c = ; c < sz; c++) {
int v = ch[u][c];
for(int s = ; s <= up; s++) {
dp[k + ][v][s | val[v]] += dp[k][u][s];
}
}
}
} LL ans = ;
for(int u = ; u <= tot; u++)
ans += dp[n][u][up];
cout << ans << '\n'; if(ans <= ) {
memset(vis, true, sizeof(vis));
sort(s, s + m, cmp);
for(int i = ; i < m; i++)
for(int j = i + ; j < m; j++)
if(check(s[i], s[j])) vis[j] = false; for(int i = ; i < m; i++)
if(vis[i]) id[top] = top, t[top++] = s[i]; for(int i = ; i < top; i++)
for(int j = ; j < top; j++)
if(i != j) bord[i][j] = cal(t[i], t[j]); vector<string> vec;
do {
string ret = t[id[]];
for(int i = ; i < top; i++) {
int len = bord[id[i - ]][id[i]];
ret += t[id[i]].substr(len, );
}
if(ret.size() == n) vec.push_back(ret);
} while(next_permutation(id, id + top)); sort(vec.begin(), vec.end());
for(int i = ; i < vec.size(); i++)
cout << vec[i] << '\n';
}
}
} ac(); int main() {
ac.init();
cin >> n >> m;
for(int i = ; i < m; i++) {
cin >> s[i];
ac.addStr(s[i], i);
}
up = ( << m) - ;
ac.build();
ac.solve();
return ;
} /*
*/
bzoj 1559 AC自动机 + dp的更多相关文章
- bzoj 1030 AC自动机+dp
代码: //先把给的单词建AC自动机并且转移fail,然后d[i][j]表示构造的文章到第i位时处在字典树的第j个节点的不包含单词的数量,最后用总的数量26^m //-d[m][0~sz]即可.其中不 ...
- [BZOJ 1559] [JSOI2009] 密码 【AC自动机DP】
题目链接:BZOJ - 1559 题目分析 将给定的串建成AC自动机,然后在AC自动机上状压DP. 转移边就是Father -> Son 或 Now -> Fail. f[i][j][k] ...
- [BZOJ 3530] [Sdoi2014] 数数 【AC自动机+DP】
题目链接:BZOJ - 3530 题目分析 明显是 AC自动机+DP,外加数位统计. WZY 神犇出的良心省选题,然而去年我太弱..比现在还要弱得多.. 其实现在做这道题,我自己也没想出完整解法.. ...
- bzoj 1030 [JSOI2007]文本生成器(AC自动机+DP)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1030 [题意] 给n个小串,随机构造一个长为m的大串,一个串合法当且仅当包含一个或多个 ...
- 【BZOJ】4861: [Beijing2017]魔法咒语 AC自动机+DP+矩阵快速幂
[题意]给定n个原串和m个禁忌串,要求用原串集合能拼出的不含禁忌串且长度为L的串的数量.(60%)n,m<=50,L<=100.(40%)原串长度为1或2,L<=10^18. [算法 ...
- POJ1625 Censored!(AC自动机+DP)
题目问长度m不包含一些不文明单词的字符串有多少个. 依然是水水的AC自动机+DP..做完后发现居然和POJ2778是一道题,回过头来看都水水的... dp[i][j]表示长度i(在自动机转移i步)且后 ...
- HDU2296 Ring(AC自动机+DP)
题目是给几个带有价值的单词.而一个字符串的价值是 各单词在它里面出现次数*单词价值 的和,问长度不超过n的最大价值的字符串是什么? 依然是入门的AC自动机+DP题..不一样的是这题要输出具体方案,加个 ...
- HDU2457 DNA repair(AC自动机+DP)
题目一串DNA最少需要修改几个基因使其不包含一些致病DNA片段. 这道题应该是AC自动机+DP的入门题了,有POJ2778基础不难写出来. dp[i][j]表示原DNA前i位(在AC自动机上转移i步) ...
- hdu 4117 GRE Words AC自动机DP
题目:给出n个串,问最多能够选出多少个串,使得前面串是后面串的子串(按照输入顺序) 分析: 其实这题是这题SPOJ 7758. Growing Strings AC自动机DP的进阶版本,主题思想差不多 ...
随机推荐
- POJ---3463 Sightseeing 记录最短路和次短路的条数
Sightseeing Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9247 Accepted: 3242 Descr ...
- bfs和优先队列求数据三角形最大值
此题用深搜很快就解决,我用宽度搜索和优先队列仅仅是为了练习他们的用法:深搜法在注释内: 1 #include<iostream> #include<cstring> #incl ...
- 基于Bootstrap的遮罩层,带有加载提示
1.body中的html <div class="modal fade" id="loadingModal"> <div style=&quo ...
- [Jenkins 新插件] 兼容阿里开发手册 (P3C) 的火线插件安装使用教程
一.前言 火线(Fireline)的Jenkins官方插件已经上线,目前火线不仅能检查出安卓代码中的安全类问题和内存泄露问题,还兼容了阿里开源的Java开发规约(P3C项目),本文将以教程的形式帮助大 ...
- defer与async的区别
当浏览器碰到 script 脚本的时候: <script src="script.js"></script> 没有 defer 或 async,浏览器会立即 ...
- poi-对于word的操作(二)
poi对于word文本的底纹和下划线的样式的展现 package poi.test; import java.io.FileOutputStream; import java.math.BigInte ...
- flask-login源码梳理
- javascript 访问cookie信息
在Javascript脚本里,一个cookie 实际就是一个字符串属性.当你读取cookie的值时,就得到一个字符串,里面当前WEB页使用的所有cookies的名称和值.每个cookie除了 name ...
- Redis .net 客户端 分布式锁
关于Redis分布式锁的参考链接:http://redis.io/topics/distlock. 在我们项目中,之前琢磨用:ServiceStack.Redis,发现ServiceStack.Red ...
- promise 如何知道所有的回调都执行完了?
var fs = require('fs'); /** * @return {object} Promise */ function doThing(fileName) { // ... // con ...