Poj 3773 (KMP)

Source: Internet
Author: User

KMP basic questions. I wrote it for a long time because I didn't have a clear idea at the beginning... Sad reminder.

To clarify the meaning of the question, the question is centered on KMP.AlgorithmThe step for matching your own strings

Suppose there are two strings "S" and "T". We need to determine whether "S" exists in "T.

The Save [I] I used indicates that the save [J]-1 after the I position is equal to the Save [I]-1 before the S string, then, based on this nature, we can solve the problem.

 

String-matching Automata
Time limit:1000 ms   Memory limit:65536 K
Total submissions:113   Accepted:70

Description

The finite state automaton (FSA) is an important model of behavioral investors in computer science, linguistics and other areas. a fsa can be typically modeled as a string pattern recognizer described by a quintuple <Σ, S, S0, Delta, 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.
Delta is a function delta: S x Σ → 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 delta. to be specific, let s be the current state and W the symbol just read, the automation on moves to the state given by Delta (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 automation is built) is therefore seconds (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 how the automaton works given an input string "ababacaba ".

Apparently, the matching process using string-matching automata is quite simple (also efficient). However, building the automation on 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 shoshould contain (m + 1) lines (M is the length of the pattern ). the Nth line describes how the automaton changes its state from the staten-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 staten-1, it will transit to state P1 after reading a character 'a '. the 2N D State number P2 indicates that when the automaton is in staten-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 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 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 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 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 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 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 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 0 0 0 0 0 0 0

Source

2010 national programming Invitational contest host by zstu
# Include <stdio. h> # Include < String . H> # Include <Iostream> 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  ;} 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.