Hust ACM/ICPC online judge problem 1010-Problem Solving report

Source: Internet
Author: User

Original question link:

Http://acm.hust.edu.cn/thx/problem.php? Id = 1010

 

It is not difficult to understand this question. Generally, the first solution is brute-force solution. However, for ACM, the brute-force solution usually gets TLE, that is, timeout.

This question is actually being investigated by KMPAlgorithm. For the introduction of KMP algorithm, see: http://baike.baidu.com/view/659777.htm

The KMP algorithm is a classic algorithm for string matching. It defines a unique feature vector for each string to match characters one by one, making the complexity of the algorithm from the general O (M * n) changed to O (m + n), greatly shortening the matching time. The key to this question is to find the regularity of feature vectors to quickly obtain the answer.

 

The source code is as follows:

# Define hust_1010 # ifdef hust_1010 # include <iostream> # include <string. h> # include <stdio. h> using namespace STD; # define n 1000002 char B [N]; int nextx [N]; void getnext (INT next [], char s [], int L) {int I = 1, j = 0; next [1] = 0; while (I <L) {If (j = 0 | s [I] = s [J]) {I ++; j ++; next [I] = J ;} else {J = next [J] ;}} int main () {// freopen ("test \ 1010-input.txt", "r", stdin ); // freopen ("test \ 1010-output.txt", "W", stdout); int Len; int sum; Wh Ile (scanf ("% s", B + 1 )! = EOF) {sum = 0; Len = strlen (B + 1); If (LEN = 1) printf ("1 \ n"); else {getnext (nextx, b, Len); sum = len-nextx [Len]; If (B [Len] = B [Len % sum]) | (LEN % sum = 0 & (B [Len] = B [Sum]) printf ("% d \ n", sum ); elseprintf ("% d \ n", Len) ;}} return 0 ;}# endif

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.