博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj 3773(KMP)
阅读量:6243 次
发布时间:2019-06-22

本文共 5068 字,大约阅读时间需要 16 分钟。

KMP基础题,一开始思路没有理清楚导致写了好久。。。 悲催。

要搞清楚题意,题目所围绕的是KMP算法中对自身串进行匹配的那一步

假设有两个串 S,T , 我们要求的是S是否存在T中。

其中我用的save[i],表示在i位置的后save[j]-1和S串的前save[i]-1个相等, 然后根据这个性质就可以解决

 

String-Matching Automata
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 113   Accepted: 70

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

ababaca0

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 01 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 02 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 03 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 04 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 05 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 06 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 07 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

 
 
#include 
#include
#include
using namespace std;char g[10010];int ans[10010][30];int save[10010];int main(){ while(scanf("%s",g)&&g[0]!='0') { int len=strlen(g); memset(ans,0,sizeof(ans)); memset(save,0,sizeof(save)); ans[0][g[0]-'a']=1; int j=0; int cnt=1; for(int i=1;i<=len;i++,cnt++) { if(i != len) ans[cnt][ g[i]-'a' ]=(i+1); ans[cnt][ g[0]-'a' ]=max(1,ans[cnt][ g[0]-'a' ]); int tj=i-1; while(1) { if(tj<=0) break; ans[cnt][ g[ save[tj] ] -'a' ]=max(ans[cnt][ g[ save[tj] ] -'a' ],save[tj]+1); tj = save[tj]-1; } if(i==len) break; ///// while(g[i]!=g[j]&&j!=0) { j=save[j-1]; } if(g[i]==g[j]) { j++; } save[i]=j; // } for(int i=0;i<=len;i++) { printf("%d",i); for(int j=0;j<26;j++) printf(" %d",ans[i][j]); printf("\n"); } } return 0;}

 

转载地址:http://risia.baihongyu.com/

你可能感兴趣的文章
iOS H5容器的一些探究(一):UIWebView和WKWebView的比较和选择
查看>>
activity启动模式
查看>>
如何将页面设置为微信端才能打开
查看>>
centos7如何关闭防火墙
查看>>
iOS开发中你是否遇到这些经验问题
查看>>
cellery ImportError & AttributeError
查看>>
正则表达式
查看>>
算法实验题 5.1 湖泊
查看>>
【235】Win10-Chrome 临时视频文件夹
查看>>
MongoDB GridFS——本质上是将一个文件分割为大小为256KB的chunks 每个chunk里会放md5标识 取文件的时候会将这些chunks合并为一个整体返回...
查看>>
Spring泛型依赖注入
查看>>
加速scp传输速度
查看>>
Kali Linux 安全渗透教程&lt;第三更&gt;1.2 安全渗透所需工具
查看>>
ios 使用Safari浏览器跳转打开、唤醒app
查看>>
HDU 1520 Anniversary party(DFS或树形DP)
查看>>
Linux 安装Nginx具体图解教程
查看>>
Suricata的所有运行方式模式(图文详解)
查看>>
1355: [Baltic2009]Radio Transmission
查看>>
kaldi的TIMIT实例三
查看>>
Prolog 逻辑推导语言
查看>>