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 ...
随机推荐
- Spring Boot 2.0 的配置绑定类Bindable居然如此强大
1. 前言 在开发Spring Boot应用时会用到根据条件来向Spring IoC容器注入Bean.比如配置文件存在了某个配置属性才注入Bean : 图中红色的部分是说,只有ali.pay.v1.a ...
- IP包头分析
• IP包头是IP协议(网络层,第三层)为数据包添加的头部. ○ 格式: ○ ○ 拆开看,每行是4+4+8+16=32bit=4Byte ○ ip协议最短20 ...
- 【Azure Developer】Python代码通过AAD认证访问微软Azure密钥保管库(Azure Key Vault)中机密信息(Secret)
关键字说明 什么是 Azure Active Directory?Azure Active Directory(Azure AD, AAD) 是 Microsoft 的基于云的标识和访问管理服务,可帮 ...
- Redis 5 配置 Redis sentinel(哨兵模式)
先了解一下哨兵都 做了什么工作:Redis 的 Sentinel 系统用于管理多个 Redis 服务器(instance), 该系统执行以下三个任务: * 监控(Monitoring): Sentin ...
- 【Java】计算机软件、博客的重要性、编程语言介绍和发展史
之前学得不踏实,重新复习一遍,打扎实基础中. 记录 Java核心技术-宋红康_2019版 & Java零基础学习-秦疆 文章目录 软件开发介绍 软件开发 什么是计算机? 硬件及冯诺依曼结构 计 ...
- 【ORA】ORA-39002,ORA-39070,ORA-29283, ORA-06512,ORA-29283解决办法
今天使用IMPDP导入的时候报了一个错误 ORA-39002: invalid operation ORA-39070: Unable to open the log file. ORA-2928 ...
- 以事实驳斥:改进你的c#代码的5个技巧(四)
测试使用的环境:vs2019+.net core3.1 原文地址:https://www.cnblogs.com/hhhnicvscs/p/14296715.html 反驳第一条:如何检查代码中的空字 ...
- SpringBoot Logback无法获取配置中心属性
SpringBoot Logback无法获取配置中心属性 前言 最近在做项目中,需要把项目中的日志信息通过RabbitMQ将规定格式的消息发送到消息队列中,然后ELK系统通过消息队列拿日志并且保存起来 ...
- all header field names in both HTTP requests and HTTP responses are case-insensitive.
https://tools.ietf.org/html/rfc6455#section-4.2.1 Please note that according to [RFC2616], all heade ...
- UserControl和CustomControl两者区别
UserControl 将多个WPF控件(例如:TextBox,TextBlock,Button)进行组合成一个可复用的控件组: 由XAML和Code Behind代码组成: 不支持样式/模板重写: ...