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,否则最长的公共子串.有多组解时输出字典序最 ...
随机推荐
- winrar3.7-winrar4.0的注冊码
首先新建记事本文件(txt文件),把下面红色代码复制进去,然后将文件另存为以 rarreg.key 为文件名称的文件(当然因为设置的不同,可能出现你保存后的文件为 rarreg.key.txt 没关系 ...
- DC综合流程
Design Compiler and the Design Flow 步骤 将HDL描述的设计输入到Design Compiler中 Design Compiler使用technology libr ...
- 忘记 mysql5.5.24 数据库 root 密码
兹整理如下,供网友参考 第一步:关闭mysql服务 第二步:新建txt 写入如下内容 UPDATE mysql.user SET Password=PASSWORD('blog.const.net.c ...
- IE标签a嵌套table标签,链接点击无效
在IE中,使用如下代码将无法触发跳转: <a href="http://xx.xx.com"> <table> <tr> <td>点 ...
- (转)SQL NEWID()随机函数
从A表随机取2条记录,用SELECT TOP 10 * FROM ywle order by newid()order by 一般是根据某一字段排序,newid()的返回值 是uniqueidenti ...
- C#Log4net日志记录组件的使用
一.Log4Net介绍 Log4net是基于.NET开发的一款非常著名的记录日志开源组件.它通过一套XML配置的日志引擎,将日志分不同的等级,分别是:FATAL . ERROR. WARN. INFO ...
- css控制图片变灰色,彩色
<A href="链接地址"><IMG src="p1.jpg" border="0"></A> < ...
- 使用PHP对数据库输入进行恶意代码清除
这是一个有用的PHP函数清理了所有的输入数据,并删除代码注入的几率. function sanitize_input_data($input_data) { $input_data = trim(ht ...
- C程序设计语言练习题1-1
练习1-1 在你自己的系统中运行"hello, world"程序.再有意去掉程序中的部分内容,看看会得到什么出错信息. 代码如下: #include <stdio.h> ...
- Swift—静态属性- 备
我先来设计一个类:有一个Account(银行账户)类,假设它有3个属性:amount(账户金额).interestRate(利率)和owner(账户名). 在这3个属性中,amount和owner会因 ...