博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 5763 Another Meaning (kmp + dp)
阅读量:5242 次
发布时间:2019-06-14

本文共 2399 字,大约阅读时间需要 7 分钟。

Another Meaning

题目链接:

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]来避免这个问题.

代码:

#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
> t; int ca = 1; while(t--) { ans = 0; scanf("%s", str); scanf("%s", p); kmp(); fill(dp, dp+maxn, 1); int len = strlen(str); int len2 = strlen(p); for(int i=0; i

转载于:https://www.cnblogs.com/Sunshine-tcf/p/5716585.html

你可能感兴趣的文章
洛谷P3216 [HNOI2011] 数学作业 [矩阵加速,数论]
查看>>
分享5个有帮助的CSS选择器
查看>>
如何搭建web服务器 使用Nginx搭建反向代理服务器 .
查看>>
android中状态栏透明
查看>>
SQL注入防御绕过——宽字节注入
查看>>
匿名函数(lambda)
查看>>
OpenCV中InputArray和OutputArray使用方法
查看>>
Java与.NET 的Web Services相互调用
查看>>
linux 查看并关闭窗口
查看>>
使用Http-Repl工具测试ASP.NET Core 2.2中的Web Api项目
查看>>
通过实例理解 RabbitMQ 的基本概念
查看>>
WPF自定义控件
查看>>
ASP.NET Core 2.2 基础知识(一) 依赖注入
查看>>
Docker在Windows上运行NetCore系列(一)使用命令控制台运行.NetCore控制台应用
查看>>
微信支付现金红包接口应用实例代码说明和DEMO详解,适合用来做微信红包营销活动、吸粉利器...
查看>>
简单说一下UWP中的JumpList
查看>>
微信小程序把玩(七)数据绑定
查看>>
C#使用Xamarin开发可移植移动应用(1.入门与Xamarin.Forms页面),附源码
查看>>
UWP开发笔记——嵌套式页面的实现
查看>>
关于有默认值的字段在用EF做插入操作时的思考(续)
查看>>