1238.Substrings
find the largest string X, such that either X, or its inverse can be found as a substring of any of the given strings.
题意:找出所有串的最长的公共连续子串
思路:直接从最小的那串,枚举所有子串去寻找
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
int main()
{
int t,n,i,j,k,MIN,f,len,MAX;
char str[105][105],s1[105],s2[105];
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
MIN = 1000;
for(i = 0; i<n; i++)
{
scanf("%s",str[i]);
len = strlen(str[i]);
if(MIN>len)//找到最小串
{
MIN = len;
f = i;
}
}
len = strlen(str[f]);
int flag = 1;
MAX = 0;
for(i = 0;i<len;i++)//作为标本串子串的头
{
for(j = i;j<len;j++)//子串的尾
{
for(k = i;k<=j;k++)//复制为两个串,顺序串s1,逆序串s2
{
s1[k-i] = str[f][k];
s2[j-k] = str[f][k];
}
s1[j-i+1] = s2[j-i+1] = '\0';
int l = strlen(s1);
for(k = 0;k<n;k++)//枚举所有串
{
if(!strstr(str[k],s1) && !strstr(str[k],s2))
{
flag = 0;
break;
}
}
if(l>MAX && flag)
MAX = l;
flag = 1;
}
}
printf("%d\n",MAX);
}
return 0;
}
1548.A strange lift
深搜
#include<stdio.h>
int s[201],book[201],step,num,i,a,n,b;//s[]数组记录每个楼层上的数字,book[]数组记录到楼层的步数
void dfs(int i,int step);
int main()
{
while(scanf("%d",&n))
{
if(n==0)
break;
scanf("%d%d",&a,&b);
for(i=1;i<=n;i++)
{
scanf("%d",&s[i]);
book[i]=99999999;
}
num=99999999;
dfs(a,0);
if(num==99999999)
printf("-1\n");
else
printf("%d\n",num);
}
return 0;
}
void dfs(int i,int step)
{
if(i<1||i>n)
return;
if(i==b&&step<num)
{
num=step;
return;
}
if(step>=book[i])//若此时到i楼的步数小于等于记录的到i楼的步数,返回
return;
book[i]=step;
dfs(i+s[i],step+1);
dfs(i-s[i],step+1);
return;
}
其它:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
int m[205],n[205];
int s,w,z;
struct node{
int x,y;
};
int bfs()
{
queue<node>qu;
int a,b,c,d,e;
node next,head;
head.x=w;
head.y=0;
n[w]=1;
qu.push(head);
while(!qu.empty())
{
head=qu.front();
qu.pop();
if(head.x==z)
{
cout<<head.y<<endl;
return 1;
}
next.x=head.x+m[head.x-1];
next.y=head.y+1;
if(n[next.x]==0&&next.x<=s)
{
qu.push(next);
n[next.x]=1;
}
next.x=head.x-m[head.x-1];
next.y=head.y+1;
if(n[next.x]==0&&next.x>=0)
{
qu.push(next);
n[next.x]=1;
}
}
return 0;
}
int main()
{
int a,b,c;
while(cin>>s)
{
if(!s)
break;
cin>>w>>z;
for(a=0;a<s;a++)
{
cin>>m[a];
}
memset(n,0,sizeof(n));
if(bfs()==0)
cout<<-1<<endl;
}
return 0;
}