转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents    by---cxlove

题意:给出一个自动机,给出所有的转移,其中还有一个矩阵,如果(u,c)=0,直接转移,字符c被接收,否则也会转移但是字符c不会被转移,也就是下一个字符依旧是字符c。

http://acm.sgu.ru/problem.php?contest=0&problem=201

题意理解了很久。。。。。

自动机都给出了状态和转移,显然是个DP。。。

但是由于存在(u,c)=1的情况,即字符不变,所以需要预处理一下,得到最终接受字符的状态

记忆化搜索即可,可能出现死循环的情况,判定为不可转移

需要高精度

import java.util.*;
import java.io.*;
import java.math.*;
public class Solution {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
Task solver = new Task();
solver.solve(in, out);
out.close();
}
}
class Task{
static int go[][]=new int [1005][26];
static boolean end[]=new boolean [1005];
static int x[][]=new int [1005][26];
static int to[][]=new int [1005][26];
int dfs(int now,int ch){
if(x[now][ch]==0){
return to[now][ch]=go[now][ch];
}
if(to[now][ch]!=-1)
return to[now][ch];
to[now][ch]=0;
return to[now][ch]=dfs(go[now][ch],ch);
}
void solve(InputReader in,PrintWriter out){
String S=in.next();
int m=S.length();
int n=in.nextInt();
int s=in.nextInt();
int endcnt=in.nextInt();
for(int i=1;i<=n;i++)
end[i]=false;
for(int i=0;i<endcnt;i++){
int u=in.nextInt();
end[u]=true;
}
for(int i=1;i<=n;i++){
for(int j=0;j<m;j++)
go[i][j]=in.nextInt();
}
for(int i=1;i<=n;i++){
for(int j=0;j<m;j++){
x[i][j]=in.nextInt();
to[i][j]=-1;
}
}
for(int i=1;i<=n;i++){
for(int j=0;j<m;j++){
dfs(i,j);
}
}
int l=in.nextInt();
BigInteger dp[][]=new BigInteger [65][1005];
for(int i=0;i<=l;i++)
for(int j=1;j<=n;j++)
dp[i][j]=BigInteger.ZERO;
dp[0][s]=BigInteger.ONE;
for(int i=1;i<=l;i++){
for(int j=1;j<=n;j++){
for(int k=0;k<m;k++){
if(to[j][k]>0)
dp[i][to[j][k]]=dp[i][to[j][k]].add(dp[i-1][j]);
}
}
}
BigInteger ans=BigInteger.ZERO;
for(int i=1;i<=n;i++)
if(end[i])
ans=ans.add(dp[l][i]);
out.println(ans);
}
}
class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer; public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream));
tokenizer = null;
} public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
} public int nextInt() {
return Integer.parseInt(next());
}
}

SGU 201 Non Absorbing DFA (DP)的更多相关文章

  1. 201. Non Absorbing DFA

    题意好难看懂的说... 有限状态自动机DFA是这么一个有序组<Σ, U, s, T, phi>:Σ代表输入字符集,表示此自动机的工作范围:U代表所有的状态集合:s是初始状态:T是最终状态: ...

  2. LightOJ 1033 Generating Palindromes(dp)

    LightOJ 1033  Generating Palindromes(dp) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid= ...

  3. lightOJ 1047 Neighbor House (DP)

    lightOJ 1047   Neighbor House (DP) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87730# ...

  4. UVA11125 - Arrange Some Marbles(dp)

    UVA11125 - Arrange Some Marbles(dp) option=com_onlinejudge&Itemid=8&category=24&page=sho ...

  5. 【POJ 3071】 Football(DP)

    [POJ 3071] Football(DP) Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4350   Accepted ...

  6. 初探动态规划(DP)

    学习qzz的命名,来写一篇关于动态规划(dp)的入门博客. 动态规划应该算是一个入门oier的坑,动态规划的抽象即神奇之处,让很多萌新 萌比. 写这篇博客的目标,就是想要用一些容易理解的方式,讲解入门 ...

  7. Tour(dp)

    Tour(dp) 给定平面上n(n<=1000)个点的坐标(按照x递增的顺序),各点x坐标不同,且均为正整数.请设计一条路线,从最左边的点出发,走到最右边的点后再返回,要求除了最左点和最右点之外 ...

  8. 2017百度之星资格赛 1003:度度熊与邪恶大魔王(DP)

    .navbar-nav > li.active > a { background-image: none; background-color: #058; } .navbar-invers ...

  9. Leetcode之动态规划(DP)专题-详解983. 最低票价(Minimum Cost For Tickets)

    Leetcode之动态规划(DP)专题-983. 最低票价(Minimum Cost For Tickets) 在一个火车旅行很受欢迎的国度,你提前一年计划了一些火车旅行.在接下来的一年里,你要旅行的 ...

随机推荐

  1. [原创]linux简单之美(三)

    原文链接:linux简单之美(三) 在linux简单之美(二)中我们尝试使用了C库的函数完成功能,那么能不能用syscall方式来搞呢?显然可以! section .data ft db sectio ...

  2. Oracle EBS-SQL (AR-1):检查应收收款发生额

    SELECT SUM(nvl(dist.amount_dr,0))-SUM(nvl(dist.amount_cr,0)) FROM ar_cash_receipt_history_all hist,  ...

  3. 一款C++静态分析工具 —— CppDepend

    Wrote by mutouyun. (http://darkc.at/cppdepend/) 去年6月份的时候,CppDepend的一位技术社区经理(technical community mana ...

  4. Write operations are not allowed in read-only mode (FlushMode.MANUAL): Turn

    这个异常我在网上查看了很多资料,一般都说是hibernate的session问题,让重新两个方法,但是我以前用的时候没问题啊,所以一直找问题,终于这个bug让我找到了,就是因为我插入操作的时候用的是别 ...

  5. SQL Server 2008 批量插入数据时报错

    前几天在SQL Server 2008同步产品数据时,总是提示二进制文本被截断的错误,但是经过检查发现数据都符合格式要求. 百思不得其解,单独插入一条条数据则可以插入,但是批量导入则报错. 批量导入代 ...

  6. 面向对象程序设计-C++_课时17函数重载和默认参数

    函数重载,区别一是参数类型不同,二是参数个数不同. 默认参数可以多于1个,但必须放在参数序列的后部. 尽量不要用默认参数,会影响阅读 error C2668: “f”: 对重载函数的调用不明确 #in ...

  7. Nginx+Tomcat7+Mencached负载均衡集群部署笔记

    Nginx+Tomcat+Memcached负载均衡集群服务搭建 操作系统:CentOS6.5 本文档主要解说,怎样在CentOS6.5下搭建Nginx+Tomcat+Memcached负载均衡集群s ...

  8. iOS深入学习 (Block全面分析)

    本文翻译自苹果的文档,有删减,也有添加自己的理解部分. 如果有Block语法不懂的,可以参考fuckingblocksyntax,里面对于Block 为了方便对比,下面的代码我假设是写在ViewCon ...

  9. html5 绘制集合图形

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  10. <原>ASP.NET 学习笔记之应养成的良好习惯

    写ASP.NET时应有的良好习惯(不定时增加): 1.view的名称一定要与对应的actionMethod的名称相同:从原理上看,客户端通过url(一般形式为http://xxx/controller ...