HDOJ 5763 Another Meaning
来源:互联网 发布:软件开发 需求文档 编辑:程序博客网 时间:2024/06/05 21:52
题意
给两个字符串a和b,b有两义性,求a一共有多少种可能的意思。
思路
首先KMP预处理出所有b是a子串的末尾位置,然后基础dp就可以了。
如果i位置是子串末尾则dp[i] = dp[i-1] + dp[i-lenb] + 1否则直接转移。
代码
#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>#include <vector>#include <queue>#include <stack>#include <set>#include <map>#include <bitset>#include <string>#include <math.h>#include <stdlib.h>#include <time.h>using namespace std;#define LL long long#define Lowbit(x) ((x)&(-x))#define lson l, mid, rt << 1#define rson mid + 1, r, rt << 1|1#define MP(a, b) make_pair(a, b)const int INF = 0x3f3f3f3f;const int MOD = 1000000007;const int maxn = 1e5 + 7;const double eps = 1e-8;const double PI = acos(-1.0);typedef pair<int, int> pii;bool ok[maxn];int dp[maxn];char a[maxn], b[maxn];void getnext(char *p, int n, int *next){ int i = 0, j = -1; next[0] = j; while (i < n) { while (j != -1 && p[j] != p[i]) j = next[j]; i++, j++; if (j >= n) next[i] = next[j-1]; else next[i] = j; }}void kmp(char *p, int n, char *s, int m){ int next[10005]; getnext(p, n, next); int i = 0, j = 0; while (i < m) { while (j != -1 && p[j] != s[i]) j = next[j]; i++, j++; if (j == n) { ok[i-1] = 1; j = next[j]; } }}int main(){ //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); int T; scanf("%d", &T); for (int ncase = 1; ncase <= T; ncase++) { scanf("%s%s", a, b); memset(ok, 0, sizeof(ok)); kmp(b, strlen(b), a, strlen(a)); memset(dp, 0, sizeof(dp)); dp[0] = ok[0]; int len = strlen(a); int lenb = strlen(b); for (int i = 1; i < len; i++) { dp[i] = dp[i-1]; if (ok[i]) dp[i] = (dp[i] + dp[i-lenb] + 1) % MOD; } printf("Case #%d: %d\n", ncase, dp[len-1] + 1); } return 0;}
0 0
- HDOJ 5763 Another Meaning
- HDOJ 5763 Another Meaning
- HDOJ 5763 Another Meaning(kmp+dp)
- HDU 5763 Another Meaning
- HDU 5763 Another Meaning
- HDU 5763 Another Meaning
- 【HDU】5763 Another Meaning
- hdu 5763 Another Meaning
- DP-HDU-5763-Another Meaning
- HDU 5763 Another Meaning (DP)
- 多校4 Another Meaning 5763
- 【47.92%】【hdu 5763】Another Meaning
- Another Meaning
- Another Meaning
- HDOJ 5763 (2016多校联合训练 Training Contest 4) Another Meaning
- HDU 5763 Another Meaning (DP)
- HDU-5763-Another Meaning(KMP+DP)
- Hdu-5763 Another Meaning(DP+KMP)
- Tornado Web服务器多进程启动的2个方法
- 60. 理解 Ajax 性能
- java正则表达式的简单入门
- Windows 上 Assimp(Open Asset Import Library) 源码的编译 及 Samples 的编译运行
- 阿里2017笔试(2)
- HDOJ 5763 Another Meaning
- C++读书笔记1:C语言基础知识
- Math对象方法
- tornado 多进程模式
- USB 3.0 对无线鼠标的干扰问题
- hduoj1879
- 【Python数据挖掘课程】五.线性回归知识及预测糖尿病实例
- Constructor详解
- Codeforces 722C - Destroying Array By Assassin