(KMP 字符串处理)Substrings -- hdu -- 1238
http://acm.hdu.edu.cn/showproblem.php?pid=1238
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Description
Input
Output
Sample Input
Sample Output
之前写个是完全的暴力写的,因为数据很小,暴力也没关系,这次用了KMP,有点小题大做的感觉,不过刚刚学,就当练习了。
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std; #define MOD 10000
#define N 210 char s[N][N];
int Next[N]; void FindNext(char b[])
{
int i=, j=-, blen=strlen(b);
Next[] = -; while(i<blen)
{
if(j==- || b[i]==b[j])
Next[++i] = ++j;
else
j = Next[j];
}
} int KMP(char a[], char b[])
{
int i=, j=;
int alen=strlen(a), blen=strlen(b); FindNext(b); while(i<alen)
{
while(j==- || (a[i]==b[j] && i<alen && j<blen))
i++, j++;
if(j==blen)
return ;
j = Next[j];
}
return ;
} int main()
{
int t;
scanf("%d", &t);
while(t--)
{
int i, j, k, n, MinLen=, len;
char ss[N]; memset(s, , sizeof(s)); scanf("%d", &n); for(i=; i<n; i++)
{
scanf("%s", s[i]);
len = strlen(s[i]); if(len<MinLen)
{
MinLen = len;
memset(ss, , sizeof(ss));
strcpy(ss, s[i]);
}
} char a[N], b[N];
int index=; for(i=MinLen; i>; i--)
for(j=; j<=MinLen-i; j++)
{
memset(a, , sizeof(a));
memset(b, , sizeof(b));
strncpy(a, ss+j, i);
strcpy(b, a);
strrev(b); for(k=; k<n; k++)
{
if(KMP(s[k], a)== && KMP(s[k], b)==)
break;
} if(k==n)
{
index = i;
i=-, j=;
}
} if(index)
printf("%d\n", index);
else
printf("0\n"); }
return ;
}
(KMP 字符串处理)Substrings -- hdu -- 1238的更多相关文章
- Substrings - HDU 1238(最大共同子串)
题目大意:给你N个串,求出来他们的最大公共子串的长度(子串反过来也算他们的子串). 分析:很久以前就做过这道题,当时是用的strstr做的,不过相同的都是枚举了子串......还是很暴力,希望下次 ...
- KMP字符串模式匹配详解(转)
来自CSDN A_B_C_ABC 网友 KMP字符串模式匹配通俗点说就是一种在一个字符串中定位另一个串的高效算法.简单匹配算法的时间复杂度为O(m*n);KMP匹配算法.可以证明它的时间复杂度 ...
- BM和KMP字符串匹配算法学习
BM和KMP字符串匹配算法学习 分类: 研究与学习 字符串匹配BM(Boyer-Moore)算法学习心得 http://www.cnblogs.com/a180285/archive/2011/12/ ...
- KMP字符串模式匹配详解(zz)
刚看到位兄弟也贴了份KMP算法说明,但本人觉得说的不是很详细,当初我在看这个算法的时候也看的头晕昏昏的,我贴的这份也是网上找的.且听详细分解: KMP字符串模式匹配详解 来自CSDN A_B_ ...
- KMP字符串模式匹配详解
KMP字符串模式匹配详解 http://www.cppblog.com/oosky/archive/2006/07/06/9486.html
- hdu 1238 Substrings(kmp+暴力枚举)
Problem Description You are given a number of case-sensitive strings of alphabetic characters, find ...
- KMP(http://acm.hdu.edu.cn/showproblem.php?pid=1711)
http://acm.hdu.edu.cn/showproblem.php?pid=1711 #include<stdio.h> #include<math.h> #inclu ...
- (字符串) Hidden String -- HDU -- 5311
链接: http://acm.hdu.edu.cn/showproblem.php?pid=5311 Time Limit: 2000/1000 MS (Java/Others) Memory ...
- KMP字符串模式匹配学习笔记
KMP算法实验 1.编程计算模式串(子串)的next值.2.利用KMP算法在主串中找到模式串的位置. 参考代码:---------int getNexlVal( char * s, int j)// ...
随机推荐
- from __future__ import division
导入python未来支持的语言特征division(精确除法),当我们没有在程序中导入该特征时,"/"操作符执行的是截断除法(Truncating Division),当我们导入精 ...
- asp.net core in centos
CentOS 7部署ASP.NET Core应用程序 看了几篇大牛写的关于Linux部署ASP.NET Core程序的文章,今天来实战演练一下.2017年最后一个工作日,提前预祝大家伙元旦快乐.不 ...
- [故障引起的故事]URL中带加号的处理
问题起因: 客户订购了一关键字为"e+h 变送器" , 在首页推荐广告中,会根据用户在search 搜索过的关键字进行一个匹配投放.技术实现是UED 通过JS 获取coo ...
- uboot的配置及编译
1. 先执行配置命令 make board_name_config 再执行编译命令 make all 2. 通过在Makefile中找到 board_name_config 目标,可以查看为了得到目标 ...
- node集成mysql——pool连接池
安装 mysql npm install mysql or cnpm install mysql 创建db.js,实现mysql操作模块 var mysql = require('mysql'); v ...
- Ubuntu 安装 Zabbix 3.2详细步骤
创建 zabbix 用户 因为zabbix 程序的守护进程需要非特权用户,所以需要创建一个 zabbix 用户,即使从 root 用户启动 zabbix 程序,也会自动切换到 zabbix 用户,所以 ...
- Android中MD5加密
最近项目中遇到MD5加密,代码很简单,又是死代码,不过要注意当长度不足32的时候要补个0.下面是具体代码,直接拷贝就能用. public static String getMD5(String str ...
- poj1088(记忆化搜索入门题)
题目链接:http://poj.org/problem?id=1088 思路: 明显的记忆化搜索题,用dp[i][j]表示从(i,j)出发能滑的最远距离,用dfs搜索,若dp[x][y]>0即已 ...
- 836. Rectangle Overlap 矩形重叠
[抄题]: A rectangle is represented as a list [x1, y1, x2, y2], where (x1, y1) are the coordinates of i ...
- swift - 画图 - 画矩形,虚线,圆和半圆
import UIKit class JYJYBouncedCouponsViewCellBgView: UIView { //一定要在这里设置 背景色, 不要再draw里面设置, override ...