# 题解：HDU6629 String Mathching

### Problem Description

String matching is a common type of problem in computer science. One string matching problem is as following:

Given a string $S[0\cdots len−1]$, please calculate the length of the longest common prefix of $S[i\cdots len−1]$ and $S[0 \cdots len−1]$ for each $i>0$.

I believe everyone can do it by brute force.
The pseudo code of the brute force approach is as the following:

We are wondering, for any given string, what is the number of compare operations invoked if we use the above algorithm. Please tell us the answer before we attempt to run this algorithm.

### input

The first line contains an integer $T$, denoting the number of test cases.
Each test case contains one string in a line consisting of printable ASCII characters except space.

• $1 \leq T \leq 30$

• string length $\leq 106$ for every string

### Output

For each test, print an integer in one line indicating the number of compare operations invoked if we run the algorithm in the statement against the input string.

KMP算法讲解传送门：🚪

## AC代码

### 评论

ACM Front-end Learning Note Programming Solution