LightOj 1334 - Genes in DNA
来源:互联网 发布:最优化方法好过吗 编辑:程序博客网 时间:2024/06/08 08:55
很好的KMP题目,正反两次KMP (利用DFA的知识概念,统计每个节点的失败节点到根节点的的距离,即代码中的dis数组) ,接下来的问题就迎刃而解了
#include<iostream>#include<cstdio>#include<cmath>#include<cstring>#include<algorithm>#include<queue>#include<set>#include<cstring>#include<vector>#include<bitset>#include<iterator>#include<list>#include<map>#include<cstdlib>#include<ctime>#include<utility>using namespace std;#define L p->ch[0]#define R p->ch[1]#define KeyTree root->ch[1]->ch[0]typedef unsigned long long LL;typedef pair<int, int> PII;typedef vector<int> VI;const int maxn = 1111111;const int INF = 1 << 30;const int mod = (int) (1e8);const LL X = 123;template<typename T>T gcd(T a, T b) {return b ? gcd(b, a % b) : a;}int Next[maxn];int dis[maxn];char s[maxn], t[maxn];int a[maxn], b[maxn];void getnext(char s[], int n) {int i = 0, j;Next[0] = j = -1;dis[0] = 0;while (i < n) {if (j == -1 || s[i] == s[j]) {Next[++i] = ++j;dis[i] = dis[j] + 1;} else {j = Next[j];}}}int KMP(char s[], int n, char t[], int m, int c[]) {int i = 0, j = 0, ans = 0;while (i < n) {if (j == -1 || s[i] == t[j]) {++i, ++j;c[i - 1] = dis[j] - (j == m);} else {j = Next[j];}if (j == m) {++ans;j = Next[j];}}return ans;}int main() {//ios::sync_with_stdio(false);int T, cas;scanf("%d", &T);for (cas = 1; cas <= T; ++cas) {scanf("%s%s", s, t);int n = strlen(s);int m = strlen(t);getnext(t, m);KMP(s, n, t, m, a);reverse(t, t + m);reverse(s, s + n);getnext(t, m);KMP(s, n, t, m, b);LL ans = 0;reverse(b, b + n);ans = 0;a[n] = b[n] = 0;for (int i = 0; i < n; ++i) {ans = ans + 1LL * a[i] * b[i + 1];}printf("Case %d: %lld\n", cas, ans);}return 0;}
- LightOj 1334 - Genes in DNA
- Lightoj-1334 Genes in DNA(kmp)
- Light OJ 1334 1334 Genes in DNA(KMP)
- Light OJ 1334 Genes in DNA KMP+DP
- Light OJ 1334 Genes in DNA (扩展KMP)
- LightOJ 1224 DNA Prefix
- DNA Sequence LightOJ
- genes
- LightOJ 1224 - DNA Prefix(字典树)
- LightOJ 1224 DNA Prefix(字典树)
- lightOJ 1244 DNA Prefix (字典树)
- Circle in Square LightOJ
- LightOJ 1073 DNA Sequence (状压DP+字符串比较)
- LightOJ 1022 Circle in Square
- lightoj-1010-Knights in Chessboard
- LightOJ - 1088 Points in Segments
- LightOJ 1010 Knights in Chessboard
- LightOj-1022-Circle in Square
- mysql中Table is read only的解决方法
- JavaScript的单线程性质以及定时器的工作原理
- SDK技术的发展进程以及了解概况
- 软件测试过程
- 格斗游戏动作设计和制作 攻击受击判定
- LightOj 1334 - Genes in DNA
- 最大子数组和问题分析总结
- NYOJ 523 (深搜,不可用的方法)
- 疯狂Java练习题 给定不同半径,用Math类画圆
- 数据恢复软件设计与实现(八)
- linux 常用命令
- CentOs安装无线网卡后,一段时间后不用,网络自动断开的解决办法
- python操作Excel读写--使用xlrd
- 当当网商家api接口取商品属性php部分代码