http://acm.fzu.edu.cn/problem.php?pid=2128

分析:利用strstr()函数将每个字串在原串中的首尾位置存储一下,再将首尾从小到大排一下序。(写着写着就感觉和看电视节目那一道题一样一样的啊~)

例子: aaaa  2  aa  aa  答案:1

       abc   1  d  答案:3 

#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#include<vector>
#include<queue>
#include<algorithm> using namespace std;
typedef long long LL; const int maxn=;
const int INF=0x3f3f3f3f;
const int mod=; char str[maxn];
char mstr[]; struct node
{
int s, e;
} a[maxn]; int cmp(node p, node q)
{
if(p.e != q.e)
return p.e<q.e; return p.s<q.s;
} int main()
{
int n, k;
while(scanf("%s", str)!=EOF)
{
scanf("%d", &n); k = ; for(int i=; i<n; i++)
{
scanf("%s", mstr); int cnt = ;
int len = strlen(mstr);
while(strstr(str+cnt, mstr)!=NULL)
{
int p = strstr(str+cnt, mstr)-str;
a[k].s = p;
a[k++].e = p + len - ;
cnt = p + len - ;
}
} a[k].s = ;
a[k++].e = strlen(str); if(k == )
{
printf("%d\n", strlen(str));
continue;
} sort(a, a+k, cmp); int maxs = -INF;
for(int i=k-; i>; i--)
maxs = max(maxs, a[i].e-a[i-].s-); printf("%d\n", maxs);
}
return ;
} /*
ab
1
c
*/

最长子串 FZU2118的更多相关文章

  1. [LeetCode] Longest Substring with At Most Two Distinct Characters 最多有两个不同字符的最长子串

    Given a string S, find the length of the longest substring T that contains at most two distinct char ...

  2. POJ 3294 Life Forms 后缀数组+二分 求至少k个字符串中包含的最长子串

    Life Forms   Description You may have wondered why most extraterrestrial life forms resemble humans, ...

  3. LeetCode: 3_Longest Substring Without Repeating Characters | 求没有重复字符的最长子串的长度 | Medium

    题目: Given a . For . 解题思路: 这个题让找一个字符串中具有不重复单词的最长子串的长度,如:ababc,子串为abc,长度为3.有这么几个方法: 方法一: 依赖字符串本身的一些特有函 ...

  4. [getLongestLength] 加和为0的最长子串长度

    点击这里查看原文 假设一个数组仅仅由1和-1组成,求该数组的和为0的最长子串的长度. 例如: {1,-1,1,-1,1,1,1} 输出:4. 昨天机试的时候做到这道题,不会做,今天思考一下. 普通的解 ...

  5. POJ-3294-Life Forms(后缀数组-不小于 k 个字符串中的最长子串)

    题意: 给定 n 个字符串,求出现在不小于 k 个字符串中的最长子串. 分析: 将 n 个字符串连起来,中间用不相同的且没有出现在字符串中的字符隔开,求后缀数组. 然后二分答案,将后缀分成若干组,判断 ...

  6. 求两个字符串最长子串的LCS算法 C语言实现(简短的实现函数)

    /************************************************************************* > File Name: lcs.c > ...

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

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

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

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

  9. SPOJ PHRASES 每个字符串至少出现两次且不重叠的最长子串

    Description You are the King of Byteland. Your agents have just intercepted a batch of encrypted ene ...

随机推荐

  1. python之类的属性

    class type(object) With one argument, return the type of an object. The return value is a type objec ...

  2. Qt StyleSheet样式表实例(转)

    QT论坛看到的,收藏一下! 在涉及到Qt 美工的时候首先需要掌握CSS 级联样式表. 下面将通过几个例子来介绍一下怎样使用Qt中的部件类型设计.自定义的前台背景与后台背景的颜色: 如果需要一个文本编辑 ...

  3. ios 开发小技巧一

    对于UITableViewCell中的textField/textView,你肯定想让它编辑时可以把所在行滚动到键盘上方.如果你的VC是UITableViewController或者子类,那么只要在o ...

  4. Wampserver2.5配置虚拟主机出现403 Forbidden解决办法

    一直在用APMServ5.2.6,mysql好办,可以用mariadb代替进行升级,但php升级到5.4.7就没办法再升级,安装加速器也困难.就想迁移到Wampserver上来,以前也尝试过Wamps ...

  5. C预处理和C库

    #include <stdio.h> #define MAN(x) "n"##x int main(void) { printf("%s",MAN( ...

  6. 转:LoadRunner负载测试之Windows常见性能计数器,分析服务器性能瓶颈

    发布于2012-10-8,来源:博客园 监测对象 System(系统) l %Total Processor Time 系统中所有处理器都处于繁忙状态的时间百分比,对于多处理器系统来说,该值可以反映所 ...

  7. TortoiseSVN 版本回滚

    尝试用TortoiseSVN进行版本回滚,回滚到的版本和实际的内容有出入,可能是点了太多次给点乱了,囧~ 不过发现一个比较靠谱的方法,如下: 右键点击文件TortoiseSVN->showlog ...

  8. Mac下导出chrome插件

    chrome最强大的功能之一就是插件,有时候需要给小伙伴们共享一些插件,所以需要将自己chrome中的插件打包,在mac下打包插件还是挺费劲的,在此记录. 打开chrome的扩展程序,找到要导出的插件 ...

  9. iptables-qos-tcpcopy-tc-tcpdump

    QOS: https://www.chiphell.com/thread-427876-1-1.html iptables指南: http://man.chinaunix.net/network/ip ...

  10. MySQL大数据分页的优化思路和索引延迟关联

    之前上次在部门的分享会上,听了关于MySQL大数据的分页,即怎样使用limit offset,N来进行大数据的分页,现在做一个记录: 首先我们知道,limit offset,N的时候,MySQL的查询 ...