hdu3689(kmp+dp)
题意:问随机生成一个长度为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)的更多相关文章
- 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 题意: 你打算在纸上印一串字 ...
- [HDOJ5763]Another Meaning(KMP, DP)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5763 题意:给定两个字符串a和b,其中a中的字符串如果含有子串b,那么那部分可以被替换成*.问有多少种 ...
- hdu_3336: Count the string(KMP dp)
题目链接 题意:求给定字符串中,可以与某一前缀相同的所有子串的数量 做这道题需要明白KMP算法里next[]数组的意义 首先用一数组nex[](这里与之前博客中提到的next明显不同)存储前缀后缀最长 ...
- HDU5763 another meaning -(KMP+DP)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5763 思路:dp[i]表示前i个字符组成的字符串所表示的意思数量,则当匹配时dp[i]=dp[i-1] ...
- hdu 3336 count the string(KMP+dp)
题意: 求给定字符串,包含的其前缀的数量. 分析: 就是求所有前缀在字符串出现的次数的和,可以用KMP的性质,以j结尾的串包含的串的数量,就是next[j]结尾串包含前缀的数量再加上自身是前缀,dp[ ...
- HDU 5763 Another Meaning (kmp + dp)
Another Meaning 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5763 Description As is known to all, ...
- 【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 ...
- hdu 6068--Classic Quotation(kmp+DP)
题目链接 Problem Description When online chatting, we can save what somebody said to form his ''Classic ...
- HDU3336(KMP + dp)
Count the string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
随机推荐
- 解决visual studio空格变成很多点号的3种方法
在用visual studio做网站时不知道按了什么快捷键,所有页面上的空格都变成了点号,就像下图那样. 要解决空格变点号的方法有两种:1.编辑->高级->查看空白2.Ctrl+E 然后按 ...
- The area面积计算
Problem Description Ignatius bought a land last week, but he didn't know the area of the land becaus ...
- jwt (JSON Web Token)官方说明
http://tools.ietf.org/html/draft-ietf-oauth-json-web-token-32#appendix-A 相关文章 http://haomou.net/2014 ...
- HDFS 上传文件的不平衡,Balancer问题是过慢
至HDFS上传文件.假定从datanode开始上传文件,上传的数据将导致目前的当务之急是全datanode圆盘.这是一个分布式程序的执行是非常不利. 解决方案: 1.从其他非datanode节点上传 ...
- 第一pga 畸形消费分析
第一pga 畸形消费分析 os: aix 6 db:10205 ------使用os 命令观察oracle 存消耗情况 #ps gv ...... ...
- COCOFrame
COCOFrame它是一个轻量级的开发框架,综合Android APP开发过程中经常使用的工具,方法,特效.当中F类採用的AndroidQuery实现,做了进一步的封装及部分优化. 1. Jar包大小 ...
- 解决因特网和xshell考虑到问题
首先需要解释.我们学校的网络是免费的.无论是实验室或宿舍.因此,互联网是基于Mac地址分配IP的,所以我VirtualBox安装了centos之后,话.就须要将VirtualBox的mac地址改成和我 ...
- 依据Uri获得sd卡图片
<pre name="code" class="java">String mBigImage = URLEncoder.encode(mImageU ...
- windows 10 install oracle 12c error:[ INS-30131 ]
"[ INS-30131 ] the Initial Setup That Is Required to Run the Installation Program Validation Wa ...
- 走向DBA[MSSQL篇] 面试官最喜欢的问题 ----索引+C#面试题客串
原文:走向DBA[MSSQL篇] 面试官最喜欢的问题 ----索引+C#面试题客串 对大量数据进行查询时,可以应用到索引技术.索引是一种特殊类型的数据库对象,它保存着数据表中一列或者多列的排序结果,有 ...