poj1226,poj3080
看来以后用pascal的函数要小心了;
简简单单pos其实时间复杂度是二次方级的……
今天学习的是KMP——字符匹配算法;
这两道题也都很简单,都是为这个算法练手的,
最朴素的匹配显然是穷举起始位置然后看是否匹配,复杂度O(nm)不尽人意
kmp的思想就是尽可能利用之前匹配的信息进行匹配。
具体分析我就不讲了,传送门http://www.cppblog.com/oosky/archive/2006/07/06/9486.html讲的很详细
但据说这两题暴力都可过……
var a:array[..] of string;
next:array[..] of integer;
f:array[..] of boolean;
s:string;
c:char;
i,j,l,t,p,v,n,w:integer;
procedure work_next(s:string);
var i:integer;
begin
fillchar(next,sizeof(next),);
i:=;
j:=;
next[]:=;
while i<=l do
begin
if (j=) or (s[i]=s[j]) then
begin
inc(i);
inc(j);
next[i]:=j;
end
else j:=next[j];
end;
end;
function compare(a,b:string):boolean;
var i,j,l1:integer;
begin
i:=;
j:=;
l1:=length(a);
while (i<=l1) and (j<=l) do
begin
if (j=) or (a[i]=b[j]) then
begin
inc(i);
inc(j);
end
else j:=next[j];
end;
if j>l then exit(true) else exit(false);
end; procedure kmp;
var j:integer;
begin
for j:= to n do
begin
if (j=v) or f[j] then continue;
if (compare(a[j],s)) then
begin
f[j]:=true;
w:=w+;
end;
end;
end; begin
readln(t);
for p:= to t do
begin
readln(n);
v:=;
for i:= to n do
begin
readln(a[i]);
if (v=) or (length(a[i])<length(a[v])) then v:=i;
end;
for l:=length(a[v]) downto do
begin
for i:= to length(a[v])-l+ do
begin
w:=;
fillchar(f,sizeof(f),false);
s:=copy(a[v],i,l);
work_next(s);
kmp;
for j:= to l div do
begin
c:=s[j];
s[j]:=s[l-j+];
s[l-j+]:=c;
end;
work_next(s);
kmp;
if w=n then break;
end;
if w=n then break;
end;
if w=n then writeln(l) else writeln();
end;
end.
poj1226
poj1226,poj3080的更多相关文章
- POJ3080 - Blue Jeans(KMP+二分)
题目大意 求N个字符串的最长公共字串 题解 和POJ1226做法一样...注意是字典序最小的...WA了一次 代码: #include <iostream> #include <cs ...
- POJ-3080 Blue Jeans---字符串+暴力
题目链接: https://vjudge.net/problem/POJ-3080 题目大意: 找最长的公共字串(长度>=3),长度相同就找字典序最小的 解题思路: 枚举第一个串的所以子串,处理 ...
- POJ1226 Substrings ——后缀数组 or 暴力+strstr()函数 最长公共子串
题目链接:https://vjudge.net/problem/POJ-1226 Substrings Time Limit: 1000MS Memory Limit: 10000K Total ...
- 【POJ1226】Substrings(后缀数组,二分)
题意: n<=10,len<=100 思路: 只有一个字符串的时候特判一下 #include<cstdio> #include<cstring> #include& ...
- POJ3080 Blue Jeans —— 暴力枚举 + KMP / strstr()
题目链接:https://vjudge.net/problem/POJ-3080 Blue Jeans Time Limit: 1000MS Memory Limit: 65536K Total ...
- POJ1226:Substrings(后缀数组)
Description You are given a number of case-sensitive strings of alphabetic characters, find the larg ...
- poj3080解题报告(暴力、最大公共子串)
POJ 3080,题目链接http://poj.org/problem?id=3080 题意: 就是求m个长度为60的字符串的最长连续公共子串,2<=m<=10 规定: 1.最长公共串长度 ...
- POJ3080——Blue Jeans(暴力+字符串匹配)
Blue Jeans DescriptionThe Genographic Project is a research partnership between IBM and The National ...
- POJ1226 - Substrings(KMP+二分)
题目大意 给定n个字符串,字符串可逆序可顺序,求它们的最长公共子串 题解 在输入的过程中记录一下最短的那个字符串,然后枚举起点,然后进行二分求出子串末位置,然后再验证是否是公共子串,记录最长的公共子串 ...
随机推荐
- laravel--模型中各种属性详解
首先以这个模型为例 首先看, 1.$guarded属性,$fillable属性 $guarded属性一般是和$fillable对应的,不是一起存在但是互相使用,他们都是laravel的批量赋值方法cr ...
- 有关linux日志分析的详细介绍
linux的日志文件可以帮助我们了解系统所处的状态,比如查出哪些用户有登入,及其它安全相关的一些问题. linux下的日志分析. 以下内容,部分参考了:探讨 linux 日志分析 这篇文章. 1.了解 ...
- ecshop改造读写分离配置与改造
前两天配置好了mysql主从方式,今天就拿ecshop练习读写分离.以下代码仅供学习参考,不成熟的地方,还需完善. <?php $db_name = "ecshop"; $p ...
- python中执行javascript代码
python中执行javascript代码: 1.安装相应的库,我使用的是PyV8 2.import PyV8 ctxt = PyV8.JSContext() ctxt.enter() ...
- lua5.3调用C/C++
马上面临毕业设计,打算做点跟网游有关的,先从做周边工具开始,目前正在做一个协议序列化和反序列化的东西,广告一波先: https://github.com/Anti-Magic/rproto 目前非常简 ...
- 微软职位内部推荐-Senior Engineering Lead
微软近期Open的职位: Job Title: Senior Engineering LeadDivision: Microsoft Business SolutionWork Location: S ...
- sharepoint One-Time Passwords (windows basic authentication)
//设计中,未完成 references: http://www.asp.net/web-api/overview/security/basic-authentication http://techn ...
- Java注解处理器(转)
Java中的注解(Annotation)是一个很神奇的东西,特别现在有很多Android库都是使用注解的方式来实现的.一直想详细了解一下其中的原理.很有幸阅读到一篇详细解释编写注解处理器的文章.本文的 ...
- WDC2106 iOS10新特性及开发者要注意什么
昨晚苹果在旧金山召开了WWDC,看了WWDC2016直播,我们发现变得谨慎而开放的苹果在新一版四大平台系统中展示了很多变化,当然重中之重还是伟大的iOS.通过试用iOS10beta版,除了长大了的更强 ...
- xcode 6.3 打包crash问题--参考
xcode升级6.3之后,有些项目会出现打包crash的问题,只要选择偏好设置,把source control全部禁用掉就可以了.