【题解】洛谷P1019[NOIP2000T3]单词接龙 dfs

题目链接

题目描述

单词接龙是一个与我们经常玩的成语接龙相类似的游戏,现在我们已知一组单词,且给定一个开头的字母,要求出以这个字母开头的最长的“龙”(每个单词都最多在“龙”中出现两次),在两个单词相连时,其重合部分合为一部分,例如 b e a s t beast beast a s t o n i s h astonish astonish,如果接成一条龙则变为 b e a s t o n i s h beastonish beastonish,另外相邻的两部分不能存在包含关系,例如 a t at at a t i d e atide atide 间不能相连。

输入输出格式

输入格式:

输入的第一行为一个单独的整数 n n n ( n ≤ 20 n \le 20 n20) 表示单词数,以下 n n n 行每行有一个单词,输入的最后一行为一个单个字符,表示“龙”开头的字母。你可以假定以此字母开头的“龙”一定存在.

输出格式:

只需输出以此字母开头的最长的“龙”的长度

输入输出样例

输入样例#1:

5
at
touch
cheat
choose
tact
a

输出样例#1:

23
说明
(连成的“龙”为 a t o u c h e a t a c t a c t o u c h o o s e atoucheatactactouchoose atoucheatactactouchoose

NOIp2000提高组第三题


//难度很高 先记下来回头看 
#include
#include
char a[100][100],h[10],d[5000],c[100];
int vis[60],n,maxn=0;
int cmp(char *first,char *second){
	int len1,len2,len,i,j,k;
	len1=strlen(first);
	len2=strlen(second);
	len=len1>len2?len2:len1;
	if(strcmp(first,second)==0){
		i=len1-1;
		j=0;
		while(i0){
			vis[i]=1;
			len1=strlen(d);
			len2=strlen(a[i]);
			for(j=k;jmaxn?len2:maxn;
			strcpy(t,c);
			strcpy(c,a[i]);
			dfs(step+1);
			strcpy(c,t);
			d[len1]='\0';
			vis[i]=0;
		}
	}
}
int main(){
	//freopen("in.txt","r",stdin);
	memset(vis,0,sizeof(vis));
	int len,i,j;
	scanf("%d",&n);
	for(i=1;i<=n;i++){
		scanf("%s",a[i]);
		strcpy(a[n+i],a[i]);
	}
	scanf("%s",h);
	for(i=1;i<=2*n;i++)
	if(a[i][0]=h[0]){
		strcpy(d,a[i]);
		vis[i]=1;
		len=strlen(d);
		if(maxn

总结

当初说回头看,结果大半年都没看233
发现大佬题解写得挺详细的,粘个链接。

你可能感兴趣的:(NOIP,洛谷,dfs)