Another Meaning

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=5763

Description

As is known to all, in many cases, a word has two meanings. Such as “hehe”, which not only means “hehe”, but also means “excuse me”.

Today, ?? is chating with MeiZi online, MeiZi sends a sentence A to ??. ?? is so smart that he knows the word B in the sentence has two meanings. He wants to know how many kinds of meanings MeiZi can express.

Input

The first line of the input gives the number of test cases T; T test cases follow.

Each test case contains two strings A and B, A means the sentence MeiZi sends to ??, B means the word B which has two menaings. string only contains lowercase letters.

Limits

T <= 30

|A| <= 100000

|B| <= |A|

Output

For each test case, output one line containing “Case #x: y” (without quotes) , where x is the test case number (starting from 1) and y is the number of the different meaning of this sentence may be. Since this number may be quite large, you should output the answer modulo 1000000007.

Sample Input

4

hehehe

hehe

woquxizaolehehe

woquxizaole

hehehehe

hehe

owoadiuhzgneninougur

iehiehieh

Sample Output

Case #1: 3

Case #2: 2

Case #3: 5

Case #4: 1

Hint

In the first case, “ hehehe” can have 3 meaings: “he”, “he”, “hehehe”.

In the third case, “hehehehe” can have 5 meaings: “hehe”, “hehe”, “hehe*”, “**”, “hehehehe”.

Source

2016 Multi-University Training Contest 4

##题意:

给出字符串A和B,单词B具有两种解释,求字符串A一共有多少种意义.


##题解:

考虑A中每个单词B,需要考虑它是否解释为第二种意义;很显然要用DP.
首先用kmp处理出A中的每个B,记录每个B的起点和终点(下面的代码记录的是以i为终点的B的起点,没有则为-1).
dp[i]为处理到第i个字符时共有多少种意义:
若i是某个B的终点,则dp[i] = dp[i-1] + dp[起点-1]. (前者为当前B不解释为二义,后者为把B解释为二义).
若i不是终点,则dp[i] = dp[i-1];

值得一提的是:由于背包dp需要用到初始值dp[0],而字符串又从0开始,导致初始值为dp[-1],所以下面的代码特别处理了这个问题.
实际上,应该在读入字符串时空出str[0]来避免这个问题.


