hdu 4552 字符串前缀数目统计

采用了kmp算法的思想,利用了之前比较的结果

#include<cstdio>
#include<cstring>
using namespace std;
int main()
{
    char s[100005];
    while(gets(s))
    {
        int l=strlen(s);
        int i,j,k;
        int ans=0;
        for(i=0;i<l;i++)//枚举串相差的距离 比如说比较串A和串B是否相等
        {//这里的i是A串头和B串头的距离
            for(j=0,k=i;j<l;j++,k++)//计算在这个距离有多少个字符串是相等的
            {
                if(s[j]!=s[k])break;
            }
            ans+=j;
        }
        printf("%d\n",ans%256);
    }
}


你可能感兴趣的:(hdu 4552 字符串前缀数目统计)