传送门: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+暴力)的更多相关文章

  1. hdu 2328 Corporate Identity(kmp)

    Problem Description Beside other services, ACM helps companies to clearly state their “corporate ide ...

  2. POJ-3450 Corporate Identity (KMP+后缀数组)

    Description Beside other services, ACM helps companies to clearly state their “corporate identity”, ...

  3. HDU 2328 POJ 3450 KMP

    题目链接:  HDU http://acm.hdu.edu.cn/showproblem.php?pid=2328 POJhttp://poj.org/problem?id=3450 #include ...

  4. POJ 3450 Corporate Identity KMP解决问题的方法

    这个问题,需要一组字符串求最长公共子,其实灵活运用KMP高速寻求最长前缀. 请注意,意大利愿父亲:按照输出词典的顺序的规定. 另外要提醒的是:它也被用来KMP为了解决这个问题,但是很多人认为KMP使用 ...

  5. POJ 3450 Corporate Identity kmp+最长公共子串

    枚举长度最短的字符串的所有子串,再与其他串匹配. #include<cstdio> #include<cstring> #include<algorithm> #i ...

  6. 剪花布条 - HDU 2087(简单KMP | 暴力)

    分析:基础的练习............... ============================================================================ ...

  7. (KMP 暴力)Corporate Identity -- hdu -- 2328

    http://acm.hdu.edu.cn/showproblem.php?pid=2328 Corporate Identity Time Limit: 9000/3000 MS (Java/Oth ...

  8. hdu2328 Corporate Identity【string库使用】【暴力】【KMP】

    Corporate Identity Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  9. POJ 题目3450 Corporate Identity(KMP 暴力)

    Corporate Identity Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 5493   Accepted: 201 ...

随机推荐

  1. Docker踩过的坑

    前言 主要是记录Docker遇到的坑,更多的是因为自己的粗心大意,以此警示 正文 Dockerfile里的RUN 某一次把启动服务的命令写在了 Dockerfile 中,后来发现服务一直拉不起来. 原 ...

  2. C++ 简单输出当前日期时间

    根据https://www.runoob.com/cplusplus/cpp-date-time.html编写. 首先介绍2个数据类型. 一个是time_t,与时间函数相关的变量,定义的变量记录着自 ...

  3. day123:MoFang:直播间列表信息的前后端实现&创建房间的前后端实现

    目录 1.服务端提供所有直播间的列表信息 2.前端显示房间列表 3.创建房间 1.服务端提供所有直播间的列表信息 1.marshmallow.py from marshmallow_sqlalchem ...

  4. js 中const 定义的值是否能更改

    const定义的基本类型不能改变,但是定义的对象是可以通过修改对象属性等方法来改变的. 1. const aa=trueaa=falseconsole.log(aa)VM1089:2 Uncaught ...

  5. docker基础总结

    搜索镜像docker search ubuntu 搜索ubuntu的Docker镜像 搜索结果单个单词ubuntu这样的镜像,被称为基础镜像或根镜像,这些基础镜像由 Docker 公司创建搜索结果ti ...

  6. ORA-12560错误

    ora-12560错误是一个经典错误之一 下面我们分析一下这个错误: 产生这个错误的原因是什么呢? 1.oracle服务没有启动 Linux下查看$ps -ef | grep ora_ windows ...

  7. 【Oracle】instr()函数详解

    1)instr()函数的格式  (俗称:字符查找函数) 格式一:instr( string1, string2 )    /   instr(源字符串, 目标字符串) 格式二:instr( strin ...

  8. oracle_fdw的安装和使用

    1.下载instant oracle client 下载网址:https://www.oracle.com/technetwork/topics/linuxx86-64soft-092277.html ...

  9. 【linux】系统编程-7-网络编程

    目录 前言 10. 网络编程 10.1 简要网络知识 10.2 IP协议 10.2.1 IP地址编址 10.2.2 特殊IP地址 10.2.1 首限广播地址 10.2.2 直接广播地址 10.2.3 ...

  10. 入门OJ:扫雪

    扫雪1 题目描述 大雪履盖了整个城市,市政府要求冬季服务部门尽快将一些街道(列在一份清单中)的积雪清除掉以恢复交通,整个城市由许多交叉路口和街道构成,当然任意两个交叉路口都是直接或间接连通的,清单给出 ...