HDU 5763 Another Meaning(DP+KMP)

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

题意:

给出一个字符串和一个模式串,模式串有两种意思,问这句话有几种意思。

思路:
因为肯定要去字符串去找模式串,所以首先用KMP计算next数组,然后用动态规划,d[i]表示分析到第i个字符时有多少种意思。

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cstring>
 4 #include<cstdio>
 5 #include<vector>
 6 #include<stack>
 7 #include<queue>
 8 #include<cmath>
 9 #include<map>
10 using namespace std;
11
12 const int maxn=1e5;
13 const int MOD=1000000007;
14
15 char a[maxn],b[maxn];
16 int _next[maxn];
17 int d[maxn];
18
19 void get_next()
20 {
21     int i=-1,j=0;
22     _next[0]=-1;
23     while(j<strlen(b))
24     {
25         if(i==-1||b[i]==b[j])
26             _next[++j]=++i;
27         else
28             i=_next[i];
29     }
30 }
31
32 int KMP(int n,int m)
33 {
34     int i, j;
35     i = j = 0;
36     while (i<n)
37     {
38         if (j == -1 || a[i] == b[j])
39         {
40             if(i>0)  d[i]=d[i-1];
41             i++;
42             j++;
43             if (j == m)  d[i-1]=(d[i-1-(j-1-_next[j-1])]+d[i-1-m]+1)%MOD;
44         }
45         else
46             j = _next[j];
47     }
48 }
49
50
51 int main()
52 {
53     //freopen("D:\\input.txt","r",stdin);
54     int T;
55     scanf("%d",&T);
56     for(int kase=1;kase<=T;kase++)
57     {
58         scanf("%s",&a);
59         scanf("%s",&b);
60         int n=strlen(a); int m=strlen(b);
61         memset(d,0,sizeof(d));
62         get_next();
63         KMP(n,m);
64         printf("Case #%d: ",kase);
65         printf("%d\n",d[n-1]+1);
66     }
67     return 0;
68 }
时间: 2024-10-10 14:42:05

HDU 5763 Another Meaning(DP+KMP)的相关文章

hdu 4763 Theme Section (简单KMP)

Theme Section Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1184    Accepted Submission(s): 621 Problem Description It's time for music! A lot of popular musicians are invited to join us in t

HDU 1058 Humble Numbers(DP,数)

题意  所有只能被2,3,5,7这4个素数整除的数称为Humble Number  输入n  输出第n个Humble Number 1是第一个humble number  对于一个Humble Number  a  有2*a,3*a,5*a,7*a都是Humble Number  可以以1为基数  依次展开即可得到一定范围内的Humble Number 用i,j,k,l分别记录 2,3,5,7分别乘到了第几个Humble Number  当前在计算第cnt个Humble Number  那么有

Hdu 3336 Count the String(DP+KMP)(好题)

题意:对于长度为len的字符串,我们知道它包含有len个前缀,现在要你统计出这个字符串里面,包含这些前缀的总个数. 思路:这题可以运用KMP的next数组来解,不过也太难想了吧orz,为了用next解这题想那么多也不算是很好的方法orz. 如何根据next数组的性质来解这道题,next数组的值是当前子串的后缀与前缀匹配的个数,所以根据这个性质把题待求的对象改一下:求每种字母作为结尾的串在原串中出现的次数.从最后一个字母分析,首先字符串本身就是以last字母作结,next[last]=x,所以又找

HDU 5763 Another Meaning(FFT)

[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5763 [题目大意] 给出两个串S和T,可以将S串中出现的T替换为*,问S串有几种表达方式. [题解] 我们定义数组f为S串中T出现的最后一个字母所在的位置,那么ans[i]=ans[i-1]+f[i-1]?ans[i-lenT]:0,一遍递推即可,所以关键就在于求出f数组了,f数组可以用kmp求,由于最近练FFT,用FFT求距离卷积匹配为0的位置,就是f数组了. [代码] #include <c

HDU 6153 A Secret(扩展kmp)

A Secret Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 256000/256000 K (Java/Others)Total Submission(s): 1530    Accepted Submission(s): 570 Problem Description Today is the birthday of SF,so VS gives two strings S1,S2 to SF as a present,wh

杭电 HDU ACM Bone Collector (dp 01)

Bone Collector Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 40414    Accepted Submission(s): 16794 Problem Description Many years ago , in Teddy's hometown there was a man who was called "Bo

HDU 5389 Zero Escape(dp解法)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5389 题面: Zero Escape Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 164    Accepted Submission(s): 73 Problem Description Zero Escape, is a vis

HDU 3613 Best Reward(扩展KMP)

[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=3613 [题目大意] 一个字符串的价值定义为,当它是一个回文串的时候,价值为每个字符的价值的和,如果不是回文串,价值为0,现在给出每种字符的价值.给出一个字符串,要求将其划分为两个子串,要求两个子串的价值和最大. [题解] 求出字符串S的反串T,以T为模板跑一遍S的exkmp就能得到S的后缀是否为回文串的信息,同理以S为模板跑一遍T就可以得到S的前缀是否是回文串的信息,枚举每个断点,取最大值即可.

HDU 2594 Simpsons’ Hidden Talents (字符串-KMP)

Simpsons' Hidden Talents Problem Description Homer: Marge, I just figured out a way to discover some of the talents we weren't aware we had. Marge: Yeah, what is it? Homer: Take me for example. I want to find out if I have a talent in politics, OK? M