【动态规划】【KMP】HDU 5763 Another Meaning
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=5763
题目大意:
T组数据,给两个字符串s1,s2(len<=100000),s2可以被解读成2种意思,问s1可以解读成几种意思(mod 1000000007)。
题目思路:
【动态规划】【KMP】
题目有点绕,看看样例就懂了。其实不用KMP直接用substr就能做。
首先不解读成另一个意思的话,f[i]=f[i-1],接着如果当前位置能够与s2匹配,那么f[i]+=f[i-strlen(s2)]表示当前这个s2串用第二种意思解读能多出的解数。
substr:
//
//by coolxxx
//<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<iomanip>
#include<memory.h>
#include<time.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//#include<stdbool.h>
#include<math.h>
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define abs(a) ((a)>0?(a):(-(a)))
#define lowbit(a) (a&(-a))
#define sqr(a) ((a)*(a))
#define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
#define mem(a,b) memset(a,b,sizeof(a))
#define eps (1e-8)
#define J 10000000
#define MAX 0x7f7f7f7f
#define PI 3.1415926535897
#define mod 1000000007
#define N 100004
using namespace std;
typedef long long LL;
int cas,cass;
int n,m,lll,ans;
int f[N];
string s1,s2; int main()
{
#ifndef ONLINE_JUDGE
freopen("1.txt","r",stdin);
// freopen("2.txt","w",stdout);
#endif
int i,j;
// for(scanf("%d",&cas);cas;cas--)
for(scanf("%d",&cas),cass=;cass<=cas;cass++)
// while(~scanf("%s",s))
// while(~scanf("%d",&n))
{
printf("Case #%d: ",cass);
cin>>s1>>s2;
n=s1.length();m=s2.length();
f[]=;
for(i=;i<=n;i++)
{
if(i>=m && s1.substr(i-m,m)==s2)
f[i]=(f[i-]+f[i-m])%mod;
else f[i]=f[i-];
}
printf("%d\n",f[n]);
}
return ;
}
/*
// //
*/
KMP:
//
//by coolxxx
//<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<iomanip>
#include<memory.h>
#include<time.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//#include<stdbool.h>
#include<math.h>
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define abs(a) ((a)>0?(a):(-(a)))
#define lowbit(a) (a&(-a))
#define sqr(a) ((a)*(a))
#define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
#define mem(a,b) memset(a,b,sizeof(a))
#define eps (1e-8)
#define J 10000000
#define MAX 0x7f7f7f7f
#define PI 3.1415926535897
#define mod 1000000007
#define N 100004
using namespace std;
typedef long long LL;
int cas,cass;
int n,m,lll,ans;
int Next[N],f[N];
char s1[N],s2[N];
bool mark[N];
void getNext(char s[])
{
int i,j,len;
Next[]=-;len=strlen(s);
for(i=,j=-;i<len;)
{
if(j==- || s[i]==s[j])
Next[++i]=++j;
else j=Next[j];
}
}
void kmp(char s1[],char s2[])
{
int i,j;
mem(mark,);
getNext(s2);
for(i=,j=;i<n;)
{
if(j==- || s1[i]==s2[j])i++,j++;
else j=Next[j];
if(j==m)
{
mark[i]=;
j=Next[j];
}
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("1.txt","r",stdin);
// freopen("2.txt","w",stdout);
#endif
int i,j;
// for(scanf("%d",&cas);cas;cas--)
for(scanf("%d",&cas),cass=;cass<=cas;cass++)
// while(~scanf("%s",s))
// while(~scanf("%d",&n))
{
printf("Case #%d: ",cass);
scanf("%s%s",s1,s2);
n=strlen(s1);m=strlen(s2);
kmp(s1,s2);
f[]=;
for(i=;i<=n;i++)
{
if(mark[i] && i>=m)
f[i]=(f[i-]+f[i-m])%mod;
else f[i]=f[i-];
}
printf("%d\n",f[n]);
}
return ;
}
/*
// //
*/
【动态规划】【KMP】HDU 5763 Another Meaning的更多相关文章
- HDU 5763 Another Meaning
HDU 5763 Another Meaning 题意:一个字串有可能在模式串出现多次,问有多少种可能出现的情况.关键是有重合的字串是不能同时计入的. 思路:先用kmp求出所有字串的位置.然后,dp. ...
- HDU 5763 Another Meaning KMP+DP
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5763 Another Meaning Time Limit: 2000/1000 MS (Java/ ...
- HDU 5763 Another Meaning (kmp + dp)
Another Meaning 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5763 Description As is known to all, ...
- HDU 5763 Another Meaning(DP+KMP)
http://acm.hdu.edu.cn/showproblem.php?pid=5763 题意: 给出一个字符串和一个模式串,模式串有两种意思,问这句话有几种意思. 思路:因为肯定要去字符串去找模 ...
- HDU 5763 Another Meaning dp+字符串hash || DP+KMP
题意:给定一个句子str,和一个单词sub,这个单词sub可以翻译成两种不同的意思,问这个句子一共能翻译成多少种不能的意思 例如:str:hehehe sub:hehe 那么,有**he.he** ...
- HDU 5763 Another Meaning (KMP/哈希+DP)
题目大意:给你两个串,一长一短,如果长串中某个子串和短串完全相同,则这个子串可以被替换成"#",求长串所有的表达形式....... 比如"hehehehe"和& ...
- HDU 5763 Another Meaning(FFT)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5763 [题目大意] 给出两个串S和T,可以将S串中出现的T替换为*,问S串有几种表达方式. [题解 ...
- TTTTTTTTTTTTTT hdu 5763 Another Meaning 哈希+dp
Another Meaning Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)T ...
- HDU 5763:Another Meaning(字符串匹配)
http://acm.hdu.edu.cn/showproblem.php?pid=5763 Another Meaning Problem Description As is known to ...
随机推荐
- codevs 2149 矩形周长(暴力扫描线)
/* 暴力应该很好理解 不多说了 至于线段树维护的嘛 还没看懂 哪天突然想明白了在写吧 */ #include<iostream> #include<cstdio> #incl ...
- css(动画,过渡,转换)
css3动画 @keyframes 规定动画,必须定义动画的名称,动画时长的百分比,一个或多个css样式属性 以百分比来规定改变发生的时间,或者通过关键词"from"和" ...
- hibernate对象关系映射( 一对一,一对多,多对一,多对多的单向,双向映射 ——)
对象之间的关系: 关系映射之间的关系只的是对象之间的关系,并不指数据库表的关系(外键关系)这儿解决的问题是当对象之间的关系之一时,数据库表该如何映射,编程上如何对待. 一对一(主键关联,和单向的外键关 ...
- Activity间的跳转,startActivity与startActivityForResult
JreduCh04 2016-07-30跳转 (由一个画面跳转到另一个画面)两种方法:Intent中 startActivity.startActivityForResult.后者可设置request ...
- ASP.net MVC 多语言处理
MVC多语言处理主要分两部分,一部分是Razor视图中的文字标签内容切换, 另一部分是javascript文件中的文标签内容切换. 这里分这两部分来说. View视图中的比较好做, 思路是使用资源文 ...
- android Services注意地方
使用service前需要在manifest声明: <manifest ... > ... <application ... > <service android:name ...
- MongoDB_1
突然想去看下MongoDB的东西,于是有了这篇文章.其实很早以前就看过一些关于NoSql的文章,还记得当时里面有介绍MongoDB的,多瞅了2眼,并且在Window下安装了MongoDB的驱动,小玩了 ...
- 【读书笔记】管道和FIFO
管道 提供一个单路(单向)数据流,可以为两个不同进程提供进程间的通信手段 #include <unistd.h> ]); 返回两个文件描述符,fd[0](读) 和 fd[1](写) 管道间 ...
- 一些硬件厂商的MAC号
http://standards.ieee.org/develop/regauth/oui/oui.txt https://svn.nmap.org/nmap/nmap-mac-prefixes
- install erlang environment on centos
#(erlide in linux can't detect the runtime if build from source, but erlang shell works correctly)su ...