Substrings - HDU 1238(最大共同子串)
#include<stdio.h>
#include<string.h> const int MAXN = ;
const int oo = 1e9+;
const int mod = ; char s[MAXN][MAXN];
int next[MAXN]; void GetNext(char s[], int N)
{
int i=, j=-;
next[] = -; while(i < N)
{
if(j==- || s[i]==s[j])
next[++i] = ++j;
else
j = next[j];
}
}
bool KMP(char a[], char b[])
{
int i=, j=;
int Na = strlen(a);
int Nb = strlen(b); GetNext(b, Nb); while(i < Na)
{
while(j==- || (a[i]==b[j] && i<Na))
i++, j++;
if(j == Nb)return true; j = next[j];
} return false;
}
bool OK(char a[], char s[])
{
if(KMP(a, s) == true)
return true;
strrev(s); return KMP(a, s);
}
int main()
{
int T; scanf("%d", &T); while(T--)
{
int i, j, k, N, len=oo, ans=;
char a[MAXN];///保存最短的那个串 scanf("%d", &N); for(i=; i<=N; i++)
{
scanf("%s", s[i]); int M = strlen(s[i]); if(len > M)
{
len = M;
strcpy(a, s[i]);
}
} for(i=; i<=len; i++)
for(j=; i+j<=len; j++)
{
char b[MAXN] = {}; strncpy(b, a+j, i); for(k=; k<=N; k++)
{
if(OK(s[k], b) == false)
break;
} if(k > N)
j=len, ans = i;
} printf("%d\n", ans);
} return ;
}
Substrings - HDU 1238(最大共同子串)的更多相关文章
- (KMP 字符串处理)Substrings -- hdu -- 1238
http://acm.hdu.edu.cn/showproblem.php?pid=1238 Substrings Time Limit:1000MS Memory Limit:32768KB ...
- HDOJ 1238 Substrings 【最长公共子串】
HDOJ 1238 Substrings [最长公共子串] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Ja ...
- hdu 1238 Substrings(kmp+暴力枚举)
Problem Description You are given a number of case-sensitive strings of alphabetic characters, find ...
- spoj 694. Distinct Substrings 后缀数组求不同子串的个数
题目链接:http://www.spoj.com/problems/DISUBSTR/ 思路: 每个子串一定是某个后缀的前缀,那么原问题等价于求所有后缀之间的不相同的前缀的个数.如果所有的后缀按照su ...
- HDU 1238 Substing
思路: 1.找出n个字符串中最短的字符串Str[N] 2.从长到短找Str[N]的子子串 subStr[N],以及subStr[N]的反转字符串strrev(subStr[N]):(从长到短是做剪枝处 ...
- HDU 1238
好吧,这题直接搜索就可以了,不过要按照长度最短的来搜,很容易想得到. 记得ACM比赛上有这道题,呃..不过,直接搜..呵呵了,真不敢想. #include <iostream> #incl ...
- hdu 1003 最大连续子串
#include <bits/stdc++.h> #define PI acos(-1.0) #define mem(a,b) memset((a),b,sizeof(a)) #defin ...
- poj 3261 后缀数组 找反复出现k次的子串(子串能够重叠)
题目:http://poj.org/problem?id=3261 仍然是后缀数组的典型应用----后缀数组+lcp+二分 做的蛮顺的,1A 可是大部分时间是在调试代码.由于模板的全局变量用混了,而自 ...
- HDU - 2328 Corporate Identity(kmp+暴力)
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2328 题意:多组输入,n==0结束.给出n个字符串,求最长公共子串,长度相等则求字典序最小. 题解:(居 ...
随机推荐
- CGContextRef一点用法
quartz 是主要的描画接口,支持基于路径的描画.抗锯齿渲染.渐变填充模式.图像.颜色.坐标空间变换.以及PDF 文档的创建.显示.和分析.UIKit 为Quartz 的图像和颜色操作提供了Ob ...
- Uploadify 笔记分享 -- 2014年10月18日
最近要做一个项目,有个部分需要用到Uploadify,以前用过,但不是很懂,找了无数遍的中文文档,发现好多都是以前的,都不能用,一时间索性自己写了个笔记,随用随查 <form> <i ...
- 图的遍历(bfs 和dfs)
BFS的思想: 从一个图的某一个顶点V0出发,首先访问和V0相邻的且未被访问过的顶点V1.V2.……Vn,然后依次访问与V1.V2……Vn相邻且未被访问的顶点.如此继续,找到所要找的顶点或者遍历完整个 ...
- php 备份数据库脚本
<?php// 备份数据库$host = "localhost";$user = "root"; //数据库账号$password = "123 ...
- ZOJ3870 Team Formation
/** Author: Oliver ProblemId: ZOJ3870 Team Formation */ /* 思路 1.异或运算,使用^会爆,想到二进制: 2.我们可以试着从前往后模拟一位一位 ...
- Skinned Mesh原理解析和一个最简单的实现示例
Skinned Mesh 原理解析和一个最简单的实现示例 作者:n5 Email: happyfirecn##yahoo.com.cn Blog: http://blog.csdn.net/n5 ...
- 一个基于nodejs,支持http/https的中间人(MITM)代理,便于渗透测试和开发调试。
源码地址:https://github.com/wuchangming/node-mitmproxy node-mitmproxy node-mitmproxy是一个基于nodejs,支持http/h ...
- 颤抖吧,Css3
相关文章地址:http://sc.chinaz.com/info/140315283609.htm http://files.cnblogs.com/xinlinux/csshake.min.css ...
- uboot的jumptable_init函数分析
一.函数说明 函数功能:安装系统函数指针 函数位置:common/exports.c 二.函数分析 void jumptable_init (void) { int i; gd->jt = (v ...
- DEDECMS-helper小助手扩展
今天在做DEDE动态调用模板的时候卡住了,后终被强大的互联网解决,记录解决问题的过程,以备后用 可以在/data/helper.inc.php中进行默认小助手初始化的设置,系统默认载入小助手 例如创建 ...