HDU - 2328 Corporate Identity(kmp+暴力)
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2328
题意:多组输入,n==0结束。给出n个字符串,求最长公共子串,长度相等则求字典序最小。
题解:(居然没t,可能数据水了吧)这个题和 HDU - 1238 基本一样,用string比较好操作。选第一个字符串然后两层循环(相当于找到所有的子串),然后和其他几个字符串比较看是否出现过,如果所有字符串中都出现了就记录下来,找出长度最大字典序最小的子串。否则输出“IDENTITY LOST”。
1 #include<bits/stdc++.h>
2 using namespace std;
3
4 string a[4100];
5 int nt[210];
6
7 void getNext(string p){ //比上边的nt数组往后一位
8 memset(nt,0,sizeof(nt));
9 nt[0]=-1;
10 int i=0,j=-1; //j控制前缀,i控制后缀
11 int lp=p.length();
12 while(i<lp){
13 if(j==-1||p[i]==p[j]){
14 ++i;++j;
15 nt[i]=j;
16 }
17 else j=nt[j];
18 }
19 }
20
21 bool kmp(string t,string p){
22 getNext(p);
23 int i=0,j=0;
24 int lt=t.length(),lp=p.length();
25 int ans=0;
26 while(i<lt){
27 if(j==-1||t[i]==p[j]){
28 i++;
29 j++;
30 }
31 else j=nt[j];
32 if(j==lp) return 1;
33 }
34 return 0;
35 }
36
37 int main()
38 {
39 ios::sync_with_stdio(false);
40 cin.tie(0);
41 cout.tie(0);
42 int n;
43 while(cin>>n&&n){
44 for(int i=0;i<n;i++) cin>>a[i];
45 int ans=0;
46 string str="IDENTITY LOST";
47 for(int i=0;i<a[0].length();i++){
48 string p;
49 for(int j=i;j<a[0].length();j++){
50 p+=a[0][j];
51 int flag=1;
52 for(int k=1;k<n;k++){
53 if(!kmp(a[k],p)){
54 flag=0;
55 break;
56 }
57 }
58 if(flag) {
59 if(ans<j-i+1) ans=j-i+1,str=p;
60 else if(ans==j-i+1) str=min(str,p);
61 }
62 else break;
63 }
64 }
65 cout<<str<<endl;
66 }
67 return 0;
68 }
HDU - 2328 Corporate Identity(kmp+暴力)的更多相关文章
- hdu 2328 Corporate Identity(kmp)
Problem Description Beside other services, ACM helps companies to clearly state their “corporate ide ...
- POJ-3450 Corporate Identity (KMP+后缀数组)
Description Beside other services, ACM helps companies to clearly state their “corporate identity”, ...
- HDU 2328 POJ 3450 KMP
题目链接: HDU http://acm.hdu.edu.cn/showproblem.php?pid=2328 POJhttp://poj.org/problem?id=3450 #include ...
- POJ 3450 Corporate Identity KMP解决问题的方法
这个问题,需要一组字符串求最长公共子,其实灵活运用KMP高速寻求最长前缀. 请注意,意大利愿父亲:按照输出词典的顺序的规定. 另外要提醒的是:它也被用来KMP为了解决这个问题,但是很多人认为KMP使用 ...
- POJ 3450 Corporate Identity kmp+最长公共子串
枚举长度最短的字符串的所有子串,再与其他串匹配. #include<cstdio> #include<cstring> #include<algorithm> #i ...
- 剪花布条 - HDU 2087(简单KMP | 暴力)
分析:基础的练习............... ============================================================================ ...
- (KMP 暴力)Corporate Identity -- hdu -- 2328
http://acm.hdu.edu.cn/showproblem.php?pid=2328 Corporate Identity Time Limit: 9000/3000 MS (Java/Oth ...
- hdu2328 Corporate Identity【string库使用】【暴力】【KMP】
Corporate Identity Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- POJ 题目3450 Corporate Identity(KMP 暴力)
Corporate Identity Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 5493 Accepted: 201 ...
随机推荐
- LeetCode700 二叉搜索树中搜索
给定二叉搜索树(BST)的根节点和一个值. 你需要在BST中找到节点值等于给定值的节点. 返回以该节点为根的子树. 如果节点不存在,则返回 NULL. 例如, 给定二叉搜索树: 4 / \ 2 7 / ...
- Docker安装MySQL,Redis,阿里云镜像加速
Docker安装 虚拟化容器技术.Docker基于镜像,可以秒级启动各种容器.每一种容器都是一个完整的环境,容器之间相互隔离. 如果之前安装的有其他版本,卸载旧的版本. $ sudo yum remo ...
- requests+BeautifulSoup | 爬取电影天堂全站电影资源
import requests import urllib.request as ur from bs4 import BeautifulSoup import csv import threadin ...
- MySQL sql命令行操作数据库
数据库命令行操作 命令行操作数据库, [if exists] 可加可不加, 命令行操作一定要加英文分号 ; 结尾 创建数据库 : create database [if not exists] 数据库 ...
- Java JDK8下载 (jdk-8u251-windows-x64和jdk-8u271-linux-x64.tar)
jdk-8u251-windows-x64 和 jdk-8u271-linux-x64.tar 链接:https://pan.baidu.com/s/1gci6aSIFhEhjY8F48qH39Q 提 ...
- 【Oracle】从删除的recyclebin中查看并恢复数据
如果数据库中用了drop删除表,后面没有加上purge的话,会出现在oracle的回收机制中 dba_recyclebin可以查看当前删除的都是哪些 这个只是部分截图,可以看到删除的对象是什么,删除的 ...
- CMU数据库(15-445)Lab1-BufferPoolManager
0. 关于环境搭建请看 https://www.cnblogs.com/JayL-zxl/p/14307260.html 1. Task1 LRU REPLACEMENT POLICY 0. 任务描述 ...
- Linux内核分析_课程学习总结报告
请您根据本课程所学内容总结梳理出一个精简的Linux系统概念模型,最大程度统摄整顿本课程及相关的知识信息,模型应该是逻辑上可以运转的.自洽的,并举例某一两个具体例子(比如读写文件.分配内存.使用I/O ...
- JVM(二)类加载的时机及其过程
类从被加载到虚拟机内存中开始,到卸载出内存为止,它的的整个生命周期包括: 加载(Loading),验证(Verification),准备(Preparation),解析(Resolution),初始化 ...
- 超详细oracle 11g安装步骤 win版本
1. 打开网址: https://edelivery.oracle.com 使用oracle 任意账号登录 账号:2696671285@qq.com 密码:Oracle123 感谢来自某位好心大佬的共 ...