SGU 201 Non Absorbing DFA (DP)
转载请注明出处,谢谢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)的更多相关文章
- 201. Non Absorbing DFA
题意好难看懂的说... 有限状态自动机DFA是这么一个有序组<Σ, U, s, T, phi>:Σ代表输入字符集,表示此自动机的工作范围:U代表所有的状态集合:s是初始状态:T是最终状态: ...
- LightOJ 1033 Generating Palindromes(dp)
LightOJ 1033 Generating Palindromes(dp) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid= ...
- lightOJ 1047 Neighbor House (DP)
lightOJ 1047 Neighbor House (DP) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87730# ...
- UVA11125 - Arrange Some Marbles(dp)
UVA11125 - Arrange Some Marbles(dp) option=com_onlinejudge&Itemid=8&category=24&page=sho ...
- 【POJ 3071】 Football(DP)
[POJ 3071] Football(DP) Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4350 Accepted ...
- 初探动态规划(DP)
学习qzz的命名,来写一篇关于动态规划(dp)的入门博客. 动态规划应该算是一个入门oier的坑,动态规划的抽象即神奇之处,让很多萌新 萌比. 写这篇博客的目标,就是想要用一些容易理解的方式,讲解入门 ...
- Tour(dp)
Tour(dp) 给定平面上n(n<=1000)个点的坐标(按照x递增的顺序),各点x坐标不同,且均为正整数.请设计一条路线,从最左边的点出发,走到最右边的点后再返回,要求除了最左点和最右点之外 ...
- 2017百度之星资格赛 1003:度度熊与邪恶大魔王(DP)
.navbar-nav > li.active > a { background-image: none; background-color: #058; } .navbar-invers ...
- Leetcode之动态规划(DP)专题-详解983. 最低票价(Minimum Cost For Tickets)
Leetcode之动态规划(DP)专题-983. 最低票价(Minimum Cost For Tickets) 在一个火车旅行很受欢迎的国度,你提前一年计划了一些火车旅行.在接下来的一年里,你要旅行的 ...
随机推荐
- HTML5学习笔记之Input类型
Input类型——email email类型用于包含email地址的输入域,在输入地址时会自动验证email域的值 例:Email:<input type="email" n ...
- 权威验证:MSDN会明确告诉你下载的光盘镜像是否正宗微软原版
MSDN是微软官方网站.这个网站的职能之一,就是向MSDN订户(付费相当高昂)提供Microsoft Windows资源,即大家通常说的操作系统光盘镜像.相信大家手头都有不少这类下载,但究竟是否微软的 ...
- (7) 引用Objective-C class library
原文 引用Objective-C class library 这个范例是如何在Xamarin.ios中去使用一个我们自行在Xcode中开发的Objective-c Class Library. 主要会 ...
- 运用JavaScript构建你的第一个Metro式应用程序(onWindows 8)(二)
原文 http://blog.csdn.net/zhangxin09/article/details/6793330 先前的学习中,我们已经了解了 Metro式的 JavaScript 应用程序大致如 ...
- 剑指offer26 复杂链表的复制
/* struct RandomListNode { int label; struct RandomListNode *next, *random; RandomListNode(int x) : ...
- RDIFramework.NET(.NET快速信息化系统开发框架) Web版介绍
RDIFramework.NET(.NET快速信息化系统开发框架) Web版介绍 B/S结构(Browser/Server,浏览器/服务器模式),是WEB兴起后的一种网络结构模式,WEB浏览器是客户 ...
- (转)Web开发中最致命的小错误
Web开发中最致命的小错误 现在,有越来越多所谓的“教程”来帮助我们提高网站的易用性.本文收集了一些在 Web 开发中容易出错和被忽略的小问题,并且提供了参考的解决方案,以便于帮助 Web 开发者更好 ...
- iframe自适应高度代码
var adjustIframe = function (id) { var iframe = document.getElementById(id) var idoc = iframe.conten ...
- VS2015使用scanf报错解决方案
版权声明:本文为博主原创文章,未经博主允许不得转载. 方法一:在程序最前面加#define _CRT_SECURE_NO_DEPRECATE: 方法二:在程序最前面加#define _CRT_SECU ...
- 错误:类Byte是公共的,应在名为Byte.java 的文件中声明public class Byte{}一个错误
解决:文件名是xyz,那你的这个类名也应该是xyz.