POJ 3450 Corporate Identity (KMP+暴搞)
题意:
给定N个字符串,寻找最长的公共字串,如果长度相同,则输出字典序最小的那个。
找其中一个字符串,枚举它的所有的字串,然后,逐个kmp比较.......相当暴力,可二分优化。
#include <cstdio>
#include <cmath>
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;
char str[4111][221];
int next[222]; void getnext(char *t) {
int i=0,j=-1;
int len = strlen(t);
next[0] = -1;
while(i < len) {
if(t[i] == t[j] || j == -1) {
i ++;
j ++;
next[i] = j;
} else j = next[j];
}
} int kmp(char *s,char *t) {
int lens = strlen(s);
int lena = strlen(t);
int i=0,j=0;
while(i < lens && j < lena) {
if(s[i] == t[j] || j == -1) {
i++;
j++;
} else j = next[j];
}
if(j < lena) return -1;
return i - lena;
} int main() {
int n,len;
while(cin >> n && n) {
char tmp[222];
int minn = 111111;
for(int i=0; i<n; i++) {
scanf("%s",str[i]);
len = strlen(str[i]);
if(minn > len) {
minn= len;
strcpy(tmp,str[i]);
}
}
len = strlen(tmp);
char p[222];
char final[222] = {0};
int ans = 0;
for(int i=1; i<=len; i++) { //枚举所有的字串
int cnt;
for(int j=0; j + i<=len; j++) {
cnt = 0;
strncpy(p,tmp+j,i);
p[i] = '\0'; getnext(p);
for(int k=0; k<n; k++) { //逐个比较
if(kmp(str[k],p) != -1) {
cnt ++;
}
else break;
}
if(cnt == n) {
ans++; if(strlen(final) < strlen(p)) strcpy(final,p);
else if(strcmp(final,p) > 0) strcpy(final,p); }
}
}
if(ans == 0) printf("IDENTITY LOST\n");
else {
printf("%s\n",final);
}
}
return 0;
}
POJ 3450 Corporate Identity (KMP+暴搞)的更多相关文章
- POJ 3450 Corporate Identity KMP解决问题的方法
这个问题,需要一组字符串求最长公共子,其实灵活运用KMP高速寻求最长前缀. 请注意,意大利愿父亲:按照输出词典的顺序的规定. 另外要提醒的是:它也被用来KMP为了解决这个问题,但是很多人认为KMP使用 ...
- POJ 3450 Corporate Identity kmp+最长公共子串
枚举长度最短的字符串的所有子串,再与其他串匹配. #include<cstdio> #include<cstring> #include<algorithm> #i ...
- POJ 3450 Corporate Identity(KMP)
[题目链接] http://poj.org/problem?id=3450 [题目大意] 求k个字符串的最长公共子串,如果有多个答案,则输出字典序最小的. [题解] 我们对第一个串的每一个后缀和其余所 ...
- POJ 3450 Corporate Identity (KMP,求公共子串,方法很妙)
http://blog.sina.com.cn/s/blog_74e20d8901010pwp.html我采用的是方法三. 注意:当长度相同时,取字典序最小的. #include <iostre ...
- poj 3450 Corporate Identity
题目链接:http://poj.org/problem?id=3450 题目分类:后缀数组 题意:求n个串的最长公共字串(输出字串) //#include<bits/stdc++.h> # ...
- POJ 题目3450 Corporate Identity(KMP 暴力)
Corporate Identity Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 5493 Accepted: 201 ...
- POJ-3450 Corporate Identity (KMP+后缀数组)
Description Beside other services, ACM helps companies to clearly state their “corporate identity”, ...
- hdu 2328 Corporate Identity(kmp)
Problem Description Beside other services, ACM helps companies to clearly state their “corporate ide ...
- POJ 3450 后缀数组/KMP
题目链接:http://poj.org/problem?id=3450 题意:给定n个字符串,求n个字符串的最长公共子串,无解输出IDENTITY LOST,否则最长的公共子串.有多组解时输出字典序最 ...
随机推荐
- poj 3181 Dollar Dayz (整数划分问题---递归+DP)
题目:http://poj.org/problem?id=3181 思路:将整数N划分为一系列正整数之和,最大不超过K.称为整数N的K划分. 递归:直接看代码: 动态规划:dp[i][j]:=将整数i ...
- Stack and queue.
队列的定义及基本运算 1.定义 队列(Queue)是只允许在一端进行插入,而在另一端进行删除的运算受限的线性表 (1)允许删除的一端称为队头(Front). (2)允许插入的一端称为队尾(Rea ...
- com.intellij.javaee.oss.admin.jmx.JmxAdminException: com.intellij.execution.ExecutionException idea 导出war 报错
com.intellij.javaee.oss.admin.jmx.JmxAdminException: com.intellij.execution.ExecutionException 部署到in ...
- 使用OpenXml实现生成数据字典文档(beta)
最近项目在走验收流程,之前没有仔细看SOW文档,发现需要补好多份文档,其中就有数据字典,项目组不愿意花时间太多的时间弄这些文档,也不希望以后还要重复劳动力,最终决定做一个工具,方便自己生成数据字典文档 ...
- (转)asp.net实现忘记密码找回的代码
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.or ...
- Geodatabase - 修改字段别名(Field Alias)
以下代码演示的是通过个人数据库打开要素类,并对指定的字段别名进行修改,其中,需要注意的是,不能通过Engine中的AxMapControl直接获得,如 //直接获得IFeatureClass. //E ...
- <display:table>属性解释
参考官方网站:http://www.displaytag.org/1.2/displaytag/tagreference.html 所有属性: cellpadding,cellspacing,clas ...
- oracle开机自启动-超简单
1. 在/etc/oratab中作如下修改$ORACLE_SID:$ORACLE_HOME:Y例如vi /etc/orataborcl:/u01/app/oracle/product/10.2.0/d ...
- pch文件出现no such file or directory错误
一般出现这种情况是由于项目直接拷贝到其他电脑上运行... clang: error: no such file or directory: '/demo2/控件代码/13/Recorder/Recor ...
- Implement Insert and Delete of Tri-nay Tree
问题 Implement insert and delete in a tri-nary tree. A tri-nary tree is much like a binary tree but wi ...