题意:问随机生成一个长度为m(m<=1000)长度的字符串,出现某个子串s的概率是多少。

解法:dp+kmp优化。ans[i][j]表示i长度,走到了s的j位置的概率,当然这是在i之前没有出现s的前提下(在状态转移时候已经保证了这一点);然后最后的概率就是1-m长度的串分别最后出现s的概率之和。

代码:

/******************************************************
* @author:xiefubao
*******************************************************/
#pragma comment(linker, "/STACK:102400000,102400000")
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <queue>
#include <vector>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <stack>
#include <string.h>
//freopen ("in.txt" , "r" , stdin);
using namespace std; #define eps 1e-8
#define zero(_) (_<=eps)
const double pi=acos(-1.0);
typedef long long LL;
const int Max=100010;
const LL INF=0x3FFFFFFF;
int n,m;
struct point
{
char c;
double p;
} points[30];
map<char,double> maps;
string s;
double ans[1010][12];
int Next[30];
void get_next()
{
int i=0;
int j=Next[0]=-1;
int len=s.size();
while(i<len)
{
while(j!=-1&&s[i]!=s[j]) j=Next[j];
Next[++i]=++j;
}
}
int OK(string t)
{
for(int i=0;i<t.size();i++)
{
if(t.substr(i,t.size()-i)==s.substr(0,t.size()-i))
return t.size()-i;
}
return 0;
}
int get(int j,int k)
{
while(j!=-1&&s[j]!=points[k].c)
{
j=Next[j];
}
return j+1;
}
int main()
{
while(scanf("%d%d",&n,&m)==2)
{
if(n==0&&m==0)
break;
memset(ans,0,sizeof ans);
for(int i=0; i<n; i++)
{
cin>>points[i].c>>points[i].p;
}
cin>>s;
get_next();
ans[0][0]=1.0;
for(int i=1; i<=m; i++)
{
for(int j=1; j<=s.size(); j++)
for(int k=0; k<n; k++)
{
ans[i][get(j-1,k)]+=ans[i-1][j-1]*points[k].p;
}
}
double out=0;
for(int i=1;i<=m;i++)
out+=ans[i][s.size()];
printf("%.2lf%%\n",out*100);
}
return 0;
}

hdu3689(kmp+dp)的更多相关文章

  1. 2021.11.09 P3426 [POI2005]SZA-Template(KMP+DP)

    2021.11.09 P3426 [POI2005]SZA-Template(KMP+DP) https://www.luogu.com.cn/problem/P3426 题意: 你打算在纸上印一串字 ...

  2. [HDOJ5763]Another Meaning(KMP, DP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5763 题意:给定两个字符串a和b,其中a中的字符串如果含有子串b,那么那部分可以被替换成*.问有多少种 ...

  3. hdu_3336: Count the string(KMP dp)

    题目链接 题意:求给定字符串中,可以与某一前缀相同的所有子串的数量 做这道题需要明白KMP算法里next[]数组的意义 首先用一数组nex[](这里与之前博客中提到的next明显不同)存储前缀后缀最长 ...

  4. HDU5763 another meaning -(KMP+DP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5763 思路:dp[i]表示前i个字符组成的字符串所表示的意思数量,则当匹配时dp[i]=dp[i-1] ...

  5. hdu 3336 count the string(KMP+dp)

    题意: 求给定字符串,包含的其前缀的数量. 分析: 就是求所有前缀在字符串出现的次数的和,可以用KMP的性质,以j结尾的串包含的串的数量,就是next[j]结尾串包含前缀的数量再加上自身是前缀,dp[ ...

  6. HDU 5763 Another Meaning (kmp + dp)

    Another Meaning 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5763 Description As is known to all, ...

  7. 【HDU 3336】Count the string(KMP+DP)

    Problem Description It is well known that AekdyCoin is good at string problems as well as number the ...

  8. hdu 6068--Classic Quotation(kmp+DP)

    题目链接 Problem Description When online chatting, we can save what somebody said to form his ''Classic ...

  9. HDU3336(KMP + dp)

    Count the string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

随机推荐

  1. UNITY3D MAC版本号破解

    首先,解释一下.是公司做开发建议去购买正版. 之前网上也有非常多人贴出了破解方法,有些也是能够的.可是大多数解说不太具体,在这里贴出相对具体点的教程.本人亲測成功(測试版本Unity4.0.1 mac ...

  2. C指针决心 ------ 指针的概念和元素

     本文是自己学习所做笔记,欢迎转载,但请注明出处:http://blog.csdn.net/jesson20121020 指针在C语言中的地位,不用多说. 指针的概念 指针是一个特殊的变量,它里面存储 ...

  3. 原产地政策,jsonp跨域

           一.同源策略         同源策略(Same origin policy)是一种约定,它是浏览器最核心也最主要的安全功能,假设缺少了同源策略,则浏览器的正常功能可能都会受到影响.能够 ...

  4. 自己写CPU第九阶段(3)——加载存储指令说明2(swl、swr)

    我们会继续上传新书<q=%E8%87%AA%E5%B7%B1%E5%8A%A8%E6%89%8B%E5%86%99CPU&ie=utf-8&src=se_lighten_quot ...

  5. HDU 1176 免费馅饼(DP)

    职务地址:HDU 1176 以时间为横轴.11个点位纵轴构造一个矩阵.然后利用数字三角形的方法从上往下递推下去. 代码例如以下: #include <iostream> #include ...

  6. 利用Ring Buffer在SQL Server 2008中进行连接故障排除

    原文:利用Ring Buffer在SQL Server 2008中进行连接故障排除 出自:http://blogs.msdn.com/b/apgcdsd/archive/2011/11/21/ring ...

  7. Visual Studio Team Services使用教程--Readers tfs组checkin权限修改

    你也可以只开启部分代码的权限 把上面开启的整个应用的权限先去掉 只开启一个文件的权限

  8. curl 要么 file_get_contents 获得授权页面的方法的必要性

    今天,需要工作,需要使用 curl / file_get_contents 获得授权的必要性(Authorization)的页面内容.解决后写了这篇文章分享给大家. php curl 扩展,可以在se ...

  9. IQ调制、整形滤波器与星座映射

    http://www.cnblogs.com/touchblue/archive/2013/01/15/2861952.html 现代通信中.IQ调制基本上属于是标准配置,由于利用IQ调制能够做出全部 ...

  10. 全自动Web后门扫描(转)

    阅读目录 工具介绍 使用方法 工具介绍 这是一款全自动Web后门查杀工具,基于Python开发 某些较新的后门可能会查杀失败 规则列表来自seay博客 回到顶部 使用方法 1.按恶意代码查杀: pyt ...