HDU 5763 Another Meaning dp+字符串hash || DP+KMP
题意:给定一个句子str,和一个单词sub,这个单词sub可以翻译成两种不同的意思,问这个句子一共能翻译成多少种不能的意思
例如:str:hehehe sub:hehe 那么,有**he、he**、和hehehe三种不同的意思,
考虑一下aaadaaa这种情况?sub:aa 前面的aaa有三种,后面的aaa有三种,所以一共应该是有9种情况。
可以考虑成3*3=9
如果你考虑分块去相乘的话,那么恭喜你,你GG了。因为这样写非常复杂,而且非常难判断。
可以考虑下dp,因为注意到,它每个单词只有两种状态,要么转换成其他意思,要么就保留原意。
记dp[i]为匹配到str的第i个字符,所拥有的方案数,那么,如果不转换意思,dp[i] = dp[i-1]
就是方案数是没增加的,还是原来拥有的总数。
那么考虑转义。需要str[i-lensub+1...i]这段字符和sub一模一样,你才能转义把?
这里可以用字符串hash的方法O(1)判断
那么dp[i] += dp[i-lenstub];
就是在屏蔽str[i-lensub+1...i]这段字符的情况下,拥有的方案数,+我的转义,就是一种全新的方案,所以匹配到这个字符的时候,方案数要加上屏蔽这段字符前拥有的方案数
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL;
typedef unsigned long long int ULL;
#include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
const int maxn = +;
char str[maxn];
char sub[maxn];
LL dp[maxn];
const int MOD = 1e9+;
ULL sumhash[maxn];
ULL powseed[maxn];
const int seed = ;
ULL calc (int begin,int end)
{
return sumhash[end] - powseed[end-begin+]*sumhash[begin-];
}
int f;
void work ()
{
scanf("%s%s",str+,sub+);
int lenstr = strlen(str+);
int lensub = strlen(sub+);
ULL sub_hash = ; for (int i=;i<=lensub;++i) sub_hash = sub_hash*seed + sub[i];
for (int i=;i<=lenstr;++i) sumhash[i] = sumhash[i-]*seed + str[i]; for (int i=;i<=lensub;++i) dp[i]=;
// printf ("%I64u\n",sub_hash);
// printf ("%I64u\n",sumhash[4]);
for (int i=lensub;i<=lenstr;++i)
{
dp[i] = dp[i-];
//printf ("%d %d %I64u\n",i-lensub+1,i,calc(i-lensub+1,i));
if (calc(i-lensub+,i) == sub_hash)
{
dp[i] += dp[i-lensub];
}
dp[i] %= MOD;
}
printf ("Case #%d: %I64d\n",++f,dp[lenstr]);
return ;
} int main()
{
#ifdef local
freopen("data.txt","r",stdin);
#endif
powseed[]=;
for (int i=;i<=maxn-;++i) powseed[i] = powseed[i-]*seed;
int t;
scanf("%d",&t);
while (t--) work();
return ;
}
这题也可以用kmp做的
就是kmp匹配一次后,用个isok数组保存str的第i位能否匹配成功就可以了
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL;
typedef unsigned long long int ULL;
#include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
const int maxn = +;
char str[maxn];
char sub[maxn];
LL dp[maxn];
bool isok[maxn];
const int MOD = 1e9+;
int f;
void get_next (char sub[],int nextliu[],int len)
{
int i=,j=;
nextliu[]=;//记得初始值不能忘记
//next[len+1]这个也有值了 后面的++i和++j先加后赋值
while (i<=len)
{
//sub[i]的含义,后缀的单个字符
//sub[j]的含义,前缀的单个字符
if (j== || sub[i]==sub[j])//考虑的是上一个的
{
nextliu[++i]=++j; //用是上一个的比较,值的当前的值
}
else j=nextliu[j];
}
return ;
}
int nextliu[maxn];
int kmp (char str[],char sub[],int pos)
{
int len1=strlen(str+);
int len2=strlen(sub+);
//int next[maxn]={0};//maxn为最大长度
get_next(sub,nextliu,len2);//得到next[]数组
int i=pos;//从哪里出发
int j=;
int ans=;
while (i<=len1)
{
if (j== || str[i]==sub[j])
{
i++;j++;
}
else j=nextliu[j];
if (j==len2+)//有一个了
{
isok[i-]=; //标记这个位置是OK的
ans++;
j=nextliu[j];//回溯匹配
//i值不用回溯的
}
}
return ans;
} void work ()
{
scanf("%s%s",str+,sub+);
int lenstr = strlen(str+);
int lensub = strlen(sub+);
memset(isok,,sizeof isok);
kmp(str,sub,);
for (int i=;i<=lensub;++i) dp[i]=;
// for (int i=1;i<=lenstr;++i)
// {
// printf ("%d ",isok[i]);
// }
// printf ("\n");
for (int i=lensub;i<=lenstr;++i)
{
dp[i]=dp[i-];
if (isok[i]) dp[i] += dp[i-lensub];
dp[i] %= MOD;
}
printf ("Case #%d: %I64d\n",++f,dp[lenstr]);
return ;
} int main()
{
#ifdef local
freopen("data.txt","r",stdin);
#endif
int t;
scanf("%d",&t);
while (t--) work();
return ;
}
Count the string
统计所有前缀在本串中出现的次数和
kmp + dp
利用next数组。
给定一个长为lenstr(<=20W)的字符串,要求找出所有前缀在本串中出现的次数,在这里前缀有,"a","ab","aba","abab" ans=2+2+1+1=6。思路:利用next[]的意义,最大的前缀和后缀匹配,那么,设dp[i]表示以第i个字符串结尾的所有子串(记住一定要是第i个字符串结尾,不要以为dp[n]就是答案,子串还有很多),和前i个字符的前缀(前缀一定是:s[1],s[1]s[2],s[1]s[2]s[3],这样的,不能是s[2]s[3]这样的。)所匹配的数目。那么,既然前缀和后缀相同,abab*****abab,那么,我的dp[i]=dp[next[i+1]-1]+1。(+1)是为了加上自己那个串,出现了一次。
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
#define inf (1<<28)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
const int MOD = ;
const int maxn = +;
char str[maxn];
void get_next (char sub[],int nextliu[],int len)
{
nextliu[]=;
int i=,j=;
while (i<=len)
{
if (j== || sub[i]==sub[j])
{
nextliu[++i]=++j;
}
else j=nextliu[j];
}
return ;
}
int nextliu[maxn];
int dp[maxn]={};
void work ()
{
int lenstr;
scanf ("%d",&lenstr);
scanf ("%s",str+);
get_next(str,nextliu,lenstr);
/*
for (int i=1;i<=lenstr;i++)
{
printf ("%d ",next[i]);
}
printf ("\n");*/ int ans=;
for (int i=;i<=lenstr;i++)
{
dp[i]=dp[nextliu[i+]-]+;
ans += dp[i];
ans %= MOD;
}
printf ("%d\n",ans);
return ;
} int main ()
{
#ifdef local
freopen("data.txt","r",stdin);
#endif
int t;
scanf ("%d",&t);
while (t--)
{
work ();
}
return ;
}
HDU 5763 Another Meaning dp+字符串hash || DP+KMP的更多相关文章
- 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/ ...
- CodeForces7D 字符串hash + dp
https://cn.vjudge.net/problem/20907/origin 长度是 n 的字符串 s,如果它自身是回文数,且它的长度为 的前缀和后缀是 (k - )-回文数,则它被称作 k- ...
- [CQOI2014][bzoj3507] 通配符匹配 [字符串hash+dp]
题面 传送门 思路 0x01 KMP 一个非常显然而优秀的想法:把模板串按照'*'分段,然后对于每一段求$next$,'?'就当成可以对于任意字符匹配就行了 对于每个文本串,从前往后找第一个可以匹配的 ...
- HDU 1880 魔咒词典 (字符串hash)
<题目链接> 题目大意: 就是每个字符串有一个配套的对应字符串,询问的时候,无论输出其中的哪一个字符串,输出另一个,如果不存在这个字符串,直接输出"what?". 解题 ...
- 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 题意: 给出一个字符串和一个模式串,模式串有两种意思,问这句话有几种意思. 思路:因为肯定要去字符串去找模 ...
- 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 (KMP/哈希+DP)
题目大意:给你两个串,一长一短,如果长串中某个子串和短串完全相同,则这个子串可以被替换成"#",求长串所有的表达形式....... 比如"hehehehe"和& ...
随机推荐
- 转载:IntelliJ IDEA 2016.2 配置Tomcat 运行Web项目
以前都用MyEclipse写程序的 突然用了IDEA各种不习惯的说 借鉴了很多网上好的配置办法,感谢各位大神~ 前期准备 IDEA.JDK.Tomcat请先在自己电脑上装好 好么~ 博客图片为主 请多 ...
- C#设计模式(11)——外观模式
一.概念 外观模式提供了一个统一的接口,用来访问子系统中的一群接口.外观定义了一个高层接口,让子系统更容易使用.使用外观模式时,我们创建了一个统一的类,用来包装子系统中一个或多个复杂的类,客户端可以直 ...
- mysql auto reset
参数说明: •相关参数说明: •dataSource: 要连接的 datasource (通常我们不会定义在 server.xml) defaultAutoCommit: 对于事务是否 autoCom ...
- Java探索之旅(18)——多线程(2)
1 线程协调 目的对各线程进行控制,保证各自执行的任务有条不紊且有序并行计算.尤其是在共享资源或者数据情况下. 1.1 易变volatile cache技术虽然提高了访问数据的效率,但是有可能导致主存 ...
- [51nod]多重背包模板
https://www.51nod.com/tutorial/course.html#!courseId=11 题目大意: 有$N$种物品和一个容量为$W$的背包.第$i$种物品最多有$c[i]$件可 ...
- python的语法糖
# -*- coding: utf-8 -*-def deco(func): print("before myfunc() called.") func() print(" ...
- 从PCD文件中读取点云数据
博客转载自:http://www.pclcn.org/study/shownews.php?lang=cn&id=84 在本小节我们学习如何从PCD文件中读取点云数据. 代码 章例1文件夹中, ...
- byte和int转换
byte b1=1,b2=2,b3,b6; final byte b4=4,b5=6; b6=b4+b5; b3=(b1+b2); System.out.println(b3+b6); b3=b1+b ...
- Java堆内存划分
根据对象的存活率(年龄)Java堆内存划分为3种,新生代,老年代,永久代: 1.新生代 比如我们在方法中区new一个对象,那这方法调用完毕后,对象就会被回收,这就是一个典型的新生代对象. 现在的商业虚 ...
- redis学习总结2
redis配置文件说明:以下这篇文章已经说明的很明白了.感谢~ http://blog.sina.com.cn/s/blog_636415010101970j.html