kuangbin专题十六 KMP&&扩展KMP HDU1238 Substrings
(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
- 这道题和之前一道题类似,都是多个字符串匹配问题,只是这道题多了倒序的匹配。直接reverse就可以了
遍历第一个串的所有后缀,然后匹配每个字符串的正反序。得到公共匹配的最小。然后枚举所有后缀取最大
- #include<stdio.h>
- #include<iostream>
- #include<string>
- #include<algorithm>
- #include<vector>
- using namespace std;
- int _,n,Next[];
- vector<string> t;
- void prekmp(string s) {
- int len=s.size();
- int i,j;
- j=Next[]=-;
- i=;
- while(i<len) {
- while(j!=-&&s[i]!=s[j]) j=Next[j];
- Next[++i]=++j;
- }
- }
- int kmp(string t,string p) {
- int lent=t.size(),lenp=p.size();
- int i=,j=,ans=-,res;
- res=-;
- while(i<lent) {
- while(j!=-&&t[i]!=p[j]) j=Next[j];
- ++i;++j;
- res=max(res,j);
- }
- ans=max(ans,res);
- reverse(t.begin(),t.end());
- res=-;
- i=,j=;
- while(i<lent) {
- while(j!=-&&t[i]!=p[j]) j=Next[j];
- ++i;++j;
- res=max(res,j);
- }
- ans=max(ans,res);
- return ans;
- }
- int main() {
- // freopen("in","r",stdin);
- for(scanf("%d",&_);_;_--) {
- scanf("%d",&n);
- string s;
- for(int i=;i<n;i++) {
- cin>>s;
- t.push_back(s);
- }
- string str=t[],tempstr;
- int len=str.size(),maxx=-;
- for(int i=;i<len;i++) {
- tempstr=str.substr(i,len-i);
- int ans=0x3f3f3f;
- prekmp(tempstr);
- for(int j=;j<t.size();j++) {
- ans=min(kmp(t[j],tempstr),ans);
- }
- maxx=max(maxx,ans);
- }
- printf("%d\n",maxx);
- t.clear();
- }
- }
kuangbin专题十六 KMP&&扩展KMP HDU1238 Substrings的更多相关文章
- kuangbin专题十六 KMP&&扩展KMP HDU2609 How many (最小字符串表示法)
Give you n ( n < 10000) necklaces ,the length of necklace will not large than 100,tell me How man ...
- kuangbin专题十六 KMP&&扩展KMP HDU2328 Corporate Identity
Beside other services, ACM helps companies to clearly state their “corporate identity”, which includ ...
- kuangbin专题十六 KMP&&扩展KMP HDU3336 Count the string
It is well known that AekdyCoin is good at string problems as well as number theory problems. When g ...
- kuangbin专题十六 KMP&&扩展KMP POJ3080 Blue Jeans
The Genographic Project is a research partnership between IBM and The National Geographic Society th ...
- kuangbin专题十六 KMP&&扩展KMP HDU3746 Cyclic Nacklace
CC always becomes very depressed at the end of this month, he has checked his credit card yesterday, ...
- kuangbin专题十六 KMP&&扩展KMP HDU2087 剪花布条
一块花布条,里面有些图案,另有一块直接可用的小饰条,里面也有一些图案.对于给定的花布条和小饰条,计算一下能从花布条中尽可能剪出几块小饰条来呢? Input输入中含有一些数据,分别是成对出现的花布条和小 ...
- kuangbin专题十六 KMP&&扩展KMP HDU1686 Oulipo
The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter 'e ...
- kuangbin专题十六 KMP&&扩展KMP HDU1711 Number Sequence
Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1], b[2], ...... , b[M] (1 <= M ...
- kuangbin专题十六 KMP&&扩展KMP HDU3613 Best Reward(前缀和+manacher or ekmp)
After an uphill battle, General Li won a great victory. Now the head of state decide to reward him w ...
随机推荐
- PDM/CDM中进行搜索
Option Explicit ValidationMode = True InteractiveMode = im_Batch Dim mdl '当前model '获取当前活 ...
- leetcode429
这道题目是属于树的层次遍历,使用两层的队列非空判断. class Solution { public: vector<vector<int>> levelOrder(Node* ...
- eclipse 报错:GC overhead limit exceeded
还是eclipse内存问题 修改eclipse.ini -Xms512m -Xmx1024m 必要的情况下, 添加 -XX:MaxPermSize=1024M 表示在编译文件时一直占有最大内存
- 如何让DIALOG点击确定按钮之后由于数据不合法不关闭
public void SetDialogIsClose(DialogInterface pDialog, Boolean pisClose) { try { Field _Field = pDial ...
- springBoot数据库jpa+对接mybatis
1 spring Data jpa hibernate引领数据访问技术,使用orm对象关系映射来进行数据库访问,通过模型和数据库进行映射,通过操作对象实现对数据库操作,把数据库相关操作从代码中独立出 ...
- day35-hibernate映射 03-Hibernate持久态对象自动更新数据库
持久态对象一个非常重要的能力:自动更新数据库. package cn.itcast.hibernate3.demo1; import static org.junit.Assert.*; import ...
- Angular22 HttpClient的使用
1 HttpClient介绍 HttpClient时Http的演进,注意:Http在@angular/http中,而HttpClient在@angular/common/http中: 使用前需要在模块 ...
- Opengl创建几何实体——四棱锥和立方体
//#include <gl\glut.h>#include <GL\glut.h>#include <iostream> using namespace std; ...
- ROS Learning-022 learning_tf-06(编程) 现在与过去中穿梭 (Python版) --- waitForTransformFull() 函数
ROS Indigo learning_tf-06 现在与过去中穿梭 (Python版) - waitForTransformFull() 函数 我使用的虚拟机软件:VMware Workstatio ...
- Python Matplotlib.plot Update image Questions
1. 最近在测试一款设备,采集了一些设备后需要一帧一帧显示图像,经常使用Python,所以选用了Matplotlib进行图像操作 数据结构: timesatamp polar_distance hor ...