POJ 2778 DNA Sequence

Problem : 给m个只含有(A,G,C,T)的模式串(m <= 10, len <=10), 询问所有长度为n的只含有(A,G,C,T)的串中有多少个不含有模式串的串。(n<=2000000000)

Solution :首先对所有模式串建立AC自动机。然后dp[i][j]表示长度为i,走到AC自动机的节点j这样的字符串满足条件的个数有多少,用AC自动机的边写出状态转移方程然后用矩阵快速幂加速运算。

#include <iostream>
#include <string>
#include <queue> using namespace std; const int N = 208;
const int mo = 100000; int id[128]; struct Matrix
{
int n;
int a[N][N];
Matrix(int n_, int p)
{
n = n_;
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
{
a[i][j] = 0;
if (i == j) a[i][j] = p;
}
}
friend Matrix operator *(Matrix A, Matrix B)
{
Matrix C(A.n, 0);
for (int i = 0; i < A.n; ++i)
for (int j = 0; j < A.n; ++j)
for (int k = 0; k < A.n; k++)
C.a[i][j] = (C.a[i][j] + 1ll * A.a[i][k] * B.a[k][j] % mo) % mo;
return C;
}
void print()
{
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < n; ++j) cout << a[i][j] << " ";
cout << endl;
}
}
}; struct AC_Automan
{
int next[N][4];
int fail[N];
int cnt[N];
int root, tot; int newnode()
{
for (int i = 0; i <= 3; ++i) next[tot][i] = -1;
fail[tot] = cnt[tot] = -1;
return tot++;
}
void clear()
{
tot = 0;
root = newnode();
}
void insert(const string &s)
{
int p = root;
for (int i = 0, len = s.length(); i < len; ++i)
{
if (next[p][id[s[i]]] == -1) next[p][id[s[i]]] = newnode();
p = next[p][id[s[i]]];
}
cnt[p] = 1;
}
void build()
{
queue <int> Q;
Q.push(root);
while (!Q.empty())
{
int p = Q.front(); Q.pop();
for (int i = 0; i < 4; ++i)
{
if (~next[p][i])
{
if (p == root) fail[next[p][i]] = root;
else fail[next[p][i]] = next[fail[p]][i];
Q.push(next[p][i]);
}
else
{
if (p == root) next[p][i] = root;
else next[p][i] = next[fail[p]][i];
}
}
}
}
Matrix power(Matrix A, int y)
{
Matrix B(tot, 1);
while (y)
{
if (y & 1) B = B * A;
A = A * A;
y >>= 1;
}
return B;
} void solve(int num)
{
Matrix A(tot, 0);
for (int i = 0; i <= tot; ++i)
{
for (int j = 0; j < 4; ++j)
{
int flag = 1;
for (int temp = next[i][j]; temp != root; temp = fail[temp])
{
if (~cnt[temp]) flag = 0;
}
A.a[i][next[i][j]] += flag;
}
}
A = power(A, num);
int ans = 0;
for (int i = 0; i < tot; ++i) ans = (ans + A.a[0][i]) % mo;
cout << ans << endl;
}
}ac; int main()
{
cin.sync_with_stdio(0);
id['A'] = 0; id['G'] = 1; id['C'] = 2; id['T'] = 3;
int m, n;
while (cin >> m >> n)
{
ac.clear();
for (int i = 1; i <= m; ++i)
{
string s; cin >> s;
ac.insert(s);
}
ac.build();
ac.solve(n);
}
}

最新文章

  1. 安卓CPU性能测试
  2. MongDB/C# 杂项
  3. js使用Switch达到切换不同颜色的效果
  4. CSS应用内容补充及小实例
  5. Effective Java 35 Prefer annotations to naming patterns
  6. C# 从数据库中删除,插入,修改 索引选中条目
  7. Stylus Studio的安装与卸载
  8. 通过硬件层提高Android动画的性能
  9. floyd+动态规划 hdu-4571-Travel in time
  10. 【从零学习openCV】IOS7下的人脸检測
  11. 点击TextView浏览器打开指定网页
  12. vue2.0+ 从插件开发到npm发布
  13. Java动态代理实现及实际应用
  14. 原生JS和jQuery操作DOM的区别小结
  15. eShopOnContainers 看微服务 ①:总体概览
  16. PHP7语法知识(三):时间与日期、表单、类与对象、正则表达式、错误异常处理、图像处理
  17. note 3 变量与简单I/O
  18. Ex 6_5棋子放置问题_第八次作业
  19. IE9及以下版本获取上传文件的大小
  20. maven-javadoc-plugin 出现错误Unsupported major.minor version 51.0

热门文章

  1. SPFarm.local返回值为null
  2. 腾讯云COS对象存储的简单使用
  3. HttpServletRequest对象,自己学习的心得。
  4. hibernate对象状态 的小问题
  5. Jvisualvm--JAVA性能分析工具
  6. 关于mapState和mapMutations和mapGetters 和mapActions辅助函数的用法及作用(四)-----mapActions
  7. 洛谷 P1339 [USACO09OCT]热浪Heat Wave (堆优化dijkstra)
  8. Java Socket 连接 Client端 和 Server端
  9. Server.MapPath() 用法
  10. myeclipse出现Failed to load JavaHL Library.