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"和& ...
随机推荐
- Apache+OpenSSL实现证书服务器提供HTTPS
通过 Linux+Apache+OpenSSL 实现 SSL ( Secure Socket Layer )证书服务器,提供安全的 HTTPS ( Hypertext Transfer Protoco ...
- 代码实现跟控制器跳转到storyBoard
- NetScaler VPX在Azure上的部署(二)
本文是Citrix的工程师协助完成.主要是Citrix的VPX的配置. 导入License 进入NetScaler 中点击管理许可 导入后将有提示,请确认重启. 配置Azure HA 由于 ...
- Linux ssh 不需要输入密码的方法
采用证书的方法可以解决ssh不需要输入密码的问题. 本文采用CentOS的操作系统,创建SSH的key,并在两台或多台机器间实现信任.从而实现SSH登录不需要输入密码的功能. 首先,在一台机器上创建S ...
- AI设计的若干规则阐述
转自:http://www.gameres.com/491742.html 一般来讲,网络游戏的AI历来就是很简单的AI.相比之下,很多单机游戏的AI就要得复杂一些.而笔者并未从事过大型单机游戏的AI ...
- python-queue知识点
1.dict获取value dict.get(key_name)2.三元运算 res,err=stdout.read(),stderr.read() #三元运算 result=res if res e ...
- 全文检索技术---solr
1 Solr介绍 1.1 什么是solr Solr 是Apache下的一个顶级开源项目,采用Java开发,它是基于Lucene的全文搜索服务器.Solr可以独立运行在Jetty.Tom ...
- Ubuntu 解决:当执行`sudo apt-get update`命令时 出现的 “apt-get 404 Not Found Package Repository Errors” 问题
Ubuntu 解决:当执行sudo apt-get update或者sudo apt-get install命令是出现的 "apt-get 404 Not Found Package Rep ...
- 树莓派 Learning 002 装机后的必要操作 --- 01 解决上网问题
树莓派 装机后的必要操作 - 解决上网问题 我的树莓派型号:Raspberry Pi 2 Model B V1.1 装机系统:NOOBS v1.9.2 树莓派 装机后的必要操作 解决上网问题 解决上网 ...
- Umbraco 的template使用的默认Model问题
Umbraco中的Template默认都继承自 Umbraco.Web.Mvc.UmbracoTemplatePage @inherits Umbraco.Web.Mvc.UmbracoTemplat ...