题目链接:

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

Another Meaning

Time Limit: 2000/1000 MS (Java/Others)Memory Limit: 65536/65536 K (Java/Others)

问题描述

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.

输入

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|

输出

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

题解

dp
用kmp处理出匹配成功的结尾的字母位置,然后就是考虑每个位置选和不选的两种情况,转移下就可以了。

代码

#include<map>
#include<queue>
#include<vector>
#include<cstdio>
#include<cstring>
#include<string>
#include<iostream>
#include<algorithm>
#define X first
#define Y second
#define mkp make_pair
#define lson (o<<1)
#define rson ((o<<1)|1)
#define M (l+(r-l)/2)
#define bug(a) cout<<#a<<" = "<<a<<endl using namespace std;typedef __int64 LL;const int maxn=1e5+10;
const int INF=0x3f3f3f3f;
const LL INFL=0x3f3f3f3f3f3f3f3fLL;
const int mod=1e9+7;char s1[maxn],s2[maxn];
int vis[maxn];
vector<int> pos;
LL dp[maxn];int f[maxn];
void getFail(char *P){int m=strlen(P);f[0]=0; f[1]=0;for(int i=1;i<m;i++){int j=f[i];while(j&&P[i]!=P[j]) j=f[j];f[i+1]=P[i]==P[j]?j+1:0;}
}void find(char* T,char *P){int n=strlen(T),m=strlen(P);getFail(P);int j=0;for(int i=0;i<n;i++){while(j&&P[j]!=T[i]) j=f[j];if(P[j]==T[i]) j++;if(j==m){pos.push_back(i+1);vis[i+1]=1;}}
}void init(){pos.clear();memset(vis,0,sizeof(vis));
}int main() {int tc,kase=0;scanf("%d",&tc);while(tc--){init();scanf("%s%s",s1,s2);find(s1,s2);memset(dp,0,sizeof(dp));dp[0]=1;int n=strlen(s1),m=strlen(s2);for(int i=1;i<=n;i++){//第i位不选 dp[i]=dp[i-1];//第i位选 if(vis[i]) dp[i]+=dp[i-m];dp[i]%=mod;}printf("Case #%d: %I64d\n",++kase,dp[n]);}return 0;
}

转载于:https://www.cnblogs.com/fenice/p/5743505.html

HDU 5763 Another Meaning KMP+DP相关推荐

  1. HDU - 5763 Another Meaning

    As is known to all, in many cases, a word has two meanings. Such as "hehe", which not only ...

  2. hdu 6153 A Secret kmp + dp

    传送门 文章目录 题意: 思路: 题意: 给你两个串a,ba,ba,b,让你求对于bbb的每个后缀,设其长度为lenlenlen,其在aaa中出现的次数为cntcntcnt,那么他的贡献为len∗cn ...

  3. HDU - 4552 怪盗基德的挑战书(后缀数组+RMQ/KMP+dp)

    题目链接:点击查看 题目大意:给出一个字符串,统计每个前缀在字符串中出现的次数之和 题目分析:可以直接先用后缀数组跑出来height,再用RMQ跑出来任意两个后缀的height,我们可以将题意转换为求 ...

  4. HDU - 6153 A Secret(KMP的next数组性质/扩展KMP)

    题目链接:点击查看 题目大意:给出两个字符串a和b,我们首先定义L:字符串b当前的后缀子字符串长度,N:字符串b当前的后缀在字符串a中出现的次数,现在询问,对于字符串b的每一个后缀,求出L*N之和,答 ...

  5. HDU 1231.最大连续子序列-dp+位置标记

    最大连续子序列 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Su ...

  6. HDU5763 another meaning -(KMP+DP)

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

  7. HDU 3336 Count the string(KMP+DP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3336 题意:给你一个字符串,计算其所有前缀在该字符串出现的次数的总和. 思路:next[j]=i,代表 ...

  8. 【47.92%】【hdu 5763】Another Meaning

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission ...

  9. HDU5763 Another Meaning(KMP+dp)

    题意: 给你一个主串一个子串,然后主串中匹配到子串就可以把当前部分改为*, 问主串有多少中不同的样子 思路: 先KMP预处理主串中所有匹配到子串的末尾位置 然后用dp dp[N][2]只更新成功匹配的 ...

最新文章

  1. java中的分页 效率考虑_面试官:数据量很大,分页查询很慢,有什么优化方案?...
  2. leetcode算法题--回文链表
  3. 如何获得阿里技术offer:从《阿里DBA面试题》体味阿里社会招聘
  4. android客户端注入及清除cookie,Android客户端注入及清除Cookie
  5. 在中国,有这样一些村落
  6. 洛谷P5110:块速递推(特征根方程、光速幂)
  7. java 1的阶乘之和_1-20的阶乘之和(java)
  8. 挑战10个最难的Java面试题(附答案)【上】
  9. 微软披露首个由中国发现的蠕虫级漏洞 奇安信代码安全实验室获致谢
  10. 谷歌更新漏洞披露规则:不管补丁打没打,够90天才披露
  11. mysql 导出所有函数_mysql 导入导出 包括函数或者存储过程
  12. PATB 1038. 统计同成绩学生(20)
  13. java 反射覆盖方法,java – 确定一个方法是否覆盖使用反射的另一个?
  14. Python3 OpenCV
  15. 0084-CYX的异己
  16. 全网首发PHP版留言系统源码
  17. 软件测试cmm等级划分,CMM的五个等级及关键过程域
  18. 高数_第3章重积分__二重积分_怎样交换积分次序
  19. 【Vue生命周期详解】
  20. java计算机毕业设计河东街摊位管理系统源码+mysql数据库+系统+LW文档+部署

热门文章

  1. haddler处理队列 netty_Netty的任务队列的Task的三种使用场景
  2. 一般服务器显示闪存多少合适,现在服务器一般内存多大合适
  3. linux 编辑启动菜单,grub2的配置,linux启动菜单修改
  4. python pandas 日期格式_pandas 快速处理 date_time 日期格式方法
  5. 怎么使用php连接mysql_如何使用PHP连接MySQL
  6. 64位电脑mysql_Windows 64位操作系统下安装和配置MySQL
  7. regression+classification
  8. qt项目出现c4819错误的解决办法
  9. 电脑显示屏亮度怎么调_金合光电丨深圳led显示屏厂家为您诠释行业专业术语
  10. Spring MVC No converter found for return value of type