HDU 4552 怪盗基德的挑战书 (KMP + DP) 或 后缀数组

题目大意:

和HDU 3336简直一模一样啊.....


大致思路:

就是KMP + DP 或者后缀数组都可以做...细节见这个吧: HDU 3336题解

一模一样不吐槽了...


代码如下:

Result  :  Accepted     Memory  :  2448 KB     Time  :  15 ms

/*
 * Author: Gatevin
 * Created Time:  2015/5/5 10:25:30
 * File Name: Rin_Tohsaka.cpp
 */
#include<iostream>
#include<sstream>
#include<fstream>
#include<vector>
#include<list>
#include<deque>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cctype>
#include<cmath>
#include<ctime>
#include<iomanip>
using namespace std;
const double eps(1e-8);
typedef long long lint;
#define foreach(e, x) for(__typeof(x.begin()) e = x.begin(); e != x.end(); ++e)
#define SHOW_MEMORY(x) cout<<sizeof(x)/(1024*1024.)<<"MB"<<endl

#define maxn 100010
#define next nextnextnext
char s[maxn];
int n;
int next[maxn];
int dp[maxn];

void KMP(char *s, int n)
{
    memset(next, 0, sizeof(next));
    for(int i = 1; i < n; i++)
    {
        int j = i;
        while(j > 0)
        {
            j = next[j];
            if(s[i] == s[j])
            {
                next[i + 1] = j + 1;
                break;
            }
        }
    }
};

const int mod = 256;

int main()
{
    while(scanf("%s", s) != EOF)
    {
        int len = strlen(s);
        KMP(s, len);
        dp[0] = 0;
        int ans = 0;
        for(int i = 1; i <= len; i++)
        {
            dp[i] = dp[next[i]] + 1;
            if(dp[i] >= mod) dp[i] -= mod;
            ans += dp[i];
            if(ans >= mod) ans -= mod;
        }
        printf("%d\n", ans);
    }
    return 0;
}

后缀数组的做法看HDU 3336的题解吧...一样的题就不写了..

你可能感兴趣的:(HDU 4552 怪盗基德的挑战书 (KMP + DP) 或 后缀数组)