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个字符串,字符串可逆序可顺序,求它们的最长公共子串 题解 在输入的过程中记录一下最短的那个字符串,然后枚举起点,然后进行二分求出子串末位置,然后再验证是否是公共子串,记录最长的公共子串 ...
随机推荐
- html5 全屏滚动活动页学习
先看几个具体的实例: 1.腾讯娱乐:http://ent.qq.com/zt2014/qqent/h5.htm?from=groupmessage&isappinstalled=0 2.苏宁互 ...
- psp系统需求分析
软件开发方向“PSP系统”软件需求规约 目录 1 引言... 4 1.1 目的... 4 1.2 文档格式... 4 1.3 预期的读者和阅读建议... 4 1.4 范围... 5 1.5 术语... ...
- 阶段性放弃 wxPython 前的总结
为了实现一个管理本地电子书的程序,搞了一段时间 GUI,使用 wxPython. 实在难以适应和习惯,也搞不出什么太好看的效果. 最不能忍受的是,多线程处理能力太弱.遂决定放弃 GUI. 放弃之前,整 ...
- MacOS10.9获取Android源码不完全笔记(2014)
第一步:安装Macports 这个我就不叙述了,网上有无数教程 第二步:创建一个磁盘镜像 1.打开磁盘工具,然后: 第三步:使用Macport安装编译环境 1.打开终端输入以下内容 sudo port ...
- Kinetic使用注意点--circle
new Circle(config) 参数: config:包含所有配置项的对象. { radius: "半径", fill: "填充色", fillRGB: ...
- EXTJS 4.2 资料 控件之 xtype: "fieldcontainer",追加html
{ xtype: "fieldcontainer", layout: "hbox", items: [ { fieldLabel: '素材目录', name: ...
- 用上新的电脑装上了VS2013了
今天老魏终于把配置好的电脑拿回来了,16G的内存,I7 4770CPU.这回啊,老魏终于可以舍弃我的本本了,装上了SQL Server,虚拟机等等运行高内存的程序,感觉就是爽.明天老魏就可以用VS20 ...
- linux驱动系列之挂载(转)
转自网页:http://www.cnblogs.com/yeahgis/archive/2012/04/05/2432779.html http://www.linuxso.com/command/c ...
- Asp.Net MVC结合ExtJs gridPanel 分页和高度自适应
Ext.onReady(function () { gridPanel(); var panel = Ext.getCmp('gridPanel'); window.onresize = functi ...
- 简单3d RPG游戏 之 004 攻击(一)
功能:实现点击键盘F键,怪物血量条减少,并且假定是近战,需要对距离进行判断,距离小于一定值的时候按F才会减少怪物的血条. 新建c#脚本PlayerAttack,绑定到Player,并在unity里将敌 ...