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. Docker registry V2

    部署私有Docker Registry 搭建 Insecure Registry 修改Registry server上的Docker daemon的配置,为DOCKER_OPTS增加–insecure ...

  2. AndroidImagePicker 的使用

    github地址 https://github.com/easonline/AndroidImagePicker 效果图

  3. 用vs2010编译和调试多个arx版本的arx项目

    翻译自dev guide<Compile and Debug Mixed-mode projects>    默认vs2010是使用ARX2014,开发AutoCAD2014使用的ARX, ...

  4. OGNL表达式(待解答)

    OGNL表达式的路径到底怎样的?下面代码看不懂为什么要这样?

  5. [转载]强烈推荐学习的blog

    膜拜大牛 原文出处:http://hedengcheng.com/?p=676 ACM Queue (Architecting Tomorrow’s Computing) 网址:http://queu ...

  6. FM/PCM与FM/PPM的区别

    FM/PCM的优点:     1 高可靠性和高抗干扰性.大家知道,一般PPM遥控设备都要求在操作时先开发射机后开接收机,先关接收机后关发射机.其原因是在没有发射信号时,接受机会因自身内部的噪音或外界的 ...

  7. 黄聪: Bootstrap之Form表单验证神器: BootstrapValidator(转)

    前言:做Web开发的我们,表单验证是再常见不过的需求了.友好的错误提示能增加用户体验.博主搜索bootstrap表单验证,搜到的结果大部分都是文中的主题:bootstrapvalidator.今天就来 ...

  8. 黄聪:MYSQL使服务器内存CPU占用过高问题的分析及解决方法

    方法一: 使用 show processlist 语句,查找负荷最重的 SQL 语句,优化该SQL,比如适当建立某字段的索引. 方法二: #查看慢SQL日志是否启用mysql> show var ...

  9. php数据库访问

    从$res获取行数据的时候,处理mysql_fetch_row($res),还有三个方法,分别是 mysql_fetch_row($res); 返回一个所以的数组,速度较快. mysql_fetch_ ...

  10. 【摘】top命令

    1.重要参数解释 VIRT:virtual memory usage.Virtual这个词很神,一般解释是:virtual adj.虚的, 实质的, [物]有效的, 事实上的.到底是虚的还是实的?让G ...