这题数据水的一B。直接暴力都能够过。

比赛的时候暴力过的。回头依照正法做了一发。

匹配的时候 失配函数 事实上就是前缀 后缀的匹配长度,之后就是乱搞了。

KMP的题可能不会非常直接的出,可是KMP的思想常常渗透在非常多题目里面,近期须要多练习一下。

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 1000005;
int _next[maxn],L;
char s[maxn];
void kmp_init(){
int i,j;
int m = strlen(s);
j = _next[0] = -1;
i = 0;
while(i < m){
while(j != -1 && s[i] != s[j]) j = _next[j];
_next[++i] = ++j;
}
}
bool kmp_solve(int len){
int i,j,m = L - len;
i = len;
j = 0;
while(i < m){
while(j != -1 && s[j] != s[i]) j = _next[j];
i++; j++;
if(j >= len){
return true;
}
}
return false;
}
int main(){
int T;
scanf("%d",&T);
while(T--){
scanf("%s",s);
L = strlen(s);
kmp_init();
int i = L - 1;
int ans = 0;
while(_next[i] >= 0){
if(kmp_solve(_next[i] + 1)){
ans = _next[i] + 1;
break;
}
i = _next[i];
}
printf("%d\n",ans);
}
return 0;
}

【HDU 4763】Theme Section(KMP)的更多相关文章

  1. HDU 4763:Theme Section(KMP)

    http://acm.hdu.edu.cn/showproblem.php?pid=4763 Theme Section Problem Description   It's time for mus ...

  2. 【hdu 5918】Sequence I(KMP)

    给定两个数字序列,求a序列中每隔p个构成的p+1个序列中共能匹配多少个b序列. 例如1 1 2 2 3 3 每隔1个的序列有两个1 2 3 kmp,匹配时每次主串往前p个,枚举1到p为起点. 题目 # ...

  3. 【HDU 6153】A Secret (KMP)

    Problem Description Today is the birthday of SF,so VS gives two strings S1,S2 to SF as a present,whi ...

  4. 【HDU 4992】 Primitive Roots (原根)

    Primitive Roots   Description We say that integer x, 0 < x < n, is a primitive root modulo n i ...

  5. 【HDU 1533】 Going Home (KM)

    Going Home Problem Description On a grid map there are n little men and n houses. In each unit time, ...

  6. 【HDU - 2102】A计划(bfs)

    -->A计划 Descriptions: 可怜的公主在一次次被魔王掳走一次次被骑士们救回来之后,而今,不幸的她再一次面临生命的考验.魔王已经发出消息说将在T时刻吃掉公主,因为他听信谣言说吃公主的 ...

  7. 【HDU 5839】Special Tetrahedron(计算几何)

    空间的200个点,求出至少四边相等,且其余两边必须不相邻的四面体的个数. 用map记录距离点i为d的点有几个,这样来优化暴力的四重循环. 别人的做法是枚举两点的中垂面上的点,再把到中点距离相等的点找出 ...

  8. 【HDU 4445】Crazy Tank(暴力)

    高中物理斜抛运动,简单分析一下角度固定下来则可以计算每个cannonball的降落坐标lnd. 因此暴力计算不同角度下的结果. #include <cstdio> #include &qu ...

  9. 【HDU 4343】Interval query(倍增)

    BUPT2017 wintertraining(15) #8D 题意 给你x轴上的N个线段,M次查询,每次问你[l,r]区间里最多有多少个不相交的线段.(0<N, M<=100000) 限 ...

随机推荐

  1. 46. AngularJS所有版本下载

    转自:https://www.cnblogs.com/best/tag/Angular/ 官网下载:https://angularjs.org/ AngularJS所有版本下载:https://cod ...

  2. 34.angularJS的{{}}和ng-bind

    转自:https://www.cnblogs.com/best/tag/Angular/ 1. <html> <head> <meta charset="utf ...

  3. c# 静态成员和实例成员的区别

    静态成员也称为共享成员,例如静态属性 静态字段 静态方法:静态成员可以在类的实例之间共享. 静态类中只能有静态成员,不能有实例成员,因为静态类不能进行实例化: 在非静态类中 即可以有静态成员 也可以有 ...

  4. BZOJ5017 炸弹(线段树优化建图+Tarjan+拓扑)

    Description 在一条直线上有 N 个炸弹,每个炸弹的坐标是 Xi,爆炸半径是 Ri,当一个炸弹爆炸时,如果另一个炸弹所在位置 Xj 满足:  Xi−Ri≤Xj≤Xi+Ri,那么,该炸弹也会被 ...

  5. python -迭代器与生成器 以及 iterable(可迭代对象)、yield语句

    我刚开始学习编程没多久,对于很多知识还完全不知道,而有些知道的也是一知半解,我想把学习到的知识记录下来,一是弥补记忆力差的毛病,二也是为了待以后知识能进一步理解透彻时再回来做一个补充. 参考链接: 完 ...

  6. LocalDateTime在spring boot中的格式化配置

    在项目中日期格式化是最常见的问题,之前涉及的 java.util.Date 和 java.util.Calendar 类易用性差,不支持时区,非线程安全,对日期的计算方式繁琐,而且容易出错,因为月份是 ...

  7. PatentTips - Virtualizing performance counters

    BACKGROUND Generally, the concept of virtualization in information processing systems allows multipl ...

  8. POJ——T 1182 食物链

    http://poj.org/problem?id=1182 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 77012   ...

  9. poj 2533 Longest Ordered Subsequence 最长递增子序列(LIS)

    两种算法 1.  O(n^2) #include<iostream> #include<cstdio> #include<cstring> using namesp ...

  10. leetcode笔记:Range Sum Query - Mutable

    一. 题目描写叙述 Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), ...