HDU_3336 Count the string(KMP)

题目请点我

题解:

题意是在确定了一个前串后,问能找到多少个与前串对应的后串。注意后串是可以重叠的,比如aaaaaa,若固定前串为aa,则后传为aaaa,共有三个与之匹配,所以共有四个aa字串。最开始的思路是枚举字串的长度,但是是O(N^2)的时间复杂度,超时了很多次。后来借鉴同学的方法,换了一种思路,每次利用while循环找到以A[i]结尾的长度不大于i/2的匹配子串。不考虑子串的长度,而是考虑共有多少个符合条件的子串。可以证明所有的子串都是唯一的,很好的方法。

代码实现:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#define MAX 200000
using namespace std;

int T;
int N;
int res;
int len;
char A[MAX];
char B[MAX];
int Next[MAX];
void solve();
void getnext(int x);
const int mod = 10007;
int main()
{
    scanf("%d",&T);
    while( T-- ){
        scanf("%d",&N);
        getchar();
        gets(A);
        res = 0;
        len = strlen(A);
        getnext(len);
        solve();
        printf("%d\n",res);
    }
    return 0;
}

void solve(){
    int j = 0;
    for( int i = 1; i <= len; i++ ){
        j = i;
        //while 循环向前查找
        while( Next[j] > 0 ){
            if( Next[j]*2 < i+1 ){
                res++;
                res%=mod;
            }
            j = Next[j];
        }
    }
    res = (res%mod+len%mod)%mod;
    return ;
}

void getnext(int x){
    memset(Next,0,sizeof(Next));
    Next[0] = -1;
    int i = 0, j = -1;
    while( i < x ){
        if( j == -1 || A[i] == A[j] ){
            Next[++i] = ++j;
        }
        else{
            j = Next[j];
        }
    }
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-06 03:26:34

HDU_3336 Count the string(KMP)的相关文章

hdu 3336 Count the string(KMP)

一道应用kmp算法中next数组的题目 这其中vis[i]从1加到n vis[i]=[next[i]]+1; #include<string.h> #include<stdlib.h> #include<stdio.h> #include<iostream> #include<algorithm> using namespace std; char s[200005]; int b; int next[200005]; int vis[20000

HDU3336-Count the string(KMP)

Count the string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4449    Accepted Submission(s): 2094 Problem Description It is well known that AekdyCoin is good at string problems as well as n

hdu 3336 count the string(KMP+dp)

题意: 求给定字符串,包含的其前缀的数量. 分析: 就是求所有前缀在字符串出现的次数的和,可以用KMP的性质,以j结尾的串包含的串的数量,就是next[j]结尾串包含前缀的数量再加上自身是前缀,dp[i]表示以i为结尾包含前缀的数量,则dp[i]=dp[next[i]]+1,最后求和即可. #include <map> #include <set> #include <list> #include <cmath> #include <queue>

HDU 3336 Count the string(KMP算法next数组的应用)

解题思路: 求前缀数组出现的次数之和,next[i] > 0 表示长度为next[i]的前缀又出现了一次. #include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> #include <algorithm> #include <cmath> #include <vector> #include <queue> #

HDU 3336 Count the string (基础KMP)

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&qu

HDOJ3336 Count the string 【KMP前缀数组】+【动态规划】

Count the string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4212    Accepted Submission(s): 1962 Problem Description It is well known that AekdyCoin is good at string problems as well as n

HDU 2594 Simpsons’ Hidden Talents (KMP)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2594 这题直接用KMP算法就可以做出来,不过我还尝试了用扩展的kmp,这题用扩展的KMP效率没那么高. KMP算法: #include<stdio.h> #include<iostream> #include<string.h> using namespace std; int next[50001]; char p[50000],s[50000]; void getnex

mybaits错误解决:There is no getter for property named &#39;id&#39; in class &#39;java.lang.String&#39;(转)

在使用mybaitis传参数的时候,如果仅传入一个类型为String的参数,那么在 xml文件中应该使用_parameter来代替参数名. 正确的写法: [html] view plain <span style="font-size:18px;">    <!-- 用于查询运单号是否存在 --> <select id="isCargoBillNoExist" resultType="java.lang.Integer&quo

poj1961 &amp; hdu 1358 Period(KMP)

poj 题目链接:http://poj.org/problem?id=1961 hdu题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1358 Description For each prefix of a given string S with N characters (each character has an ASCII code between 97 and 126, inclusive), we want to know whether