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
*/
04-30 20:09