11:

KMP next 的强大

题意求前缀在S中出现的次数之和

next[j] 表示 S[0....NEXT[J]]==S[J-NEXT[J].....J];

于是我们得到。。后加入一个字符所得到新的前缀会多ADD[next[J]]个

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<math.h>
using namespace std;
#define mod 1000000007
typedef long long ll;
#define N 123456
char s[N];
int next[N];
int n;
ll a[N];
void kmp()
{
    n=strlen(s);
    int i=0,j=-1;
    next[0]=-1;
    while (i<n)
    {
        if (j==-1||s[i]==s[j])
        {
            i++;
            j++;
            next[i]=j;
        }else j=next[j];
    }
}
int main()
{
    while (scanf("%s",s)!=EOF)
    {
        kmp();
        ll ans=0;
        memset(a,0,sizeof(a));
        for (int i=1;i<=n;i++)
        {
            a[i]=a[next[i]];
            a[i]++;
            ans+=a[i];
        }
        printf("%lld\n",ans);
    }
    return 0;
}

HUST 1328 String的更多相关文章

  1. HUST 1328 String (字符串前缀子串个数 --- KMP)

    题意 给定一个字符串S,定义子串subS[i] = S[0..i],定义C[i]为S中subS[i]的数量,求sigma(C[i])(0<=i<N). 思路 我们以子串结尾的位置来划分阶段 ...

  2. HUST 4681 String (DP LCS变形)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4681 题目大意:给定三个字符串A,B,C 求最长的串D,要求(1)D是A的字序列 (2)D是B的子序列 ...

  3. string黑科技

    1. string对象的定义和初始化以及读写 string s1; 默认构造函数,s1为空串string s2(s1); 将s2初始化为s1的一个副本string s3("valuee&qu ...

  4. poj 1328 Radar Installation

    题目链接:http://poj.org/problem?id=1328 题意:给出海上有n个小岛的坐标,求发出的信号可以覆盖全部小岛的最少的雷达个数.雷达发射信号是以雷达为圆心,d为半径的圆,雷达都在 ...

  5. HUST 1017 - Exact cover (Dancing Links 模板题)

    1017 - Exact cover 时间限制:15秒 内存限制:128兆 自定评测 5584 次提交 2975 次通过 题目描述 There is an N*M matrix with only 0 ...

  6. Dancing Link --- 模板题 HUST 1017 - Exact cover

    1017 - Exact cover Problem's Link:   http://acm.hust.edu.cn/problem/show/1017 Mean: 给定一个由0-1组成的矩阵,是否 ...

  7. 字符串 - 近似回文词 --- csu 1328

    近似回文词 Problem's Link:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1328 analyse: 直接暴力枚举每一个终点,然后枚举 ...

  8. AC自动机---Searching the String

    ZOJ   3228 题目网址:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=16401 Description Little ...

  9. KMP---Count the string

    题目网址:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=110060#problem/A Description It is well k ...

随机推荐

  1. vue路由高级语法糖

    1.当 <router-link> 对应的路由匹配成功,将自动设置 class 属性值 .router-link-active. 模板中可以用{{$route.params.xxx取到路由 ...

  2. sql格式化工具

    桌面版: SQLInform: http://www.sqlinform.com/download_free_desktop_sw.html 在线格式化: http://www.dpriver.com ...

  3. Python100天打卡-Day10-图形用户界面和游戏开发

    基于tkinter模块的GUIPython默认的GUI开发模块是tkinter(在Python 3以前的版本中名为Tkinter)使用tkinter来开发GUI应用需要以下5个步骤: 导入tkinte ...

  4. regular expression matching DP

    这个题目,我从前天晚上(8月6号晚上)调试到现在(8月8号16:21),太心酸了,不好好总结一下,就太对不起自己了! 这是题目: Implement regular expression matchi ...

  5. 【搜索】P1041 传染病控制

    题目链接:P1041 传染病控制 题解: 这个题目是看别人的博客做出来的,其实挺不错的一个题目,考察的东西挺多的, 一个dfs可以处理5个东西: 1.找出父亲 2.找出深度 3.每一层的节点,存进Ve ...

  6. Caused by: java.io.FileNotFoundException: Could not open ServletContext resource [/config/spring/applicationContext.xml]

    在搭建SpringMVC框架的时候遇到了这个问题 问题的原因: 就是没有找到applicatoincontext.xml这个文件, 因为idea自动生成的路径不正确 因此需要再web.xml里面, ( ...

  7. Failed to load class “org.slf4j.impl.StaticLoggerBinder”

    背景: 在配置使用Hibernate的时候遇到了这个问题, 然后就很头疼. SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerB ...

  8. My Friends

    HMQ's blog RMY's blog Shq's blog wjyyy‘s blog

  9. 数独(深搜)(poj2726,poj3074)

    数独(深搜)数据最弱版本(poj 2676) Description Sudoku is a very simple task. A square table with 9 rows and 9 co ...

  10. 第一次:从今开始玩Linux,Ubuntu16.04

    首先声明,我的文章不配图,就靠文字描述,然后自己体会,摸着石头体验吧! 从今天开始玩Linux,Ubuntu16.04据说是比较稳定的,界面友好,类似与Windows界面,也有Linux的命令终端,用 ...