【HDU 4763】Theme Section(KMP)
这题数据水的一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)的更多相关文章
- HDU 4763:Theme Section(KMP)
http://acm.hdu.edu.cn/showproblem.php?pid=4763 Theme Section Problem Description It's time for mus ...
- 【hdu 5918】Sequence I(KMP)
给定两个数字序列,求a序列中每隔p个构成的p+1个序列中共能匹配多少个b序列. 例如1 1 2 2 3 3 每隔1个的序列有两个1 2 3 kmp,匹配时每次主串往前p个,枚举1到p为起点. 题目 # ...
- 【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 ...
- 【HDU 4992】 Primitive Roots (原根)
Primitive Roots Description We say that integer x, 0 < x < n, is a primitive root modulo n i ...
- 【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, ...
- 【HDU - 2102】A计划(bfs)
-->A计划 Descriptions: 可怜的公主在一次次被魔王掳走一次次被骑士们救回来之后,而今,不幸的她再一次面临生命的考验.魔王已经发出消息说将在T时刻吃掉公主,因为他听信谣言说吃公主的 ...
- 【HDU 5839】Special Tetrahedron(计算几何)
空间的200个点,求出至少四边相等,且其余两边必须不相邻的四面体的个数. 用map记录距离点i为d的点有几个,这样来优化暴力的四重循环. 别人的做法是枚举两点的中垂面上的点,再把到中点距离相等的点找出 ...
- 【HDU 4445】Crazy Tank(暴力)
高中物理斜抛运动,简单分析一下角度固定下来则可以计算每个cannonball的降落坐标lnd. 因此暴力计算不同角度下的结果. #include <cstdio> #include &qu ...
- 【HDU 4343】Interval query(倍增)
BUPT2017 wintertraining(15) #8D 题意 给你x轴上的N个线段,M次查询,每次问你[l,r]区间里最多有多少个不相交的线段.(0<N, M<=100000) 限 ...
随机推荐
- DGA特征挖掘
摘自:https://paper.seebug.org/papers/Archive/drops2/%E7%94%A8%E6%9C%BA%E5%99%A8%E5%AD%A6%E4%B9%A0%E8%A ...
- django session深入
转至原文 https://www.cnblogs.com/zhaof/p/6281468.html 基于cookie做用户验证时:敏感信息不适合放在cookie中 session依赖cookie s ...
- BZOJ 1024 SCOI2009 生日快乐 暴搜
思路:eng...按照题意搜就好了 (一定要注意题面的n<=10--) 枚举断点...反正n<=10不怂 //By SiriusRen #include <cstdio> #i ...
- tomcat动态查看服务器打印日志
[root@localhost ~]# cd /usr/local/tomcat/logs [root@localhost logs]# tail -f catalina.out FROM:htt ...
- 最小生成树(MST) prim() 算法 kruskal()算法 A - 还是畅通工程
某省调查乡村交通状况,得到的统计表中列出了任意两村庄间的距离. 省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可),并要求铺设的公 ...
- TypeError: 'dict' object is not callabled
Traceback (most recent call last): File "/root/Desktop/JuniperBackdoor-master/censys.py", ...
- 软raid 实验
RAID0 条带卷 2+ 100% 读写速度快,不容错 RAID1 镜像卷 2 50% 读写速度一般,容错 RAID5 带奇偶校验的条带卷 3+ (n-1)/n 读写速度快,容错,允许坏一块盘 RAI ...
- users---显示当前登录系统的所有用户的用户列表
users命令用于显示当前登录系统的所有用户的用户列表.每个显示的用户名对应一个登录会话.如果一个用户有不止一个登录会话,那他的用户名将显示相同的次数. 语法 users(选项) 选项 --help: ...
- Win10 +VS2015 配置openCV3.4.0
配置过程参考链接:https://www.cnblogs.com/linshuhe/p/5764394.html 其他链接:https://blog.csdn.net/weixin_39393712/ ...
- Hdu 4930 斗地主
模拟题,只是想纪念下,WA到死了…… 看到好多代码都好长,其实想说不用这么暴力. #include <iostream> #include <cstdio> #include ...