##代码:
``` cpp
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define LL int
#define eps 1e-8
#define maxn 101000
#define mod 1000000007
#define inf 0x3f3f3f3f
#define IN freopen("in.txt","r",stdin);
using namespace std;

char str[maxn],p[maxn];

int f[maxn],cnt;

int flag[maxn];

void getf()

{

memset(f,0,sizeof(f));

memset(flag, -1, sizeof(flag));

int len=strlen(p);

for(int i=1;i<len;i++)

{

int j=f[i];

while(j&&p[i]!=p[j]) j=f[j];

f[i+1]=(p[i]p[j]? j+1:0);

}

}

int ans;

void kmp()

{

getf();

int len1=strlen(str),len2=strlen(p);

for(int i=0,j=0;i<len1;i++)

{

while(j&&str[i]!=p[j]) j=f[j];

if(str[i]p[j]) j++;

if(j==len2) {

flag[i] = i-len2+1;

ans++;

}

}

}

int dp[maxn];

//由于字符串从0开始,这里需要用到dp[-1] = 1;

int dps(int i) {

if(i<0) return 1;

return dp[i]%mod;

}

int main(int argc, char const *argv[])

{

//IN;

  1. int t; cin >> t; int ca = 1;
  2. while(t--)
  3. {
  4. ans = 0;
  5. scanf("%s", str); scanf("%s", p);
  6. kmp();
  7. fill(dp, dp+maxn, 1);
  8. int len = strlen(str);
  9. int len2 = strlen(p);
  10. for(int i=0; i<len; i++) {
  11. if(flag[i] == -1) dp[i] = dps(i-1)%mod;
  12. else {
  13. dp[i] = dps(i-1);
  14. dp[i] = (dp[i] + dps(flag[i]-1))%mod;
  15. }
  16. }
  17. printf("Case #%d: %d\n", ca++, dp[len-1]%mod);
  18. }
  19. return 0;

}

HDU 5763 Another Meaning (kmp + dp)的更多相关文章

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

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

  2. HDU5763 another meaning -(KMP+DP)

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

  3. 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 题意: 你打算在纸上印一串字 ...

  4. HDU 4035:Maze(概率DP)

    http://acm.split.hdu.edu.cn/showproblem.php?pid=4035 Maze Special Judge Problem Description   When w ...

  5. HDU 3565 Bi-peak Number(数位DP)题解

    题意:我们定义每一位先严格递增(第一位不为0)后严格递减的数为峰(比如1231),一个数由两个峰组成称为双峰,一个双峰的价值为每一位位数和,问L~R双峰最大价值 思路:数位DP.显然这个问题和pos有 ...

  6. HDU 4169 Wealthy Family(树形DP)

    Problem Description While studying the history of royal families, you want to know how wealthy each ...

  7. HDU 5763 Another Meaning(DP+KMP)

    http://acm.hdu.edu.cn/showproblem.php?pid=5763 题意: 给出一个字符串和一个模式串,模式串有两种意思,问这句话有几种意思. 思路:因为肯定要去字符串去找模 ...

  8. HDU 5763 Another Meaning(FFT)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5763 [题目大意] 给出两个串S和T,可以将S串中出现的T替换为*,问S串有几种表达方式. [题解 ...

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

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

随机推荐

  1. php无法上传大文件完美解决方案

    php.ini无法上传大文件完美解决办法 1.打开php.ini(打开方式就不用说了,百度一大堆) 2.查找post_max_size 表单提交最大数值,此项不是限制上传单个文件的大小,而是针对整个表 ...

  2. android中getSystemService详解

        android的后台运行在很多service,它们在系统启动时被SystemServer开启,支持系统的正常工作,比如MountService监 听是否有SD卡安装及移除,ClipboardS ...

  3. MySql和Hibernate中关于cascade的用法

    数据库里的cascade的用法,Mysql和Hibernate里面是不相同. 在数据库里,进行增加.修改.删除记录的时候,经常会涉及到父子关系的表. 例如:有省份表和城市表,其中城市表有一个外键pro ...

  4. Js获取日期时间及其它操作

    var myDate = new Date();myDate.getYear();        //获取当前年份(2位)myDate.getFullYear();    //获取完整的年份(4位,1 ...

  5. (任寒韬)WebApp群主 - MobileTech 资料

    web app : http://www.lightapp.cn/brand/index/4101 https://github.com/jtyjty99999/mobileTech/blob/mas ...

  6. WDF模型驱动程序开发

    WDF驱动程序开发 1. 引言 设备驱动程序是硬件设备连接到计算机系统的软件接口,任何设备都必须有相应的驱动程序才能在计算机系统上正常工作.设备驱动程序的优劣直接关系到整个系统的性能和稳定性,因此,设 ...

  7. 一天一个Java基础——对象和类

    1.在Java中你所做的全部工作就是定义类,产生那些类的对象,以及发送消息给这些对象 2.可以在类中设置两种类型的元素:字段(也被称作数据成员)和方法(也被称作成员函数) 3.字段可以是任何类型的对象 ...

  8. Java应用调优指南之-工具篇

    1. 土法调优两大件 先忆苦思甜,一般人在没有Profile工具的时候,调优的两大件,无非Heap Dump 与 Thread Dump. 1.1 Heap Dump jmap -dump:live, ...

  9. 22个所见即所得在线 Web 编辑器

    前言: 关于编辑器,适合的才是最好的,接下来,我会写一些关于日志编辑器的文章,今天就写写,可能内容会比较多. --------------------------------------------- ...

  10. hdu 2059(dp)

    题意:容易理解... 思路:dp[i]表示乌龟到达第i个充电站时最少花费时间到第 i 个充电站后,从起点开始遍历到第 i-1 个充电站,得到最少花费时间 状态转移方程:dp[i]=min(dp[j]+ ...