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的更多相关文章

  1. Binary String Matching

    问题 B: Binary String Matching 时间限制: 3 Sec  内存限制: 128 MB提交: 4  解决: 2[提交][状态][讨论版] 题目描述 Given two strin ...

  2. NYOJ之Binary String Matching

    Binary String Matching 时间限制:3000 ms  |  内存限制:65535 KB 难度:3 描述     Given two strings A and B, whose a ...

  3. ACM Binary String Matching

    Binary String Matching 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 Given two strings A and B, whose alp ...

  4. 南阳OJ----Binary String Matching

    Binary String Matching 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 Given two strings A and B, whose alp ...

  5. Binary String Matching(kmp+str)

    Binary String Matching 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 Given two strings A and B, whose alp ...

  6. Aho - Corasick string matching algorithm

    Aho - Corasick string matching algorithm 俗称:多模式匹配算法,它是对 Knuth - Morris - pratt algorithm (单模式匹配算法) 形 ...

  7. [POJ] String Matching

    String Matching Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4074   Accepted: 2077 D ...

  8. String Matching Content Length

    hihocoder #1059 :String Matching Content Length 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 We define the ...

  9. NYOJ 5 Binary String Matching

    Binary String Matching 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 Given two strings A and B, whose alp ...

  10. (find) nyoj5-Binary String Matching

    5-Binary String Matching 内存限制:64MB 时间限制:3000ms 特判: No通过数:232 提交数:458 难度:3 题目描述: Given two strings A ...

随机推荐

  1. SQL语句中drop、truncate和delete的用法

    一.SQL中的语法 1.drop table  dbo.Sys_Test   2.truncate  table dbo.Sys_Test                     3.delete f ...

  2. 在SSIS包中使用 Checkpoint从失败处重新启动包[转]

    使用SSIS做ETL的过程中会遇到各种各样的错误,对于一些大数据量的Job失败以后我们不希望重新运行,因为重新运行的时间开销是非常大的,我们只希望从失败的部分开始运行,这样可以省去很多的时间. SSI ...

  3. vue element 导出 分页数据的excel表格

    1.安装相关依赖 npm install --save xlsx file-saver 2.导入相关插件 在组建头部导入相关插件 const FileSaver = require("fil ...

  4. Y7000 安装ubuntu16.04.6 的tips :禁用nouveau 、Wifi 问题 、nvidia 驱动安装

    由于最近要跑DeepLearning 所以在自己的Y7000上装一个Ubuntu ,自己碰了好多壁 写下来以防止自己忘掉以便后续再用! 配置:i7-8750H +GeForce GTX 1050Ti ...

  5. Yii2.0 limit(1)与one()

    ActiveRecord中的limit(1)与one()的区别 (new \yii\db\Query())->from('user')->limit(1)->one() public ...

  6. xilinx基础入门

    2019.09.03 一.基础部分及语法 一.FPGA程序的固化 [USF-XSim-62] 'simulate' step failed with errors. Please check the ...

  7. 使用css3的repeating-linear-gradient画虚线

    还在用 border-style: dashed 画虚线吗?虽然也是虚线,但是不能控制每一个虚线的宽度 .dashed { height: 1px; background-image: repeati ...

  8. pycharm链接数据库以及连接时候出现错误的集合

    1.pycharm如何直接连接数据库? 作用:这是一种管理数据库的方式而已,因为在开发过程中结合使用还是不错的!当然,还有有很多管理数据库的工具和方法. 比如:navicat工具 1.1  如何找到管 ...

  9. 使用impala连接hive踩坑过程

    一.打包镜像出错 docker build总是出错,如果你用的是python3.7,可以考虑使用python3.6版本 并且注意:选择thrift-sasl==0.2.1,否则会出现: Attribu ...

  10. 安装运行redis

    在Linux系统上安装Redis 环境准备 Redis是C语言开发,建议在Linux上运行,本人系统centos-6.5. 安装redis需要先将官网下载的源码进行编译,编译依赖gcc环境,如果没有g ...