POJ 3080 Blue Jeans
来源:互联网 发布:怎么更改自己淘宝网址 编辑:程序博客网 时间:2024/06/05 16:46
题意:
就是求k个长度为60的字符串的最长连续公共子串,2<=k<=10
1、 最长公共串长度小于3不输出。
2、 若出现等长的最长的子串,则输出字典序最小的串。
算法思路:按字符串的长度由短到长进行快排。枚举第一个字符串的不同长度子串,判断她是否为下面多有的公共子串?如果是的话,那么我们就表明找到,则比较其长度,如果比已经找到的串长,那么就替换结果串 否则按字典序比较。取字典序考前的,就可以。
#include<iostream>#include<cstring>#include<stdio.h>using namespace std;const int Max = 62;char text[10][Max], pat[Max];int n, maxs, lenp, next[Max];void get_next(){ int i = 0, j = -1; next[0] = -1; while(i < lenp) { if(j == -1 || pat[i] == pat[j]) { i ++; j ++; next[i] = j; } else j = next[j]; }}void KMP()//KMP求出这个后缀串与其余所有串的最大匹配。{ int k, m, i, j; get_next(); maxs = 100; for(k = 1; k < n; k ++) { i = 0; j = 0; m = 0; while(i < 60 && j < lenp) { if(j == -1 || text[k][i] == pat[j]) { i ++; j ++; } else j = next[j]; if(j > m) //单个求最大 m = j; } if(m < maxs) maxs = m;//整体求最小 }}int main(){ int t, i; char result[Max]; scanf("%d", &t); while(t --) { scanf("%d", &n); for(i = 0; i < n; i ++) scanf("%s", text[i]); int ans = 0; for(i = 0; i <= 60; i ++) { strcpy(pat, text[0] + i); // 枚举第一个串的所有后缀串 lenp = 60 - i; KMP(); // KMP求出这个后缀串与其余所有串的最大匹配。 if(ans < maxs) { ans = maxs; strncpy(result, text[0] + i, ans);//将ans从text[0]+i开始的ans个字符的部分放到result中 result[ans] = '\0'; } else if(ans == maxs) { // 存在多个最长公共子串,输出字典序最小的,WA了一次。 char tmp[Max]; strncpy(tmp, text[0] + i, ans); // 复习: strncpy()没有复制最后的'\0'。 tmp[ans] = '\0'; if(strcmp(tmp, result) < 0) strcpy(result, tmp); } } if(ans >= 3) printf("%s\n", result); else printf("no significant commonalities\n"); } return 0;}
0 0
- poj 3080 Blue Jeans
- poj 3080 Blue Jeans
- poj 3080 Blue Jeans
- poj 3080 Blue Jeans
- poj 3080 Blue Jeans
- POJ 3080 Blue Jeans
- POJ 3080 Blue Jeans
- POJ 3080 Blue Jeans
- POJ 3080 Blue Jeans
- Poj 3080 Blue Jeans
- POJ 3080 Blue Jeans
- POJ 3080 Blue Jeans
- POJ 3080 Blue Jeans
- poj 3080 Blue Jeans
- poj 3080 Blue jeans
- POJ 3080 Blue Jeans
- POJ 3080 Blue Jeans
- POJ 3080-Blue Jeans
- hdu 2087 剪花布条 求模式串在主串中的个数
- 经典排序算法 - 冒泡排序Bubble sort
- Android访问网络,使用HttpURLConnection还是HttpClient?
- iOS状态栏颜色的修改
- BZOJ 1190: [HNOI2007]梦幻岛宝珠
- POJ 3080 Blue Jeans
- You must supply a resourceID for a TextView
- Java工程师技术栈--成神之路
- Spring JdbcTemplate方法详解
- linux C语言常见错误
- 欢迎使用CSDN-markdown编辑器
- python正则表达式模块,re.sub的函数使用
- CF 598C Nearest vectors (精度)
- 手动配置hibernate5.1入门(1)