字符串截取模板 && POJ 3450、3080 ( 暴力枚举子串 && KMP匹配 )
//截取字符串 ch 的 st~en 这一段子串返回子串的首地址
//注意用完需要根据需要最后free()掉
char* substring(char* ch,int st,int en)
{
;
char* pch=ch;
);
pch=pch+st;
;i<length;i++) subch[i]=*(pch++);
subch[length]='\0';
return subch;
}
字符串截取
题意 : 给出 n 个包含 60 个字符的字符串,问你这 n 个字符串的最长公共子串是什么,如果有多个,输出字典序最小的,如若没有则输出 “no significant commonalities”
分析 : 直接选择第一个串然后从长到短截取各个长度的子串去跟剩下的 n-1 个串进行匹配,如果得以匹配则比较这个长度下的其他未枚举的子串,选出字典序最小的继续匹配,持续更新答案,最后输出即可。
#include<string.h>
#include<stdio.h>
#include<string>
#include<malloc.h>
#include<stdlib.h>
using namespace std;
+ ;
][maxn], fir[maxn];
char * mo, * ans;
int num, Next[maxn], moL;
char* substring(char* ch,int st,int en)
{
;
char* pch=ch;
);
pch=pch+st;
;i<length;i++) subch[i]=*(pch++);
subch[length]='\0';
return subch;
}
inline void GetNext()
{
, j = -;
Next[i] = j;
while(i < moL){
&& mo[i]!=mo[j]) j = Next[j];
Next[++i] = ++j;
}
}
bool KmpCount(int who)
{
, j = ;
){
&& mo[j]!=str[who][i]) j = Next[j];
i++, j++;
}
if(j == moL) return true;
return false;
}
bool Is_Match(int st, int en)
{
moL = en - st + ;
mo = substring(fir, st, en);
GetNext();
; k<num; k++){
if(!KmpCount(k))
return false;
}return true;
}
inline void Find_better_ans(int pos, int len, int en)
{
; i<=en; i++){
,j=i; j<=i+len; j++,k++){
if(ans[k] > fir[j]){
if(Is_Match(i, i+len)){
ans = substring(mo, , moL-);
}else { free(mo); break; }
}else if(ans[k] < fir[j]) break;
}
}
}
bool Have_ans()
{
; len>=; len--){///枚举截取子串的长度,从大到小枚举
; i<=-len; i++){///在这个长度下,枚举所有子串的首字母
if(Is_Match(i, i+len)){///判断是否匹配
ans = substring(mo, , moL-);
Find_better_ans(i, len, -len);
return true;
}else free(mo);
}
}return false;
}
int main(void)
{
int nCase;
scanf("%d", &nCase);
while(nCase--){
scanf("%d", &num);
scanf("%s", fir);
; i<num; i++) scanf("%s", str[i]);
if(Have_ans()) { puts(ans); free(ans); }
else puts("no significant commonalities");
}
;
}
题意 : 给出 n 个字符串,问你最长的公共子串,没有则输出 “IDENTITY LOST”
分析 : 这次直接选一个最短的,枚举其所有的子串去匹配剩下的 n-1 个串即可
#include<stdio.h>
#include<string.h>
#include<malloc.h>
#include<algorithm>
;
char StrGroup[maxn][maxn], str[maxn], *mo, *ans;
int moL, Next[maxn], num, MinL, index, Len[maxn];
char* substring(int st,int en)
{
;
);
,j=st;i<length;j++,i++) subch[i]=StrGroup[index][j];
subch[length]='\0';
return subch;
}
inline void GetNext()
{
, j = -;
Next[i] = j;
while(i < moL){
&& mo[i]!=mo[j]) j = Next[j];
Next[++i] = ++j;
}
}
bool RunKmp(int who)
{
, j = ;
while(j < moL && i < Len[who]){
&& mo[j]!=StrGroup[who][i]) j = Next[j];
i++, j++;
}
if(j == moL) return true;
return false;
}
bool RunMatch()
{
GetNext();
; i<=num; i++){
if(i!=index){
if(!RunKmp(i)) return false;
}
}return true;
}
inline void Find_Better_Ans(int st, int len)
{
; i<=MinL-len; i++){
; k<len; j++,k++){
//printf("ok %s\n", ans);
if(ans[k] > StrGroup[index][j]){
mo = substring(i, i+len-);
moL = strlen(mo);
if(RunMatch()){
free(ans);
ans = mo;
break;
}else{ free(mo); break; }
}else if(ans[k] < StrGroup[index][j]) break;
}
}
}
bool Match(int len)
{
; i<=MinL-len; i++){
moL = len;
mo = substring(i, i+len-);
if(RunMatch()){
ans = mo;
Find_Better_Ans(i, len);
return true;
}else free(mo);
}return false;
}
inline void PrintAns()
{
; len--)
if(Match(len)){ puts(ans); free(ans); return; }
puts("IDENTITY LOST");
}
int main(void)
{
while(~scanf("%d", &num) && num){
MinL = 0x3f3f3f3f;
; i<=num; i++){
scanf("%s", StrGroup[i]);
Len[i] = strlen(StrGroup[i]);
if(MinL > Len[i]){
MinL = Len[i];
index = i;
}
}PrintAns();
}
;
}
字符串截取模板 && POJ 3450、3080 ( 暴力枚举子串 && KMP匹配 )的更多相关文章
- hdu_2328_Corporate Identity(暴力枚举子串+KMP)
题目链接:hdu_2328_Corporate Identity 题意: 给你n个串,让你找这n个串的最大公共子串 题解: 串比较小,暴力枚举第一个的子串,然后KMP判断是否可行 #include&l ...
- poj 3080 Blue Jeans (暴力枚举子串+kmp)
Description The Genographic Project is a research partnership between IBM and The National Geographi ...
- POJ 2912 - Rochambeau - [暴力枚举+带权并查集]
题目链接:http://poj.org/problem?id=2912 Time Limit: 5000MS Memory Limit: 65536K Description N children a ...
- POJ 3080 Blue Jeans (字符串处理暴力枚举)
Blue Jeans Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 21078 Accepted: ...
- 【poj 3080】Blue Jeans(字符串--KMP+暴力枚举+剪枝)
题意:求n个串的字典序最小的最长公共子串. 解法:枚举第一个串的子串,与剩下的n-1个串KMP匹配,判断是否有这样的公共子串.从大长度开始枚举,找到了就break挺快的.而且KMP的作用就是匹配子串, ...
- POJ - 3294~Relevant Phrases of Annihilation SPOJ - PHRASES~Substrings POJ - 1226~POJ - 3450 ~ POJ - 3080 (后缀数组求解多个串的公共字串问题)
多个字符串的相关问题 这类问题的一个常用做法是,先将所有的字符串连接起来, 然后求后缀数组 和 height 数组,再利用 height 数组进行求解. 这中间可能需要二分答案. POJ - 3294 ...
- Thinkphp 模板中直接对数据处理 模板中使用函数 中文字符串截取
1.Thinkphp 模板中直接对数据处理:{$data.name|substr=0,3} 2.中文字符串截取函数:mb_substr=0,14,'utf-8' 3.中文字符串统计:iconv_str ...
- Codeforces Round #410 (Div. 2)(A,字符串,水坑,B,暴力枚举,C,思维题,D,区间贪心)
A. Mike and palindrome time limit per test:2 seconds memory limit per test:256 megabytes input:stand ...
- POJ - 1426 暴力枚举+同余模定理 [kuangbin带你飞]专题一
完全想不到啊,同余模定理没学过啊,想起上学期期末考试我问好多同学'≡'这个符号什么意思,都说不知道,你们不是上了离散可的吗?不过看了别人的解法我现在会了,同余模定理介绍及运用点这里点击打开链接 简单说 ...
随机推荐
- C++:函数求阶乘(如有不好之处还请谅解)
#include<iostream> using namespace std; long long f1(int n); int main() { int n=0; cin>> ...
- ssh远程连接linux服务器并执行命令
详细方法: SSHClient中的方法 参数和参数说明 connect(实现ssh连接和校验) hostname:目标主机地址 port:主机端口 username:校验的用户名 password:登 ...
- 你知道e.g.和i.e.的区别吗?
见 i.e. 是对前面的完全举例,特指 e.g. 则是不完全举例,有可能是...也有可能是...还可能是其他. 注意,i.e. 和 e.g. 第二个点后面都常跟一个逗号.
- <<用法
数据移位运算符,左移几位,如:x=i<<4;就是将i的值左移4位(放大2的4次方)后,赋给x,若i=2,则X=32.
- java 接入微信 spring boot 接入微信
1.pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="h ...
- Java——ArrayList使用Demo
三种遍历方式 通过迭代器Iterator遍历 通过get(索引值)遍历 for循环遍历 ArrayList使用Demo package list; import java.util.ArrayList ...
- 拦截器Interceptor和过滤器Filter的区别
(1)过滤器(Filter):当你有一堆东西的时候,你只希望选择符合你要求的某一些东西.定义这些要求的工具,就是过滤器.(理解:就是一堆字母中取一个B) (2)拦截器(Interceptor):在一个 ...
- steps 步骤条、时间轴
steps 步骤条.时间轴:http://www.fxss5201.cn/project/plugin/steps/1.0/ Github地址:https://github.com/fxss5201/ ...
- 对C++拷贝构造函数的一点理解
一. 什么是拷贝构造函数 先看一个简单的例子: #include <iostream> using namespace std; class CExample { private: int ...
- 洛谷P2507 [SCOI2008]配对 题解(dp+贪心)
洛谷P2507 [SCOI2008]配对 题解(dp+贪心) 标签:题解 阅读体验:https://zybuluo.com/Junlier/note/1299251 链接题目地址:洛谷P2507 [S ...