最长子串(FZU2128)

最长子串

Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Submit Status Practice FZU 2128

Description

问题很简单,给你一个字符串s,问s的子串中不包含s1,s2...sn的最长串有多长。

Input

输入包含多组数据。第一行为字符串s,字符串s的长度1到10^6次方,第二行是字符串s不能包含的子串个数n,n<=1000。接下来n行字符串,长度不大于100。

字符串由小写的英文字符组成。

Output

最长子串的长度

Sample Input

lgcstraightlalongahisnstreet

5

str

long

tree

biginteger

ellipse

Sample Output

12

#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std;

char str[1000005];
int len;
struct kode
{
    char s[105];
} a[1005];

struct node
{
    int s,e;
} b[1000005];

int cmp(node a,node b)
{
    return a.e<b.e;
}

int main()
{
    int n,ans;
    while(~scanf("%s",str))
    {
        int i,j,k;
        scanf("%d",&n);
        len = 0;
        for(i = 0; i<n; i++)
        {
            scanf("%s",a[i].s);
            int pos = 0;
            int l = strlen(a[i].s);
            while(strstr(str+pos,a[i].s)!=NULL)
            {
                int f = strstr(str+pos,a[i].s)-str;
                b[len].s=f;
                b[len].e=f+l-1;
                len++;
                pos = f+l-1;
                // printf("%d %s\n",f,str+pos);
            }
        }
        b[len].s = b[len].e = strlen(str);
        len++;
        sort(b,b+len,cmp);
        ans = -1;
        // printf("len = %d\n",len);
        for(i = 1; i<len; i++)
        {
            int tem = b[i].e-b[i-1].s-1;
            // printf("%d\n",tem);
            ans = max(ans,tem);
        }
        if(ans == -1)
            printf("%d\n",strlen(str));
        else
            printf("%d\n",ans);

    }

    return 0;
}
时间: 2024-08-24 05:50:20

最长子串(FZU2128)的相关文章

Leetcode3---&gt;无重复字符的最长子串长度

题目:给定一个字符串string,找出string中无重复字符的最长子串. 举例: Given "abcabcbb", the answer is "abc", which the length is 3. Given "bbbbb", the answer is "b", with the length of 1. Given "pwwkew", the answer is "wke"

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

 Problem 2128 最长子串 Accept: 134    Submit: 523Time Limit: 3000 mSec    Memory Limit : 65536 KB  Problem Description 问题很简单,给你一个字符串s,问s的子串中不包含s1,s2...sn的最长串有多长.  Input 输入包含多组数据.第一行为字符串s,字符串s的长度1到10^6次方,第二行是字符串s不能包含的子串个数n,n<=1000.接下来n行字符串,长度不大于100. 字符串由小

LeetCode 记录之求最长子串

//the basic idea is, keep a hashmap which stores the characters in string as keys and their positions as values, and keep two pointers which define the max substring. move the right pointer to scan through the string , and meanwhile update the hashma

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

题目: Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest s

数据结构——算法之(032)(求两个串中的第一个最长子串)

[申明:本文仅限于自我归纳总结和相互交流,有纰漏还望各位指出. 联系邮箱:[email protected]] 题目: 求两个串中的第一个最长子串(神州数码曾经试题).如"abractyeyt","dgdsaeactyey"的最大子串为"actyey". 题目分析: 1.这里仅仅是实现了简单的字符串算法(最大支持字符串长度64),主要是展示算法思想 2.思路是把2个字符串每一个字符的匹配关系,映射到一张二维数组表中,匹配写1,非匹配写0 算法实现

需找字符串中重复的最长子串

注意此处说的是重复的最长子串,只要出现重复就好,没说重复的次数. 下面给出的代码是编程珠玑中给出的一种实现. 他是通过后缀数组的方式实现的. #include<stdlib.h> #include<stdio.h> #include<string.h> using namespace std; int comlen(char*p, char*q)//找出公共的最长子串 { int i = 0; if (p == NULL || q == NULL) return 0;

在字符串中找到全部是由指定的字符组成的最长子串

/* *!============================================================== *! FNAME: search.cpp *! BRIEF: *! AUTHR: RollStone *! EMAIL: [email protected] *! VERNO: 1.0.31 *! CREAT: 2015-04-28 23:43:04 *! CHGON: 2015-04-30 07:53:00 *! *! Copyright (c) 2015 A

Uva 12012 Detection of Extraterrestrial 求循环节个数为1-n的最长子串长度 KMP

题目链接:点击打开链接 题意: 给定一个字符串str 求字符串str的 循环节个数为 1-len 个的 最长子串长度 思路:套用kmp的性质 #include<string.h> #include<stdio.h> #include <iostream> using namespace std; #define n 1300 void getnext(char str[n],int next[n]){ int m=strlen(str); next[0]=next[1]

Java 最长子序列和最长子串

最长子序列:匹配的字符不需要连续. 最长子串: 匹配的字符需要连续,可能有多种结果. 解决思路:将输入字符串1看作行, 输入字符串2看作列,构成二位数组,然后将对角线匹配字符的值标记为1,计算满足条件的匹配字符个数即可. 基本思想: 空间换时间,动态规划. 图解与公式(只针对最长子序列,最长子串类似) 状态转移方程 直观版: 最长子序列 1 /** 2 * find longest common sequence from two input string 3 * @param s1 4 * @