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 ...
随机推荐
- kafka学习(五)
kafka可靠的数据传递 kafka可靠性保证 ACID 是关系型数据库保证数据的规范,指的是原子性,一致性,隔离性和持久性,这是数据库给出的可靠性保证. kafka给出的保证是什么? 1.k ...
- 利用coverage工具进行Python代码覆盖率测试
Coverage是一种用于统计Python代码覆盖率的工具,通过它可以检测测试代码对被测代码的覆盖率情况. Coverage安装 1.安装命令:pip install coverage 2.查看cov ...
- FastDFS分布式文件服务器
5.分布式文件服务器FastDFS(阿里巴巴) 5.1什么是FastDFS FastDFS 是用 c 语言编写的一款开源的分布式文件系统.FastDFS 为互联网量身定制,充分考虑了冗余备份.负载均衡 ...
- 《剑指offer》面试题23 从上往下打印二叉树 Java版
注意层序遍历的时候对每一层的处理方式可能不同,这里把每一层的元素保存进一个List中了,那么就需要记录每一层的数量. public List<List<Integer>> se ...
- uboot初识
一. 什么是uboot 1.1. uboot的由来 1.1.1. uboot是SourceForge上的开源项目 1.1.2. uboot就是由一个人发起,然后由整个网络上所有感兴趣的人共同维护发展而 ...
- [Codeforces 1214D]Treasure Island(dfs)
[Codeforces 1214D]Treasure Island(dfs) 题面 给出一个n*m的字符矩阵,'.'表示能通过,'#'表示不能通过.每步可以往下或往右走.问至少把多少个'.'变成'#' ...
- tensorflow学习笔记六----------神经网络
使用mnist数据集进行神经网络的构建 import numpy as np import tensorflow as tf import matplotlib.pyplot as plt from ...
- P2586 [ZJOI2008]杀蚂蚁
传送门 快乐模拟,修身养性 代码长度其实还好,主要是细节多 只要知道一些计算几何基础知识即可快乐模拟,按着题目要求一步步实现就行啦 注意仔细读题,蚂蚁每 $5$ 秒乱走一次的时候是只要能走就走了,不一 ...
- JavaScript生成简单数字验证码
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8& ...
- oracle给用户赋dblink权限
create database link 别名(可任意起) connect to 需要连接库的用户名identified by 需要连接库的用户名 using '(DESCRIPTION =(ADDR ...