HDU 1238 - Substrings(暴力)

题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1238

【题意】
n n 个字符串中,找出所有字符串中共同拥有的一个子串,该子串(正向或反向)是任何一个母串的子串,求该子串的最长长度( n < = 100 n<=100 , 字符串长度 < = 100 <=100

【思路】
暴力枚举一个串的所有子串,然后和所有串去匹配即可

#include<bits/stdc++.h>
using namespace std;

const int maxn=105;

int n;
char s[maxn];
char g[maxn][maxn];

int main(){
	int T;
	scanf("%d",&T);
	while(T--){
		scanf("%d",&n);
		for(int i=0;i<n;++i) scanf("%s",g[i]);
		memcpy(s,g[0],sizeof(s));
		int len=strlen(s);
		int ans=0;
		for(int i=0;i<len;++i){
			for(int j=i;j<len;++j){
				char tmp[maxn],tmp2[maxn];
				int p=0;
				for(int k=i;k<=j;++k){
					tmp[p++]=s[k];
				}
				tmp[p]='\0';
				strcpy(tmp2,tmp);
				strrev(tmp2);
				bool ok=true;
				for(int k=0;k<n;++k){
					if(NULL==strstr(g[k],tmp) && NULL==strstr(g[k],tmp2)){
						ok=false;
						break;
					}
				}
				if(ok) ans=max(ans,j-i+1);
			}
		}
		printf("%d\n",ans);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/xiao_k666/article/details/82877382