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;
}
时间: 2024-08-27 21:55:03

hdu3689(kmp+dp)的相关文章

[HDOJ5763]Another Meaning(KMP, DP)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5763 题意:给定两个字符串a和b,其中a中的字符串如果含有子串b,那么那部分可以被替换成*.问有多少种替换方法. kmp求出b在a中完全匹配后的结尾位置,然后dp(i)表示匹配到i时替换的方案数(不替换也算一次方案).首先更新dp(i)=dp(i-1),当且仅当i点是一个完全匹配的终点时,加上dp(i-nb)处的值. 1 #include <bits/stdc++.h> 2 using names

hdu 3336 count the string(KMP+dp)

题意: 求给定字符串,包含的其前缀的数量. 分析: 就是求所有前缀在字符串出现的次数的和,可以用KMP的性质,以j结尾的串包含的串的数量,就是next[j]结尾串包含前缀的数量再加上自身是前缀,dp[i]表示以i为结尾包含前缀的数量,则dp[i]=dp[next[i]]+1,最后求和即可. #include <map> #include <set> #include <list> #include <cmath> #include <queue>

HDU 3689 Infinite monkey theorem(KMP + DP)

题目链接:点击打开链接 思路: 用d[i][j]表示前i个字符,已经匹配了字母中的j个字符,最终包含这个字母的概率. 每次转移的时候有n个方向, 表示第i个字符选哪个字符, 那么有个问题, 如果我当前选的这个字符失配了, 那么转移之后我还匹配了多少个字符. 这恰恰是KMP能做的. 细节参见代码: #include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #inc

hdu 6068--Classic Quotation(kmp+DP)

题目链接 Problem Description When online chatting, we can save what somebody said to form his ''Classic Quotation''. Little Q does this, too. What's more? He even changes the original words. Formally, we can assume what somebody said as a string S whose

HDU3336(KMP + dp)

Count the string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 6875    Accepted Submission(s): 3191 Problem Description It is well known that AekdyCoin is good at string problems as well as nu

hdu5593--ZYB&#39;s Tree(树形dp)

问题描述 ZYB有一颗N个节点的树,现在他希望你对于每一个点,求出离每个点距离不超过KK的点的个数. 两个点(x,y)在树上的距离定义为两个点树上最短路径经过的边数, 为了节约读入和输出的时间,我们采用如下方式进行读入输出: 读入:读入两个数A,B,令fai??为节点i的父亲,fa?1??=0;fa?i??=(A∗i+B)%(i−1)+1,i∈[2,N] . 输出:输出时只需输出N个点的答案的xor和即可. 输入描述 第一行一个整数TT表示数据组数. 接下来每组数据: 一行四个正整数N,K,A,

HDU 4960 (水dp)

Another OCD Patient Problem Description Xiaoji is an OCD (obsessive-compulsive disorder) patient. This morning, his children played with plasticene. They broke the plasticene into N pieces, and put them in a line. Each piece has a volume Vi. Since Xi

hdu 2089 不要62 (数位dp)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2089 思路:用变量记录吉利数,和最高位为2的吉利数还有不是吉利数的个数... code: #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int dp[10][3]; //dp[i][j] ,i表示位数,j表示状态<pre name="code"

hdu 3555 Bomb(数位dp)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3555 题目大意:就是给你一个数n,判断从0到n有多少个数含有数字49...... 是不是觉得跟hdu2089很相似呀... 思路:跟hdu2089一样的,注意给出的数比较大,所以这儿用__int64  .... code: #include<cstdio> #include<iostream> #include<cstring> #include<algorithm&