思路: 对于每个子串,求出 母串中 所有该子串 的 开始和结束位置,保存在 mark数组中,求完所有子串后,对mark数组按 结束位置排序,然后 用后一个的结束位置 减去 前一个的 开始 位置 再 减去 1,记录最大值
比如 aaaqwer  1  aaa 那么 最长为 aaqwer

用strstr判断子串是否存在于母串中。

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; const int maxn = 1000005; struct MARK {
int begin, end;
bool operator <(const MARK& cmp) const {
return end < cmp.end;
}
} mark[maxn]; char s[maxn], t[1005][105];
int n, cnt, next[105]; void work(const char str[], const char sub[]) {
//memset(next, 0, sizeof(next));
//get_next(sub, next);
int exp = 0, from, len = strlen(sub);
while (strstr(str + exp, sub) != NULL) {
from = strstr(str + exp, sub) - str;
mark[cnt].begin = from;
mark[cnt].end = from + len - 1;
cnt++;
exp = from + len - 1;
}
} int main() {
while (scanf("%s", s) == 1) {
scanf("%d", &n);
for (int i = 0; i < n; i++)
scanf("%s", t[i]);
cnt = 0;
for (int i = 0; i < n; i++) {
work(s, t[i]);
}
mark[cnt].begin = mark[cnt].end = strlen(s);
cnt++;
sort(mark, mark + cnt);
int ans = -1;
for (int i = 0; i < cnt - 1; i++) {
int len = mark[i + 1].end - mark[i].begin - 1;
if (len > ans)
ans = len;
}
printf("%d\n", ans == -1 ? strlen(s) : ans);
}
}

用KMP判断子串是否存在于母串中。

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; const int maxn = 1000005; struct MARK {
int begin, end;
bool operator <(const MARK& cmp) const {
return end < cmp.end;
}
} mark[maxn]; char s[maxn], t[1005][105];
int n, cnt, next[105]; void get_next(const char *sub, int *next) {
int len = strlen(sub);
int i, k;
next[0] = k = -1;
for (i = 0; i < len;) {
if (k == -1 || sub[i] == sub[k]) {
k++;
i++;
if (sub[k] != sub[i])
next[i] = k;
else
next[i] = next[k];
} else
k = next[k];
}
} int KMP(const char *str, const char *sub, const int *next) {
int i, j;
int len1 = strlen(str), len2 = strlen(sub);
for (i = 0, j = 0; i < len1 && j < len2;) {
if (j == -1 || str[i] == sub[j]) {
i++;
j++;
} else
j = next[j];
}
if (j == len2)
return i - len2;
return -1;
} void work(const char str[], const char sub[]) {
//memset(next, 0, sizeof(next));
get_next(sub, next);
int exp = 0, from = 0, len = strlen(sub);
while ((from = KMP(str + exp, sub, next)) != -1) {
mark[cnt].begin = from + exp;
mark[cnt].end = from + exp + len - 1;
cnt++;
exp += from + len - (len == 1 ? 0 : 1);
}
} int main() {
while (scanf("%s", s) == 1) {
scanf("%d", &n);
for (int i = 0; i < n; i++)
scanf("%s", t[i]);
cnt = 0;
for (int i = 0; i < n; i++) {
work(s, t[i]);
}
// for (int i = 0; i < cnt; i++)
// printf("%d %d\n", mark[i].begin, mark[i].end);
mark[cnt].begin = mark[cnt].end = strlen(s);
cnt++;
sort(mark, mark + cnt);
int ans = -1;
for (int i = 0; i < cnt - 1; i++) {
int len = mark[i + 1].end - mark[i].begin - 1;
if (len > ans)
ans = len;
}
printf("%d\n", ans == -1 ? strlen(s) : ans);
}
}

