POJ 2778 DNA Sequence 题解&代码
来源:互联网 发布:网络博客大全评级 编辑:程序博客网 时间:2024/06/11 13:09
调了三个小时…2333333感觉自己傻逼了
是一道很有(ma)趣(fan)的AC自动机+矩阵快速幂
我们需要算出的初始矩阵是dp[i][j]是用一步从自动机的第i个位置到第j个位置有几种不产生病毒DNA的方式
然后乘n次就得到答案辣【答案当然是从初始位置(0)到最终位置i的方式数之和
被vim坑了一发…还是不太习惯,平时写习惯的矩阵快速幂迷之CE…
后来发现初始矩阵算对了但是还是答案太少…感谢样例简洁明了
我发现我是直接从有效节点开始寻找路径,事实则是我们应该从trie树的虚节点0开始寻找路径
即对于样例AA AT AC AG
我们第一步应该尝试0->A、0->T、0->C、0->G
我尝试的则是A->A、A->T、A->C、A->G
自然会少掉很多情况
但是不知道为什么别人没有给trie树设立虚节点也能过…大概是建立自动机的方式不一样…这个问题暂且搁置
#include <iostream>#include <cstdio>#include <cstring>using namespace std;int n,len,tot,temp;long long dp[105][105],base[105][105],ans[105][105];int ch[105][4],flag[105],fail[105],q[105];char s[105];int cal(char x){ if(x == 'A') return 0; if(x == 'T') return 1; if(x == 'C') return 2; if(x == 'G') return 3; return -1;}void newnode(int x, int v){ ch[x][v]=++tot;}void addtree(char s[]){ int x = 0, p = 0, len = strlen(s); while( p < len ) { temp = cal(s[p]); if ( !ch[x][temp] ) newnode(x,temp); x = ch[x][temp]; p++; } flag[x] = 1;}void buildtree(void){ int h = 0,p = 0; for(int i = 0; i < 4; i++) if( ch[0][i] ) { q[p++] = ch[0][i]; fail[ch[0][i]] = 0; } while( h < p ) { temp=q[h++]; flag[temp]|=flag[fail[temp]]; for(int i = 0; i < 4; i++) { if( ch[temp][i] ) { q[p++] = ch[temp][i]; fail[ch[temp][i]] = ch[fail[temp]][i]; } else ch[temp][i] = ch[fail[temp]][i]; } }}void mul(long long a[105][105], long long b[105][105]){ for (int i = 0; i <= tot; i++) for (int j = 0; j <= tot; j++) { ans[i][j] = 0; for (int k = 0; k <= tot; k++) ans[i][j] += a[i][k] * b[k][j]; ans[i][j] %= 100000; } for(int i = 0;i <= tot; i++) for(int j = 0; j <= tot; j++) a[i][j] = ans[i][j];}void pow(int k){ for(int i = 0;i <= tot; i++) for(int j = 0; j <= tot; j++) base[i][j] = dp[i][j]; while(k) { if(k & 1)mul(dp,base); mul(base,base); k>>=1; }}int main (void){ scanf("%d%d",&n,&len); for(int i = 0; i < n; i++) { scanf("%s", s); addtree(s); } buildtree(); for(int i = 0; i <= tot; i++) for(int j = 0; j < 4; j++) if( !flag[i] && !flag[ch[i][j]]) dp[i][ch[i][j]]++; pow(len-1); int ans = 0; for(int i = 0; i <=tot; i++) ans += dp[0][i]; printf("%d\n",ans % 100000); return 0;}
0 0
- POJ 2778 DNA Sequence 题解&代码
- 【POJ 2778】DNA Sequence 中文题意&题解&代码(C++)
- poj 2778 DNA Sequence
- POJ 2778 DNA Sequence
- poj 2778 DNA Sequence
- Poj 2778 DNA Sequence
- POJ 2778 DNA Sequence
- POJ 2778 DNA Sequence
- POJ 2778 DNA Sequence
- poj-2778 DNA Sequence
- POJ 2778DNA Sequence
- poj 2778 DNA Sequence
- POJ 2778 DNA Sequence
- [题解]POJ2778 DNA Sequence
- POJ 2778 DNA Sequence(AC自动机+矩阵)
- POJ 2778 DNA Sequence (AC自动机)
- 【POJ】2778 DNA Sequence AC自动机+矩阵
- POJ 2778 DNA Sequence (AC自动机 + dp)
- disksim raid 部分源代码解析
- 相对误差的传递
- TCP/IP基础(二)
- PHP实现冒泡、选择、插入和快速排序
- gravity 与layout_gravity的区别
- POJ 2778 DNA Sequence 题解&代码
- JAVA的包装类 http://blog.csdn.net/hjf19790118/article/details/7081925
- 节点动作
- zzulioj 1730: 通信基站 (全排列&DFS)好题
- 安卓开发中SQLite的使用
- PB中TreeView控件使用技巧
- 【ASP.NET】六大内置对象
- 【CodeForces】A. I Wanna Be the Guy
- HTTP状态码大全