poj2406 (KMP next array)

Source: Internet
Author: User

The main idea: give a string to ask it up by how many of the same string composition

If the abababab is made up of 4 AB

Analysis:

The application of the next array in KMP to the minimum cyclic section

For example

Ababab next[6] = 4; That

Ababab

Ababab

The 1~4 bit is the same as the 2~6 bit

So the first two

is equal to 3, 4 bits

3, 4 bits equals 5, 6 bits

......

So if we can divide it, we'll loop to the end.

If not divisible

The last remaining few are not in the loop.

For example

1212121

1212121

The last remaining 1 cannot be equal to the cyclic section

Code:

1#include <iostream>2#include <cstdio>3#include <cstring>4 using namespacestd;5 6 Const intMAXN =1000005;7 8 intNEXT[MAXN];9 Ten void Get(Char*s) { One     intL =strlen (s); A     intj =0, k =-1; -next[0] = -1; -      while(J <l) { the         if(k = =-1|| S[J] = =S[k]) { -NEXT[++J] = + +K; -}Else { -K =Next[k]; +         } -     } + } A CharS[MAXN]; at  - intMain () { -      while(gets (s)) { -         if(strcmp (s),".") ==0) { -              Break; -         } in         Get(s); -         intAns =1; to         intL =strlen (s); +         if(l% (l-next[l]) = =0) { -ans = l/(L-next[l]); the         } *printf"%d\n", ans); $     }Panax Notoginseng}
View Code

poj2406 (KMP next array)

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.