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 ...
随机推荐
- itchat监听微信撤回消息
import itchat from itchat.content import * import re msg_infomation = {} # 监听发送消息 @itchat.msg_regist ...
- xmake新增对WDK驱动编译环境支持
xmake v2.2.1新版本现已支持WDK驱动编译环境,我们可以直接在系统原生cmd终端下,执行xmake进行驱动编译,甚至配合vscode, sublime text, IDEA等编辑器+xmak ...
- js for 循环
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- Docker开启ssh服务
一.准备 apt-get update 更新环境 apt-get install vim 安装vim vim /etc/apt/source.list 更换软件源, 我 ...
- 杭州集训Day4
别问我为什么没有前三天,有时间再补~ 60+60+50=170. T1 . 坐等 memset0 ( 1s 256MB )( 原题:洛谷CF1151E Number of Components ) 树 ...
- C/C++ 内存模型
C分为四个区:堆,栈,静态全局变量区,常量区 C++内存分为5个区域(堆栈全常代 ): 堆 heap : 由new分配的内存块,其释放编译器不去管,由我们程序自己控制(一个new对应一个delete) ...
- merge into 导致序列跳号
For each row merged by a MERGE statement. The reference to NEXTVAL can appear in the merge_insert_cl ...
- python3—廖雪峰之练习(二)
函数的参数练习 请定义一个函数quadratic(a, b, c), 接收3个参数,返回一元二次方程 : $ ax^2+b+c=0 $ 的两个解 提示:计算平方根可以调用math.sqrt()函数: ...
- MyBatis插入并返回id技巧
1, 使用返回插入id的值,这个值即是当前插入的id
- Android remote gdb
On Android phone adb push ~/utils/android-ndk-r12b/prebuilt/android-arm64/gdbserver/gdbserver /data/ ...