标签:
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=1000009; const int INF=0x3f3f3f3f; const int mod=2009; char str[maxn]; char mstr[110]; 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 = 0; for(int i=0; i<n; i++) { scanf("%s", mstr); int cnt = 0; 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 - 1; cnt = p + len - 1; } } a[k].s = 0; a[k++].e = strlen(str); if(k == 1) { printf("%d\n", strlen(str)); continue; } sort(a, a+k, cmp); int maxs = -INF; for(int i=k-1; i>0; i--) maxs = max(maxs, a[i].e-a[i-1].s-1); printf("%d\n", maxs); } return 0; } /* ab 1 c */
标签:
原文地址:http://www.cnblogs.com/daydayupacm/p/5798336.html