先上题目:

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

Problem Description
The finite state automaton (FSA) is an important model of behavioral investigations in computer science, linguistics and many other areas. A FSA can be typically modeled as a string pattern recognizer described by a quintuple <Σ, S, s0, δ, F>, where:

Σ 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.

 
Input
Several lines, each line has one pattern consist of only lowercase alphabetic characters. The length of the longest pattern is 10000. The input ends with a separate line of ‘0’.
 
Output
For each pattern, output should contain (m + 1) lines(m is the length of the pattern). The nth line describes how the automaton changes its state from state (n-1) after reading a character. It starts with the state number (n – 1), and then 26 state numbers follow. The 1st state number p1 indicates that when the automaton is in state (n-1), it will transit to state p1 after reading a character ‘a’. The 2nd state number p2 indicates that when the automaton is in state (n-1), it will transit to state p2 after reading a character ‘b’… And so on.
 
Sample Input
ababaca
0
 
Sample Output
0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 1 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
2 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
3 1 4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
4 5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
5 1 4 6 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
6 7 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
7 1 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 
Source
 
  题意:给你一个串,问你当匹配到某个位置的时候如果当匹配的字母为'a'-'z'的时候分别需要转移的位置是哪里?
 
  解法可以用KMP+一点其他操作。另一种方法是直接写一个AC自动机,然后build了自动机以后就沿着Trie树上输入的单词对于某一个节点就直接打印这个节点的next[i]['a'~'z']就可以了。
 
上代码:
 
 #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的更多相关文章

  1. Binary String Matching

    问题 B: Binary String Matching 时间限制: 3 Sec  内存限制: 128 MB提交: 4  解决: 2[提交][状态][讨论版] 题目描述 Given two strin ...

  2. NYOJ之Binary String Matching

    Binary String Matching 时间限制:3000 ms  |  内存限制:65535 KB 难度:3 描述     Given two strings A and B, whose a ...

  3. ACM Binary String Matching

    Binary String Matching 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 Given two strings A and B, whose alp ...

  4. 南阳OJ----Binary String Matching

    Binary String Matching 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 Given two strings A and B, whose alp ...

  5. Binary String Matching(kmp+str)

    Binary String Matching 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 Given two strings A and B, whose alp ...

  6. Aho - Corasick string matching algorithm

    Aho - Corasick string matching algorithm 俗称:多模式匹配算法,它是对 Knuth - Morris - pratt algorithm (单模式匹配算法) 形 ...

  7. [POJ] String Matching

    String Matching Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4074   Accepted: 2077 D ...

  8. String Matching Content Length

    hihocoder #1059 :String Matching Content Length 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 We define the ...

  9. HDU 3374 String Problem (KMP+最大最小表示)

    HDU 3374 String Problem (KMP+最大最小表示) String Problem Time Limit: 2000/1000 MS (Java/Others)    Memory ...

随机推荐

  1. SpringBoot集成MybatisPlus解决Mapper文件修改后动态刷新的问题

    很多人在使用SpringBoot集成Mybatis或者MybatisPlus的时候在查询复杂的情况下会写mapper文件,虽然说MyBatisPlus提供了常用的增删查改,但还是难以应付复杂的查询.关 ...

  2. 数据结构之顺序队列(C实现)

    一.队列是什么 队列是一种可以实现“先进先出”的存储结构. 队列通常可以分为两种类型: 一.顺序队列,采用顺序存储,当长度确定时使用. 顺序队列又有两种情况: ①使用数组存储队列的称为静态顺序队列. ...

  3. ACM博弈论总结

    一.Bash博弈 1.问题模型:只有一堆n个物品,两人轮流从这堆物品中取物,最多取m个,最后取光者胜. 2.解决思路:当n=m+1时,由于一次最多取m个,无论先取者拿走多少个,后取者都能一次拿走剩余的 ...

  4. php pdo oracle

    <?php/** * Created by mestars. * User: mestars * Date: 6/13/16 * Time: 10:52 PM */header('Access- ...

  5. [转].net cookie版购物车

    本文转自:http://www.sulong.cc/article/program/aspx/110613114249.html #region 添加到购物车AddShoppingCar /// &l ...

  6. idea工程jdk设置问题

    经常用idea的朋友,会遇到一个问题,那就是你在单测的时候,会报一个jdk的错,截图如下: 我的解决方案是在pom.xml里配置一个节点: <properties> <maven.c ...

  7. C#和Java在语法上的差异(原创,持续更新中)

    1.switch  C#一直支持String类型 Java直到1.7才支持 2.C#里String有Length属性 Java里是Length方法 3.C#中修饰class的sealed效果与Java ...

  8. WordPress腾讯云存储搭建教程,完美解决

    写在前面的话: 为什么会有今天的话题:WordPress+腾讯云存储? 因为博主不想使用七牛云,也不想使用又拍云,所以才有了今天的话题. 在使用腾讯云存储的过程中是很不顺利的,万幸的是现在终于完美融合 ...

  9. Docker在Ubuntu16.04上安装

    转自:http://blog.51cto.com/collen7788/2047800 1.添加Docker源 sudo apt-get update 2.增加CA证书 sudo apt-get in ...

  10. 面试中的一些小问题之html5和html4的区别?

    HTML5建立的一些新规则: 新特性应该基于HTML.CSS.DOM.JavaScript: 减少对外部插件的需求,如flash将会用video标签和audio标签代替: 更加优秀的错误处理: 更多取 ...