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

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

代码:

  1. /******************************************************
  2. * @author:xiefubao
  3. *******************************************************/
  4. #pragma comment(linker, "/STACK:102400000,102400000")
  5. #include <iostream>
  6. #include <cstring>
  7. #include <cstdlib>
  8. #include <cstdio>
  9. #include <queue>
  10. #include <vector>
  11. #include <algorithm>
  12. #include <cmath>
  13. #include <map>
  14. #include <set>
  15. #include <stack>
  16. #include <string.h>
  17. //freopen ("in.txt" , "r" , stdin);
  18. using namespace std;
  19.  
  20. #define eps 1e-8
  21. #define zero(_) (_<=eps)
  22. const double pi=acos(-1.0);
  23. typedef long long LL;
  24. const int Max=100010;
  25. const LL INF=0x3FFFFFFF;
  26. int n,m;
  27. struct point
  28. {
  29. char c;
  30. double p;
  31. } points[30];
  32. map<char,double> maps;
  33. string s;
  34. double ans[1010][12];
  35. int Next[30];
  36. void get_next()
  37. {
  38. int i=0;
  39. int j=Next[0]=-1;
  40. int len=s.size();
  41. while(i<len)
  42. {
  43. while(j!=-1&&s[i]!=s[j]) j=Next[j];
  44. Next[++i]=++j;
  45. }
  46. }
  47. int OK(string t)
  48. {
  49. for(int i=0;i<t.size();i++)
  50. {
  51. if(t.substr(i,t.size()-i)==s.substr(0,t.size()-i))
  52. return t.size()-i;
  53. }
  54. return 0;
  55. }
  56. int get(int j,int k)
  57. {
  58. while(j!=-1&&s[j]!=points[k].c)
  59. {
  60. j=Next[j];
  61. }
  62. return j+1;
  63. }
  64. int main()
  65. {
  66. while(scanf("%d%d",&n,&m)==2)
  67. {
  68. if(n==0&&m==0)
  69. break;
  70. memset(ans,0,sizeof ans);
  71. for(int i=0; i<n; i++)
  72. {
  73. cin>>points[i].c>>points[i].p;
  74. }
  75. cin>>s;
  76. get_next();
  77. ans[0][0]=1.0;
  78. for(int i=1; i<=m; i++)
  79. {
  80. for(int j=1; j<=s.size(); j++)
  81. for(int k=0; k<n; k++)
  82. {
  83. ans[i][get(j-1,k)]+=ans[i-1][j-1]*points[k].p;
  84. }
  85. }
  86. double out=0;
  87. for(int i=1;i<=m;i++)
  88. out+=ans[i][s.size()];
  89. printf("%.2lf%%\n",out*100);
  90. }
  91. return 0;
  92. }

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. datagrid直接编辑保存“设计缺陷”

    当今使用easyUI的datagrid组件的时候,碰到了一些问题,记录下来以便下次高速解决. 需求是在一张表单里会关联有一个列表,能够增删查改 曾经没用easyUI的时候,这个增和改的页面我通常是用一 ...

  2. DataGridView绑定数据源

    给DataGridView绑定数据源比較简单,方法主要有两种: 1.直接在控件属性中绑定数据源,这样的方法最简单,但它是直接连接数据库的,这样就和传DataTable的后果差点儿相同了,所以还是尽量避 ...

  3. JDBC调用存储过程

    一. JDBC调用存储过程 (1)使用存储过程SQL编写的程序代码,等一段语句和控制流语句.在创建时被编译成机器代码和存储在数据库中的client转让. 存储过程具有以下优势: 1.所生成的机器代码被 ...

  4. jsp include指令标签

    假设须要在JSP页面内某处总体嵌入一个文件,就能够考虑使用这个指令标签. 该指令标签例如以下: <%@ include file ="文件的名字"%> 该指令标签的作用 ...

  5. DWR入门实例(二)

    DWR(Direct Web Remoting) DWR is a Java library that enables Java on the server and JavaScript in a b ...

  6. Action、Category、Data、Extras知识具体解释

    开头 Intent作为联系各Activity之间的纽带,其作用并不仅仅仅仅限于简单的数据传递.通过其自带的属性,事实上能够方便的完毕非常多较为复杂的操作.比如直接调用拨号功能.直接自己主动调用合适的程 ...

  7. Android L SDK -- 一些有趣的新功能

    一些普通的就不提了,自己查看最新的文档就可以 文档地址 Task locking 功能:让我们在使用一个应用时,能够免受通知(消息)的打搅. 怎样使用:当我们在应用中激活任务锁模式.我们接收到的通知( ...

  8. SQL Server 连接问题-TCP/IP

    原文:SQL Server 连接问题-TCP/IP 出自:http://blogs.msdn.com/b/apgcdsd/archive/2012/02/24/ms-sql-server-tcp-ip ...

  9. Lichee(三) Android4.0该产品的目标文件夹,Lichee链接---extract-bsp

    由<Lichee() 在sun4i_crane平台下的编译>介绍了编译lichee的基本情况,我们终于得到了编译后的结果例如以下: out/ ├── android │   ├── bIm ...

  10. jquery.validate.unobtrusive

    ASP.NET MVC Unobtrusive JavaScript 实现 onfocusout 验证, onfocusin 清除错误 在 ASP.NET MVC 中启用 Unobtrusive Ja ...