注意fail时怎么走。

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <cstring>
#include <map>
#include <queue>
#include <set>
#include <cassert>
#include <stack>
#include <bitset>
#define mkp make_pair
using namespace std;
const double EPS=1e-;
typedef long long lon;
const lon SZ=,SSZ=,APB=,one=;
const lon INF=0x7FFFFFFF,mod=;
lon n,m,mk[SZ],nex[SZ][APB],fail[SZ];
int cnt,endpos[SSZ],dst[SSZ][SSZ];
int dtb[SZ],dp[][SSZ],len[SSZ];
int ctn[SSZ][SSZ];
char ch[SZ];
string str[SSZ]; void add(int type,int id)
{
int cur=;
for(int i=;ch[i];++i)
{
int c=ch[i]-'';
if(!nex[cur][c])nex[cur][c]=++cnt;
cur=nex[cur][c];
}
if(type)mk[cur]=;
else endpos[id]=cur;
} void build()
{
queue<int> q;
q.push();
for(;q.size();)
{
int fr=q.front();
q.pop();
for(int i=;i<APB;++i)
{
int t=nex[fr][i];
if(t)
{
if(!fr)fail[t]=;
else
{
int u=fail[fr];
for(;u&&!nex[u][i];u=fail[u]);
u=nex[u][i];
fail[t]=u;
mk[t]|=mk[u];
}
q.push(t);
}
}
}
} void bfs(int x)
{
dtb[x]=;
queue<int> q;
q.push(x);
for(;q.size();)
{
int fr=q.front();
q.pop();
for(int i=;i<APB;++i)
{
int t=nex[fr][i];
if(t)
{
if(dtb[t]>dtb[fr]+&&!mk[t])
{
q.push(t);
dtb[t]=dtb[fr]+;
}
}
else
{
int u=fail[fr];
for(;u&&!nex[u][i];u=fail[u]);
t=nex[u][i];
if(dtb[t]>dtb[fr]+&&!mk[t])
{
q.push(t);
dtb[t]=dtb[fr]+;
}
}
}
}
} void init()
{
for(int i=;i<n;++i)
{
//cin>>ch+1;
scanf(" %s",ch+);
len[i]=strlen(ch+);
add(,i);
string tmp(ch+);
str[i]=tmp;
}
for(int i=;i<m;++i)
{
scanf(" %s",ch+);
add(,-);
}
build();
for(int i=;i<n;++i)
{
for(int j=;j<=cnt;++j)
dtb[j]=INF/;
int sta=;
bfs(endpos[i]);
for(int j=;j<n;++j)
{
dst[i][j]=dtb[endpos[j]];
}
}
} int dfs(int sta,int last)
{
//cout<<sta<<" "<<last<<endl;
if(dp[sta][last])return dp[sta][last];
else
{
int res=INF/;
for(int i=;i<n;++i)
{
if(ctn[last][i]&&(sta&(<<i)))sta^=<<i;
}
if(sta==)return len[last];
for(int i=;i<n;++i)
{
if(sta&(<<i))
{
res=min(res,dfs(sta,i)+dst[i][last]);
}
}
return dp[sta][last]=res;
}
} void work()
{
for(int i=;i<n;++i)
{
dp[<<i][i]=len[i];
}
int res=INF/;
for(int i=;i<n;++i)
{
for(int j=;j<n;++j)
{
if(str[i].find(str[j])!=-)ctn[i][j]=;
}
}
// for(int i=0;i<n;++i)
// {
// for(int j=0;j<n;++j)
// {
// cout<<ctn[i][j]<<" ";
// }cout<<endl;
// }
for(int i=;i<n;++i)
{
res=min(res,dfs((<<n)-,i));
//cout<<"i: "<<dfs((1<<n)-1,i)<<" "<<dst[0][1]<<" "<<dst[1][0]<<endl;
}
cout<<res<<endl;
} void release()
{
for(int i=;i<=cnt;++i)
{
mk[i]=;
memset(nex[i],,sizeof(nex[i]));
fail[i]=;
}
cnt=;
for(int i=;i<(<<n);++i)
{
memset(dp[i],,sizeof(dp[i]));
}
} int main()
{
//std::ios::sync_with_stdio(0);
//freopen("d:\\1.txt","r",stdin);
int casenum;
//memset(bel,-1,sizeof(bel));
//cin>>casenum;
//cout<<casenum<<endl;
//for(int time=1;time<=casenum;++time)
for(int time=;cin>>n>>m,n;++time)
{
init();
work();
release();
}
return ;
}

hdoj3247的更多相关文章

  1. 专题训练之AC自动机

    推荐博客:http://www.cnblogs.com/kuangbin/p/3164106.html AC自动机小结 https://blog.csdn.net/creatorx/article/d ...

随机推荐

  1. js常用的400个特效

    JavaScript实现可以完全自由拖拽的效果,带三个范例     http://www.sharejs.com/showdetails-501.aspx javascript实现可以自由拖动的树形列 ...

  2. lock 单例模式

    单例模式只允许创建一个对象,因此节省内存,加快对象访问速度,因此对象需要被公用的场合适合使用,如多个模块使用同一个数据源连接对象等等 网站的计数器,一般也是采用单例模式实现,否则难以同步 单例模式要素 ...

  3. Tensorflow --BeamSearch

    github:https://github.com/zle1992/Seq2Seq-Chatbot 1. 注意在infer阶段,需要需要reuse, 2.If you are using the Be ...

  4. OAuth授权 | 把这一篇丢给他

    OAuth授权 一.背景 上一篇我们介绍了单点登录(SSO),它能够实现多个系统的统一认证.今天我们来谈一谈近几年来非常流行的,大名鼎鼎的OAuth.它也能完成 统一认证,而且还能做更多的事情.至于O ...

  5. sql server导出数据,远程连接失败,需要设置权限

    在sql  server management中右键当前连接——>方面 在 服务器配置中 将  RemoteAccessEnabled.RemoteDacEnabled设置为TRUE 安全性—— ...

  6. IT题库7-线程加锁

    转载:http://www.cnblogs.com/linjiqin/p/3208843.html 一.同步问题提出 线程的同步是为了防止多个线程访问一个数据对象时,对数据造成的破坏.例如:两个线程T ...

  7. 2017.11.27 变量进阶与LED矩阵

    局部变量:函数内部声明的变量,只在函数内部有效. 全部变量:在函数外部声明的变量,全局都有效,直到程序执行完毕. 全局变量负作用: 1.降低函数的独立性 2.降低函数的通用性,不利于函数的重复调用. ...

  8. TTL与非门电路的工作原理

    分立元件门电路虽然结构简单,但是存在着体积大.工作可靠性差.工作速度慢等许多缺点.1961年美国德克萨斯仪器公司率先将数字电路的元器件和连线制作在同一硅片上,制成了集成电路.由于集成电路体积小.质量轻 ...

  9. Linux环境配置文件的理解

    百度百科: .bashrc这个文件主要保存个人的一些个性化设置,如命令别名.路径等.也即在同一个服务器上,只对某个用户的个性化设置相关. 示例: 编辑# User specific aliases a ...

  10. JavaScript之事件的绑定与移除

    对于事件的绑定的方法有多种多样,但是在解除绑定事件的时候,就要注意使用的是那种绑定事件的方法,因为不同的绑定方法所对应的解除事件是不同的. 1. 原始写法 1.1 绑定事件:对象.事件=事件处理函数 ...