HDU - 3407 - String-Matching Automata
先上题目:
String-Matching Automata
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 215 Accepted Submission(s): 140
Σ is the input alphabet (a finite nonempty set of symbols).
S is a finite nonempty set of states.
s0 is an element in S designated as the initial state.
δ is a function δ: S × Σ → S known as the transition function.
F is a (possibly empty) subset of S whose elements are designated as the final states.
An FSA with the above description operates as follows:
At the beginning, the automaton starts in the initial state s0.
The automaton continuously reads symbols from its input, one symbol at a time, and transits between states according to the transition function δ. To be specific, let s be the current state and w the symbol just read, the automaton moves to the state given by δ(s, w).
When the automaton reaches the end of the input, if the current state belongs to F, the string consisting sequentially of the symbols read by the automaton is declared accepted, otherwise it is declared rejected.
Just as the name implies, a string-matching automaton is a FSA that is used for string matching and is very efficient: they examine each character exactly once, taking constant time per text character. The matching time used (after the automaton is built) is therefore Θ(n). However, the time to build the automaton can be large.
Precisely, there is a string-matching automaton for every pattern P that you search for in a given text string T. For a given pattern of length m, the corresponding automaton has (m + 1) states {q0, q1, …, qm}: q0 is the start state, qm is the only final state, and for each i in {0, 1, …, m}, if the automaton reaches state qi, it means the length of the longest prefix of P that is also a suffix of the input string is i. When we reaches state qm, it means P is a suffix of the currently input string, which suggest we find an occurrence of P.
The following graph shows a string-matching automaton for the pattern “ababaca”, and illustrates how the automaton works given an input string “abababacaba”.
Apparently, the matching process using string-matching automata is quite simple (also efficient). However, building the automaton efficiently seems to be tough, and that’s your task in this problem.
0
#include <cstdio>
#include <cstring>
#include <queue>
#define MAX 10002
using namespace std; struct Trie{
int next[MAX][],fail[MAX],end[MAX],num[MAX][];
int root,L; int newnode(){
for(int i=;i<;i++){ next[L][i]=-; num[L][i]=;}
end[L++]=;
return L-;
}
void init(){
L=; root=newnode();
} void insert(char buf[]){
int len=strlen(buf);
int now = root;
for(int i=;i<len;i++){
if(next[now][buf[i]-'a']==-){
next[now][buf[i]-'a']=newnode(); }
now=next[now][buf[i]-'a'];
}
end[now]++;
} void build(){
queue<int> Q;
fail[root]=root;
for(int i=;i<;i++){
if(next[root][i]==-) next[root][i]=root;
else{
fail[next[root][i]]=root;
Q.push(next[root][i]);
}
}
while(!Q.empty()){
int now=Q.front();
Q.pop();
for(int i=;i<;i++){
if(next[now][i]==-) next[now][i]=next[fail[now]][i];
else{
fail[next[now][i]]=next[fail[now]][i];
Q.push(next[now][i]);
}
}
}
} void print(char buf[]){
int len=strlen(buf);
int now=root;
for(int i=;i<=len;i++){
printf("%d",i);
for(int j=;j<;j++) printf(" %d",next[now][j]);
printf("\n");
now=next[now][buf[i]-'a'];
}
}
}; Trie ac;
char s[MAX]; int main()
{
//freopen("data.txt","r",stdin);
while(scanf("%s",s),strcmp(s,"")){
ac.init();
ac.insert(s);
ac.build();
ac.print(s);//printf("\n");
}
return ;
}
/*3407*/
HDU - 3407 - String-Matching Automata的更多相关文章
- Binary String Matching
问题 B: Binary String Matching 时间限制: 3 Sec 内存限制: 128 MB提交: 4 解决: 2[提交][状态][讨论版] 题目描述 Given two strin ...
- NYOJ之Binary String Matching
Binary String Matching 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 Given two strings A and B, whose a ...
- ACM Binary String Matching
Binary String Matching 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 Given two strings A and B, whose alp ...
- 南阳OJ----Binary String Matching
Binary String Matching 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 Given two strings A and B, whose alp ...
- Binary String Matching(kmp+str)
Binary String Matching 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 Given two strings A and B, whose alp ...
- Aho - Corasick string matching algorithm
Aho - Corasick string matching algorithm 俗称:多模式匹配算法,它是对 Knuth - Morris - pratt algorithm (单模式匹配算法) 形 ...
- [POJ] String Matching
String Matching Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 4074 Accepted: 2077 D ...
- String Matching Content Length
hihocoder #1059 :String Matching Content Length 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 We define the ...
- HDU 3374 String Problem (KMP+最大最小表示)
HDU 3374 String Problem (KMP+最大最小表示) String Problem Time Limit: 2000/1000 MS (Java/Others) Memory ...
随机推荐
- Django day 34 过滤课程,登录,redis,python操作redis
一:过滤课程, 二:登录 三:redis, 四:python操作redis
- 学习http协议的三次握手和四次挥手 ~~笔记
http协议是基于tcp协议的 所以应该说是tcp协议的三次握手和四次挥手 SYN:请求建立连接,并在其序列号的字段进行序列号的初始值设定.建立连接,设置为1 FIN:用来释放一个连接.FIN=1表 ...
- ElementaryOS 0.4快速配置工具
使用方法: 终端执行 wget http://linux-1251056822.costj.myqcloud.com/elementary_config && bash element ...
- Hadoop的数据采集框架
问题导读: Hadoop数据采集框架都有哪些? Hadoop数据采集框架异同及适用场景? Hadoop提供了一个高度容错的分布式存储系统,帮助我们实现集中式的数据分析和数据共享.在日常应用中我们比如要 ...
- 关于java日期输出格式
String.format("%tY%tm", new Date(), new Date()): //201905 String.format("%tY-%tm" ...
- python自动化--语言基础四模块、文件读写、异常
模块1.什么是模块?可以理解为一个py文件其实就是一个模块.比如xiami.py就是一个模块,想引入使用就在代码里写import xiami即可2.模块首先从当前目录查询,如果没有再按path顺序逐一 ...
- activity间数据传递
传递简单数据 创建两个activity,FirstActivity和TwoActivity,这里将会将数据从FisrtActivity传给TwoActivity. 创建完activity的目录界面如下 ...
- error C2143: syntax error : missing ';' before '}'
今天弄Tab控件,干了一件非常愚蠢的事,没有去声明头文件.这也是今天要记录的问题,提示如下各种 前面一个符号是错误的.如果初学者遇到,算作一个提示,记得声明新类的.h 头文件 标签空间再进一步.cpp ...
- 创建一个TCP服务器端通信程序的步骤
创建一个TCP服务器端通信程序的步骤: 1). 创建一个ServerSocket 2). 从ServerSocket接受客户连接请求 3). 创建一个服务线程处理新的连接 4). 在服务线程中,从so ...
- 03C++基本数据类型
基本数据类型 2.2.1整型数据 短整型(short int) 有符号短整型(signed short int) 无符号短整型(unsigned short int) 一般整型(int) 有符号一般整 ...