HDU 3336 KMP

Count the string

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 11954    Accepted Submission(s): 5545


Problem Description
It is well known that AekdyCoin is good at string problems as well as number theory problems. When given a string s, we can write down all the non-empty prefixes of this string. For example:
s: "abab"
The prefixes are: "a", "ab", "aba", "abab"
For each prefix, we can count the times it matches in s. So we can see that prefix "a" matches twice, "ab" matches twice too, "aba" matches once, and "abab" matches once. Now you are asked to calculate the sum of the match times for all the prefixes. For "abab", it is 2 + 2 + 1 + 1 = 6.
The answer may be very large, so output the answer mod 10007.
 

Input
The first line is a single integer T, indicating the number of test cases.
For each case, the first line is an integer n (1 <= n <= 200000), which is the length of string s. A line follows giving the string s. The characters in the strings are all lower-case letters.
 

Output
For each case, output only one number: the sum of the match times for all the prefixes of s mod 10007.
 

Sample Input
1 4 abab
 

Sample Output
6
 

Author
foreverlin@HNU
 

Source


題目大意

給定一個字符串,求着個字符串所有的前綴在字符串中出現的總次數
比如樣例的
abab
前綴分別是a,ab,aba,abaa,在母串中出現的次數爲2,2,1,1,所以答案是6

解題思路

如果每一個前綴我們都去挨個匹配母串的話,很顯然我們將會面臨超時的問題,所以如果有好好的學KMP的話,我們會聯想到的是KMP裏面的NEXT數組,NEXT數組記錄的是要跳轉的位置,既然能跳轉,那麼就一定說明當前位置的前方有一段字符串是和前綴相同的,所以問題這裏就變得簡單了,只要NEXT處理一遍就可以了,如果NEXT存在,那麼答案(初始化答案爲n)加一就可以了.
如果你這麼做了,那麼你會AC,但是你的解法並不正確,或者可以說你對NEXT數組的理解不夠深刻,本題數據很水所以導致有的問題你沒有處理到也可以AC
這裏我舉一個例子 asasa,正確答案是9,但是如果按照上述做法是8
爲什麼會這樣呢,我們把前綴取出來分別是a,as,asa,asas,asasa
  他們的個數分別是3,2,2,1,1,若果按照原來的算法它的個數分別是2,2,2,1,1
        這裏我把NEXT的值列出來NEXT[0] = -1,NEXT[1] = 0,NEXT[2] = 0,NEXT[3] = 1,NEXT[4] = 2,NEXT[5] = 3,
        原因出在哪裏呢?我們在計算NEXT數組的時候沒有進行回溯就造成了這個問題,因爲NEXT[5] = 3,但是NEXT[3] = 1,
       這裏可以進行回溯,這說明什麼?說明前綴不止一個,當前前綴還包含若干個小前綴。
   理解了這個本題就做完了
#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
using namespace std;
const int maxn = 2000000 + 10;
char q[maxn],p[maxn];
int ne[maxn];
int getnext(char * a){
  int lena = strlen(a);
  int i = -1;
  int j = 0;
  ne[0] = -1;
  while(j < lena){
    if(i == -1 || a[i] == a[j]){
        i++;
        j++;
        ne[j] = i;
    }
    else{
        i = ne[i];
    }
  }
}
int main(){
  int  T;
  scanf("%d", &T);
  while(T--)
 {
    int n;
    scanf("%d %s", &n, q);
    int tot = n;
    memset(ne, 0, sizeof(ne));
    getnext(q);
    for(int i = 1; i <= n; i++){
        if(ne[i] > 0)
        {
            int an = i;
            while(ne[an] > 0){
                  tot = (tot + 1) % 10007;
                  an = ne[an];
            }
        }
    }
    printf("%d\n", tot);
 }
  return 0;
}


發佈了69 篇原創文章 · 獲贊 58 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章