经典搜索题目
hdu 1238
思路對(duì)了,題目被A的可能就大了
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<cctype>
#include<algorithm>
#include <vector>
#include <queue>
using namespace std;
#define INF 0x3f3f3f3f
#define N 200
char s[N][N], minstr[N], str[N], revstr[N], tmp[N];
int minlen, n;
int judge(char s1[], int k)
{
strncpy(str, s1, k);//strncpy的用處,真的很好用,復(fù)制后一個(gè)字符串最多n個(gè)字符到第一個(gè)字符串
strcpy(tmp, str);
strrev(tmp);
strcpy(revstr, tmp);
for(int i=0; i<n; i++)
{
if(strstr(s[i], str)==0&&strstr(s[i], revstr)==0)//在前一個(gè)字符串中搜索看有沒有第二個(gè)字符串,沒有時(shí)返回NULL(也就是0)
return 0;
}
return 1;
}
int solve()
{
for(int i=minlen; i>=0; i--)
{
memset(str, 0, sizeof(str));
memset(revstr, 0, sizeof(revstr));
for(int j=0; j+i<=minlen; j++)
{
if(judge(minstr+j, i))
return i;
}
}
return 0;
}
int main()
{
int T;
scanf("%d", &T);
while(T--)
{
scanf("%d", &n);
minlen=INF;
for(int i=0; i<n; i++)
{
scanf("%s", s[i]);
if(strlen(s[i])<minlen)
{
minlen=strlen(s[i]);
strcpy(minstr, s[i]);
}
}
int ans=solve();
printf("%d
", ans);
}
return 0;
}
總結(jié)
- 上一篇: 无忧旅行翻译(中国电信也有类似的无忧旅行
- 下一篇: 骈文的特点(什么是骈文)