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,否则最长的公共子串.有多组解时输出字典序最 ...
随机推荐
- 左右TextView旋转门问题
最近由于旋转门问题的一个客户找我,当在字符25更多的时候是不是走了,后来,我在重现的问题,发现问题如下面: 问题1.人物25几个月之内可以去. 问题2.在人物25个月,虽然比屏幕宽度,不去 问题3.屏 ...
- jsPlumb开发入门教程(实现html5拖拽连线)
jsPlumb是一个强大的JavaScript连线库,它可以将html中的元素用箭头.曲线.直线等连接起来,适用于开发Web上的图表.建模工具等.它同时支持jQuery+jQuery UI.MooTo ...
- Linux字符设备驱动file_operations
struct _file_operations struct _file_operations在Fs.h这个文件里面被定义的,如下所示: struct file_operations { struct ...
- linux命令之seq
seq命令简述 seq命令比较常用,在需要做循环的时候用于产生一个序列是再合适不过的工具了,常用方法也比较简单: Usage: seq [OPTION]... LAST seq [ ...
- 自定义悬浮按钮:FloatingButton
floating_button_layout.xml <?xml version="1.0" encoding="utf-8"?> <Rela ...
- ViewPager欢迎页
布局 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:to ...
- 64位Windows 7平台安装32位Timesten,配置ODBC数据源
问题: 由于系统版本原因,客户机只能安装32位的Timesten,但客户机的平台是64位的win 7,安装完成后按照常规的控制面板->管理工具->数据源(ODBC)打开的ODBC数据源管理 ...
- 8 个实用的 Linux netcat 命令示例
Netcat 或者叫 nc 是 Linux 下的一个用于调试和检查网络工具包.可用于创建 TCP/IP 连接,最大的用途就是用来处理 TCP/UDP 套接字. 这里我们将通过一些实例来学习 netca ...
- SmartQQ二维码登陆接口分析
SmartQQ是腾讯在Web上推出的一款单纯的聊天工具,pc端与移动端都可以访问,接下来具体的分析下登陆流程. 网站:http://w.qq.com/ 工具:这个随意能够看到http数据包就可以,浏览 ...
- nodejs个人配置
国内镜像,飞一般的感觉!编辑 ~/.npmrc 加入下面内容 registry = http://registry.cnpmjs.org npm config set registry http:/ ...