福州大学第十届校赛 & fzu 2128最长子串的更多相关文章

  1. FZU 2128 最长子串

    题目链接:最长子串 思路:依次找出每个子串的在字符串中的首尾地址,所有子串先按照尾地址从小到大排序.然后首地址从小到大排. 遍历一遍每个子串的首地址和它后面相邻子串的尾地址之差-1, 第一个子串的首地 ...

  2. 河南省第十届省赛 Plumbing the depth of lake (模拟)

    title: Plumbing the depth of lake 河南省第十届省赛 题目描述: There is a mysterious lake in the north of Tibet. A ...

  3. 河南省第十届省赛 Intelligent Parking Building

    title: Intelligent Parking Building 河南省第十届省赛 tags: [模拟,省赛] 题目描述: There is a new revolution in the pa ...

  4. 四川第十届省赛 A.Angel Beats bitset

    四川第十届省赛 A.Angel Beats bitset 题目链接 题解参考:http://www.cnblogs.com/Aragaki/p/9142250.html 考虑用bitset来维护对于所 ...

  5. squee_spoon and his Cube VI---郑大校赛(求最长子串)

    市面上最常见的魔方,是三阶魔方,英文名为Rubik's Cube,以魔方的发明者鲁比克教授的名字命名.另外,二阶魔方叫Pocket Cube,它只有2*2*2个角块,通常也就比较小:四阶魔方叫Reve ...

  6. Problem 2128 最长子串(kmp+strstr好题经典)

     Problem 2128 最长子串 Accept: 134    Submit: 523Time Limit: 3000 mSec    Memory Limit : 65536 KB  Probl ...

  7. CSUST 第15届 校赛总结

    一直想记录一下自己的比赛,却感觉空间说说有点不适,思考了一番还是打算放到自己的博客园 这次比赛总体来说还是不错,签到还是稳的一批,基本前四小时都在rk1 开局切了几道签到题,然后开了一道思维gcd,正 ...

  8. HZNU第十二届校赛赛后补题

    愉快的校赛翻皮水! 题解 A 温暖的签到,注意用gets #include <map> #include <set> #include <ctime> #inclu ...

  9. fzu Problem 2128 最长子串(KMP + strstr 经典好题)

     Problem Description 问题很简单,给你一个字符串s,问s的子串中不包含s1,s2...sn的最长串有多长.  Input 输入包含多组数据.第一行为字符串s,字符串s的长度1到10 ...

随机推荐

  1. 设计模式(十二)职责链模式(Chain of Responsibility)(对象行为型)

     设计模式(十二)职责链模式(Chain of Responsibility)(对象行为型) 1.概述 你去政府部门求人办事过吗?有时候你会遇到过官员踢球推责,你的问题在我这里能解决就解决,不能解决就 ...

  2. 神奇的矩阵 NOI模拟题

    神奇的矩阵 题目大意 有一个矩阵\(A\),第一行是给出的,接下来第\(x\)行,第\(y\)个元素的值为数字\(A_{x-1,y}\)在\(\{A_{x-1,1},A_{x-1,2},A_{x-1, ...

  3. UVA 10603 Fill(正确代码尽管非常搓,网上很多代码都不能AC)

    题目链接:option=com_onlinejudge&Itemid=8&page=show_problem&problem=1544">click here~ ...

  4. Python学习-使用matplotlib画动态多图

    最近常常使用matplotlib进行数学函数图的绘制,可是怎样使用matplotlib绘制动态图,以及绘制动态多图.直到今天才学会. 1.參考文字 首先感谢几篇文字的作者.帮我学会了怎样绘制.大家也能 ...

  5. PHP升级之后$SESSION丢失

    要在生产环境为一个内部系统升PHP版本,由5.3升成5.4.16 生成以后发现不能login,一路打断点过去,发现服务器端两个页面跳转的时候,取不到$SESSION 悲催的上网找解决方案,结果发现各种 ...

  6. 分享非常有用的Java程序(关键代码)(八)---Java InputStream读取网络响应Response数据的方法!(重要)

    原文:分享非常有用的Java程序(关键代码)(八)---Java InputStream读取网络响应Response数据的方法!(重要) Java InputStream读取数据问题 ======== ...

  7. 虚拟化:搭建本地虚拟化环境和安装ubuntu操作系统

    本文介绍如何在本地(windows操作系统)安装虚拟机,并在虚拟机下安装ubuntu操作系统. 一.机器升级 因为是在我的笔记本电脑上操作.首先升级了我的笔记本,买了内存条,将我机器的内存增加到8G, ...

  8. linux网络体系架构

    原创kylin_zeng:http://blog.csdn.net/kylin_fire_zeng  本文参考国嵌视频教程,再此感谢国嵌教育. 一.协议栈层次对比: 1)网络接口层把数据链路层和物理层 ...

  9. SQL Server截取字符串和处理中文技巧

    一 环境介绍 SQL  Server PRINT @@VERSION MicrosoftSQLServer2012-11.0.2100.60(X64) Feb10201219:39:15 Copyri ...

  10. 将DataTable 存到一个集合当中

    将DataTable 存到一个集合中 此做法来自:http://www.codeproject.com/Articles/692832/Simple-way-of-using-SQL-DataTabl ...