hdu1238 kmp
InputThe first line of the input file contains a single integer t (1 <= t <= 10), the number of test cases, followed by the input data for each test case. The first line of each test case contains a single integer n (1 <= n <= 100), the number of given strings, followed by n lines, each representing one string of minimum length 1 and maximum length 100. There is no extra white space before and after a string.
OutputThere should be one line per test case containing the length of the largest string found.
Sample Input
- 2
- 3
- ABCD
- BCDFF
- BRCD
- 2
- rose
- orchid
Sample Output
- 2
- 2
题意:找n个字符串里的公共子串,相反方向的也算公共子串
题解:枚举第一个的子串和后面的进行kmp
(1a的感觉真tm爽)
- #include<map>
- #include<set>
- #include<cmath>
- #include<queue>
- #include<stack>
- #include<vector>
- #include<cstdio>
- #include<iomanip>
- #include<cstdlib>
- #include<cstring>
- #include<iostream>
- #include<algorithm>
- #define pi acos(-1)
- #define ll long long
- #define mod 10007
- #define ls l,m,rt<<1
- #define rs m+1,r,rt<<1|1
- using namespace std;
- const double g=10.0,eps=1e-;
- const int N=+,maxn=(<<)-,inf=0x3f3f3f3f;
- int Next[N],slen,plen;
- string a[N],ptr,str;
- void getnext()
- {
- int k=-;
- Next[]=-;
- for(int i=;i<slen;i++)
- {
- while(k>-&&str[k+]!=str[i])k=Next[k];
- if(str[k+]==str[i])k++;
- Next[i]=k;
- }
- }
- bool kmp()
- {
- int k=-;
- for(int i=;i<plen;i++)
- {
- while(k>-&&str[k+]!=ptr[i])k=Next[k];
- if(str[k+]==ptr[i])k++;
- if(k==slen-)return ;
- }
- return ;
- }
- int main()
- {
- ios::sync_with_stdio(false);
- cin.tie();
- // cout<<setiosflags(ios::fixed)<<setprecision(2);
- int t,n;
- cin>>t;
- while(t--){
- cin>>n;
- for(int i=;i<n;i++)cin>>a[i];
- int ans=;
- for(int i=;i<=a[].size();i++)
- {
- for(int j=;j<=a[].size()-i;j++)
- {
- str=a[].substr(j,i);
- slen=str.size();
- getnext();
- bool flag=;
- for(int k=;k<n;k++)
- {
- ptr=a[k];
- plen=a[k].size();
- if(kmp())continue;
- reverse(ptr.begin(),ptr.end());
- if(kmp())continue;
- flag=;
- break;
- }
- if(flag)ans=max(ans,slen);
- }
- }
- cout<<ans<<endl;
- }
- return ;
- }
hdu1238 kmp的更多相关文章
- hdu-1238(kmp+枚举)
题意:给你n个字符串,问你这里面最长的公共子串的长度是多少,一个公共子串的反串也算,比如样例二: 解题思路:随便找一个字符,枚举它的子串然后跑kmp就行了,很多人的博客都是用string类里面的函数来 ...
- hdu1238 Substrings 扩展KMP
You are given a number of case-sensitive strings of alphabetic characters, find the largest string X ...
- kuangbin专题十六 KMP&&扩展KMP HDU1238 Substrings
You are given a number of case-sensitive strings of alphabetic characters, find the largest string X ...
- KMP 、扩展KMP、Manacher算法 总结
一. KMP 1 找字符串x是否存在于y串中,或者存在了几次 HDU1711 Number Sequence HDU1686 Oulipo HDU2087 剪花布条 2.求多个字符串的最长公共子串 P ...
- KMP算法求解
// KMP.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> using namespac ...
- 简单有效的kmp算法
以前看过kmp算法,当时接触后总感觉好深奥啊,抱着数据结构的数啃了一中午,最终才大致看懂,后来提起kmp也只剩下“奥,它是做模式匹配的”这点干货.最近有空,翻出来算法导论看看,原来就是这么简单(先不说 ...
- KMP算法
KMP算法是字符串模式匹配当中最经典的算法,原来大二学数据结构的有讲,但是当时只是记住了原理,但不知道代码实现,今天终于是完成了KMP的代码实现.原理KMP的原理其实很简单,给定一个字符串和一个模式串 ...
- 萌新笔记——用KMP算法与Trie字典树实现屏蔽敏感词(UTF-8编码)
前几天写好了字典,又刚好重温了KMP算法,恰逢遇到朋友吐槽最近被和谐的词越来越多了,于是突发奇想,想要自己实现一下敏感词屏蔽. 基本敏感词的屏蔽说起来很简单,只要把字符串中的敏感词替换成"* ...
- [KMP]【学习笔记】
Oulipo Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 36916 Accepted: 14904 Descript ...
随机推荐
- addEventListener的click和onclick的区别
前两节都和addEventListener的click有关,于是在想它与onclick有什么区别呢,自己调试了一下,网上也有相关资料 事件绑定 onclick绑定方式 优点: - 简洁 - 处理事件的 ...
- 异常和TCP通讯
第七章 异常处理 * 异常处理机制中的try-catch * 语法: * try{ * 代码片段 * }catch(XXXException e){ * 当try中的代码片段出现了XXXExcepti ...
- spring MVC @Resource不支持Lazy加载及解决方法
今天迁一系统时发现有个bean使用@Resource注入了另外一个bean,这个被注入的bean是将被deprecated的类,而且只有一两个功能使用到,为了先调整进行测试,增加了@Lazy注解,启动 ...
- SCP报错:WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!
经过google,出现这个问题的原因是,这是ssh的问题, GkFool大神说(第一次使用SSH连接时,会生成一个认证,储存在客户端的known_hosts中) 我的解决办法是: ssh-keygen ...
- 20165310 Java实验五《网络编程与安全》
20165310 Java实验五<网络编程与安全> 任务一 题目:①编写MyBC.java实现中缀表达式转后缀表达式的功能:②编写MyDC.java实现从上面功能中获取的表达式中实现后缀表 ...
- 0xc0000005:读取位置时发生访问冲突
这是空指针,比如: A* a=NULL; a->fun();//会提示标题错误,因为a没有分配空间
- 《js高级程序设计》--第三章数据类型
一.关键字 二.保留字 三.数据类型 (数据类型具有动态性) 1.Undefined 声明变量却未对其加以初始化(赋值) 2.Null null值表示一个空对象指针,而这也正是使用typeof操作 ...
- FJUT 奇怪的数列(线性选择算法)题解
题意:找出无需数列中位数(偶数为两个中位数平均数向下取整) 思路:用nth_element(a + first,a + k,a+ end + 1)找出中位数,复杂度一般为O(n).这个STL能将 [ ...
- LightOJ 1296 Again Stone Game(sg函数)题解
题意:每次必须拿且只能拿不超过一半的石头,不能拿为败 思路:显然算出每个的sg函数,但是范围1e9显然不能直接打表.所以先打表找规律,发现偶数一直是自己的一半,奇数好像没规律.偶数x的sg函数值是x/ ...
- linux下安装/升级openssl
(2810) (1) 安装环境: 操作系统:CentOs7 OpenSSL Version:openssl-1.0.2j.tar.gz 安装: 目前版本最新的SSL地址为 http://www.op ...