string matching
string matching
exkmp
#include<bits/stdc++.h>
using namespace std;
const int maxn=;
int Nex[maxn],extend[maxn];
void getNex(char str[])
{
int i=,j,po,len=strlen(str);
Nex[]=len;
while(str[i]==str[i+]&&i+<len)
{
i++;
}
Nex[]=i;
po=;
for(i=; i<len; i++)
{
if(Nex[i-po]+i<Nex[po]+po)
{
Nex[i]=Nex[i-po];
}
else
{
j=Nex[po]+po-i;
if(j<)j=;
while(i+j<len&&str[j]==str[j+i])
{
j++;
}
Nex[i]=j;
po=i;
}
}
}
void Extend(char s1[],char s2[])
{
int i=,j,po,len=strlen(s1),l2=strlen(s2);
getNex(s2);
while(s1[i]==s2[i]&&i<l2&&i<len)
{
i++;
}
extend[]=i;
po=;
for(i=; i<len; i++)
{
if(Nex[i-po]+i<extend[po]+po)
{
extend[i]=Nex[i-po];
}
else
{
j=extend[po]+po-i;
if(j<)j=;
while(i+j<len&&j<l2&&s1[j+i]==s2[j])j++;
extend[i]=j;
po=i;
}
}
}
char s[];
char t[];
int main()
{
freopen("1.in","r",stdin);
int T;
scanf("%d",&T);
while(T--)
{
memset(extend,,sizeof extend);
memset(Nex,,sizeof Nex);
scanf("%s",s);
strcpy(t,s);
Extend(s,t);
int n=strlen(s);
long long ans=;
//cout<<s<<t<<endl;
for(int i=; i<=n-; i++)
{
if(i+extend[i]==n)
ans+=extend[i];
else
ans+=extend[i]+;
//cout<<extend[i]<<" "<<i<<" "<<n-1<<'\n';
}
if(n>)
ans+=;
cout<<ans<<'\n';
}
}
string matching的更多相关文章
- Binary String Matching
		问题 B: Binary String Matching 时间限制: 3 Sec 内存限制: 128 MB提交: 4 解决: 2[提交][状态][讨论版] 题目描述 Given two strin ... 
- NYOJ之Binary String Matching
		Binary String Matching 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 Given two strings A and B, whose a ... 
- ACM Binary String Matching
		Binary String Matching 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 Given two strings A and B, whose alp ... 
- 南阳OJ----Binary String Matching
		Binary String Matching 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 Given two strings A and B, whose alp ... 
- Binary String Matching(kmp+str)
		Binary String Matching 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 Given two strings A and B, whose alp ... 
- Aho - Corasick string matching algorithm
		Aho - Corasick string matching algorithm 俗称:多模式匹配算法,它是对 Knuth - Morris - pratt algorithm (单模式匹配算法) 形 ... 
- [POJ] String Matching
		String Matching Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 4074 Accepted: 2077 D ... 
- String Matching Content Length
		hihocoder #1059 :String Matching Content Length 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 We define the ... 
- NYOJ 5 Binary String Matching
		Binary String Matching 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 Given two strings A and B, whose alp ... 
- (find) nyoj5-Binary String Matching
		5-Binary String Matching 内存限制:64MB 时间限制:3000ms 特判: No通过数:232 提交数:458 难度:3 题目描述: Given two strings A ... 
随机推荐
- Mongodb-安全配置优化
			1.MongoDB配置文件样例 # mongod.conf, Percona Server for MongoDB # for documentation of all options, see: # ... 
- 牛客练习赛46  E  华华和奕奕学物理 (树状数组)
			https://ac.nowcoder.com/acm/contest/894/E 一开始写了一个简单的模拟 通过率只有5%...... 看题解真的理解了好久!!肥宅大哭orz 题解如下 最后一句:“ ... 
- 循环结构 :for
			循环结构 :for 循环四要素: 1.初始化条件 2.循环条件 3.循环体 4.迭代条件 格式: for(初始化条件;循环条件;迭代条件){ 循环体; } 执行顺序 :1 -> 2 -> ... 
- 【论文解读】[目标检测]retinanet
			作为单阶段网络,retinanet兼具速度和精度(精度是没问题,速度我持疑问),是非常耐用的一个检测器,现在很多单阶段检测器也是以retinanet为baseline,进行各种改进,足见retinan ... 
- linux 打包和压缩的概念和区别
			对于刚刚接触Linux的人来说,一定会给Linux下一大堆各式各样的文件名 给搞晕.别个不说,单单就压缩文件为例,我们知道在Windows下最常见的压缩文件就只有两种,一是,zip,另一个是.rar. ... 
- 对数据集做标准化处理的几种方法——基于R语言
			数据集——iris(R语言自带鸢尾花包) 一.scale函数 scale函数默认的是对制定数据做均值为0,标准差为1的标准化.它的两个参数center和scale: 1)center和scale默认为 ... 
- CentOS7搭建FastDFS V5.11分布式文件系统及Java整合详细过程
			1.1 FastDFS的应用场景 FastDFS是为互联网应用量身定做的一套分布式文件存储系统,非常适合用来存储用户图片.视频.文档等文件.对于互联网应用,和其他分布式文件系统相比,优势非常明显.其中 ... 
- 负载均衡的3种模型(httpd+lvs几十万并发的负载均衡搭建)
			一.几种常见的负载均衡模型 二.搭建httpd+lvs LVS .node01 启动一块eth0:2的网卡子接口(ifconfig eth0: down可以把网卡down掉) ifconfig eth ... 
- Java Annotation 刷课笔记(二)
			1.反射机制性能问题(安全检查) 1.1setAccessible 启用和禁用访问安全检查的开关,值为true,则指示反射的对象在使用时应该取消Java语言访问检查,值为false,则指示反射的对象应 ... 
- 牛客练习赛49 E	筱玛爱游戏 (线性基+博弈)
			链接:https://ac.nowcoder.com/acm/contest/946/E 来源:牛客网 筱玛爱游戏 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 524288K,其他 ... 
