UVA 11468 Substring(AC自动机+树上的动态规划)
来源:互联网 发布:windows xp桌面主题 编辑:程序博客网 时间:2024/04/29 15:26
这题pro数组开小了,一直TLE。
就是简单的记忆化搜索,走不是单词节点的点就可以,记得标出危险节点。
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<queue>using namespace std;const int maxnode=500;int idx[256];int k,n,L;struct DFA{ int ch[maxnode][64]; int f[maxnode]; int match[maxnode]; int sz; void clear() {sz=1;memset(ch[0],0,sizeof(ch[0]));} void insert(char *s) { int u=0; for(int i=0;s[i];i++) { int c=idx[s[i]]; if(!ch[u][c]) { memset(ch[sz],0,sizeof(ch[sz])); match[sz]=0; ch[u][c]=sz++; } u=ch[u][c]; } match[u]=1; } void getfail() { queue<int> q; f[0]=0; for(int i=0;i<64;i++) { int v=ch[0][i]; if(v) {f[v]=0;q.push(v);} } while(!q.empty()) { int r=q.front();q.pop(); for(int c=0;c<64;c++) { int u=ch[r][c]; if(!u) {ch[r][c]=ch[f[r]][c];continue;} q.push(u);int v=f[r]; while(v&&!ch[v][c]) v=f[v]; f[u]=ch[v][c]; match[u]|=match[f[u]]; } } }};#define rep(i,a,b) for(int i=(a);i<(b);i++)#define ss(x) scanf("%d",&x)DFA ac;char s[30][30];double pro[64];int vis[maxnode][105];double d[maxnode][105];double getPro(int u,int l){ if(!l) return 1.0; if(vis[u][l]) return d[u][l]; vis[u][l]=1; double &ans=d[u][l]; ans=0.0; rep(i,0,n) if(!ac.match[ac.ch[u][i]]) ans+=pro[i]*getPro(ac.ch[u][i],l-1); return ans;}int main(){ int t,kase=0;ss(t); while(t--) { ss(k);rep(i,0,k) scanf("%s",s[i]); ss(n);rep(i,0,n) {char cmd[10];scanf("%s%lf",cmd,&pro[i]);idx[cmd[0]]=i;} ac.clear(); rep(i,0,k) ac.insert(s[i]); ac.getfail(); ss(L); memset(vis,0,sizeof(vis)); printf("Case #%d: %.6lf\n",++kase,getPro(0,L)); } return 0;}
0 0
- UVA 11468 Substring(AC自动机+树上的动态规划)
- UVA 11468 Substring AC自动机
- UVA 11468 - Substring(AC自动机)
- Uva 11468 Substring (AC自动机)
- UVA 11468(Substring-AC自动机上dp)[Template:AC自动机]
- UVA 11468 Substring(AC自动机 + dp)
- UVA 11468-Substring(AC自动机+概率dp)
- UVA 11468 Substring(AC自动机+概率DP)
- UVa 11468 Substring AC自动机+概率DP
- UVA - 11468 Substring,AC自动机 + DP
- uva 11468 - Substring(AC自动机+概率)
- 【UVA】11468-Substring(AC自动机)
- UVA 11468 Substring(AC自动机+dp)
- UVA 11468 Substring AC自动机+概率DP
- UVA - 11468 Substring ( AC自动机 + dp)
- UVA 11468 Substring(AC自动机+概率DP)
- 【AC自动机】基于自动机状态设计的动态规划
- UVA 11468 —— Substring(AC自动机+DP)
- 前端框架增加自定义fontIcons的方法
- 连续子数组的最大和
- Three.js 学习记录 之 纹理加载
- jquery点击table表头排序
- 润乾报表的集算报表V5.0集成到WEB项目
- UVA 11468 Substring(AC自动机+树上的动态规划)
- 发起一个微信支付请求接口— wx.chooseWXPay() – 微信JS开发文档
- Minecraft 编程 学习笔记
- Maven项目文件路径问题
- effective C++之禁止拷贝构造函数和赋值运算符
- 一文搞懂HMM模型(隐马儿可夫模型)
- Linux目录贴
- 安卓开发爬坑之修改包名
- Android7.0 BatteryStatsService