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个字符串,求最长公共子串,长度相等则求字典序最小. 题解:(居 ...
随机推荐
- TSQL Beginners Challenge 1 - Find the second highest salary for each department
很久以前准备写的系列文章,后来因为懒一直耽搁着,今天突然决定继续下去,于是有了这篇文章,很基础,但很常用.题目描述依然拷贝.简单来说就是找出个个部门薪水排名第二的人,排名相同的要一起列出来. Intr ...
- ubuntu桌面变空白,或者只有壁纸,任务栏消失的解决办法
原因:因为打开了桌面特效的原因,但设置不合导致的. 解决方法:方法一:1.按住Ctrl+Alt+F1切换到字符终端下,输入用户名和密码登录2.输入以下命令删除出错的Compiz配置文件相关目录:rm ...
- 转-SecureCRT设置
原帖地址:http://www.2cto.com/os/201410/341569.html 一.基本设置 1.修改设置 为了SecureCRT用起来更方便,需要做一些设置,需要修改的有如下几处: 1 ...
- WampServer修改端口及菜单Localhost
一.修改Apache端口 1.在界面中选Apache,弹出隐藏菜单选项,打开配置文件httpd.conf: 2.找到 Listen 80: 3.将 80 改成 8080(当然自己也可以设定别的不使用的 ...
- C++ STL的基本基本原理
STL都是在内存的堆区分配的,但是其析构也是STL帮我们做好的,不用手动去delete. 1.vector 逻辑地址连续的一片内存空间,当空间不足,重新申请新的地址空间,将原有的数据复制过去,而新的地 ...
- 【POJ3580】【splay版】SuperMemo
Description Your friend, Jackson is invited to a TV show called SuperMemo in which the participant i ...
- 【BZOJ2741】【块状链表+可持久化trie】FOTILE模拟赛L
Description FOTILE得到了一个长为N的序列A,为了拯救地球,他希望知道某些区间内的最大的连续XOR和. 即对于一个询问,你需要求出max(Ai xor Ai+1 xor Ai+2 .. ...
- 永久关闭防火墙和selinux
临时关闭selinux: setenforce 0 //设置SELinux 成为permissive模式 彻底禁用selinux: 使用root用户,vim /etc/sysconfig/sel ...
- 神秘链接__proto__是什么鬼
_proto_实际上是某个实例对象的隐藏属性,而prototype是其构造器函数(或者说‘类’)的原型属性; function Mine() {} var hi = new Function(), ...
- uboot总结:uboot配置和启动过程3(config.mk分析)
说明:文件位置:在uboot的目录下,文件名为:config.mk.是一个makefile文件,以后会被主Makefile调用. 它的主要作用的是: (1)具体的设置交叉编译工具链接(主Makefil ...