传送门: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. 【Azure Developer】使用Postman获取Azure AD中注册应用程序的授权Token,及为Azure REST API设置Authorization

    Azure Active Directory (Azure AD) is Microsoft's cloud-based identity and access management service, ...

  2. Svm算法原理及实现

    Svm(support Vector Mac)又称为支持向量机,是一种二分类的模型.当然如果进行修改之后也是可以用于多类别问题的分类.支持向量机可以分为线性核非线性两大类.其主要思想为找到空间中的一个 ...

  3. Python使用Protobuf&&如何赋值&&如何正反序列化

    前言 使用protobuf主要是两个步骤,序列化和反序列化. 关于Proto有哪些数据类型,然后如何编写,此处就不赘述了,百度一下有很多. 此文主要是总结,python使用protobuf的过程,如何 ...

  4. 如何构建一个多人(.io) Web 游戏,第 1 部分

    原文:How to Build a Multiplayer (.io) Web Game, Part 1 GitHub: https://github.com/vzhou842/example-.io ...

  5. 【MySQL】centos6中/etc/init.d/下没有mysqld启动文件,怎么办

    如果/etc/init.d/下面没有mysqld的话,service mysqld start也是不好使的,同样,chkconfig mysqld on也是不能用 解决办法: 将mysql的mysql ...

  6. 【ASM】查看ASM磁盘组剩余容量和总容量

    col total_size for a10; col free_size for a20; select name,total_mb/1024 || 'G' as total_size , free ...

  7. random模块常用函数

    random模块常用函数: from random import * # Random float: 0.0 <= x < 1.0 random() # Random float: 2.5 ...

  8. ubuntu20.04并添加桌面快捷方式,以安装火狐可浏览器开发版(水狐)为例

    @参考原文 1. 下载linux版源文件 从火狐官网下载linux版的水狐源文件压缩包,@火狐浏览器开发版(水狐)下载地址. 2. 解压下载源文件 将下载的"tar.bz2"文件解 ...

  9. 5V充12.6V三节锂电池,5V升压12.6V的电路图

    三串锂电池的充电电压是三串锂电池的最高电压值,就是12.6V了.5V充12.6V是5V给三串锂电池充电.如笔记本的USB口5V给三串锂电池充电,如5V的适配器或者手机充电器插上数据线给三串锂电池充电电 ...

  10. Vue的核心思想

    Vue的核心思想主要分为两部分: 1.数据驱动  2.组件系统 1.数据驱动 在传统的前端交互中,我们是通过Ajax向服务器请求数据,然后手动的去操作DOM元素,进行数据的渲染,每当前端数据交互变化时 ...