题意:先给一堆短的字符,当遇到(.).时表示这些短的字符输入结束,然后给一个长的字符(这里注意题意,这个字符不一定在一行,有可能是多行组合的)。
判断由这些字符能组成这个长的字符的前缀最长能有多长。
样例:
A AB BA CA BBC . ABABACABAABC输出是11.
thinking:用个队列装着往后搜索,把每个能到的位置标记,如果没到过就放入队列,然后输出最长的值即可。
涉及strncmp,strcat。
the reason of failure:1、题意理解,长字符是可以由多行组成的
2、数组开小了。
代码
/*
ID: me
PROG: prefix
LANG: C++
*/
#include<iostream>
#include <string.h>
#include <stdio.h>
#include <map>
#include <algorithm>
#include <queue>
using namespace std;
char qq[205][15];
char q1[205000];
char q2[200];
bool walked[205000];//2、
int dfs(int x){
queue<int>gg;
gg.push(0);
int u,i,l1;
int c1=0;
int l2=strlen(q1);
//cout << "l2=" << l2 << endl;
while(!gg.empty()){
u=gg.front();gg.pop();
for(i=1;i<x;i++){
l1=strlen(qq[i]);
if(strncmp(q1+u,qq[i],l1)==0&&walked[l1+u]==0){
walked[l1+u]=1;
gg.push(l1+u);
if(l1+u>c1)c1=l1+u;
}
}
}
return min(c1,l2);
}
int main(){
//freopen("in.txt","r",stdin);
freopen("prefix.in","r",stdin);
freopen("prefix.out","w",stdout);
int i,j,k,l,f1,f2,f3,t1,t2,t3;
memset(qq,0,sizeof(qq));
memset(q1,0,sizeof(q1));
memset(walked,0,sizeof(walked));
memset(q2,0,sizeof(q2));
t1=1;
while(scanf("%s",qq[t1])==1&&qq[t1][0]!='.'){
t1++;
}
//cout << t1<< endl;
while(scanf("%s",q2)==1){//1、
strcat(q1,q2);
}
cout << dfs(t1) << endl;
}