P1019 单词接龙

题目链接

不会做看题解一步一步敲,最后竟然还是wa,原来是有一步自己改动给改错了。。
这题是对字符串不会操作,+不会找关系。

#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int n,vis[30]={0},yc[30][30]={0};
string tr[30];
int mt(int x,int y)
{
	bool pp;
	int ky;
	for(int k=tr[x].size()-1;k>=0;k--)
	{
		pp=true;ky=0;
		for(int kx=k;kx<tr[x].size();kx++)
		{
			if(tr[x][kx]!=tr[y][ky++])
			{
				pp=false;
				break;
			}
		}
		if(pp==true)
		{
			return tr[x].size()-k;
		}
	}
	return 0;
}
char ch;
int ans=-1;
int an=0;
void dfs(int p)
{
	bool jx=false;
	for(int j=1;j<=n;j++)
	{
		if(vis[j]>=2) continue;
		if(!yc[p][j]) continue;
		if(yc[p][j]==tr[p].size()||yc[p][j]==tr[j].size()) continue;
		an+=tr[j].size()-yc[p][j];
		vis[j]++;
		jx=true;
		dfs(j);
		an-=tr[j].size()-yc[p][j];
		vis[j]--;
	}
	if(jx==false) ans=max(ans,an);
	return ;
}
int main()
{
	cin>>n;
	for(int i=1;i<=n;i++) cin>>tr[i];
	cin>>ch;
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=n;j++)
		{
			yc[i][j]=mt(i,j);
		}
	}
	for(int i=1;i<=n;i++)
	{
		if(tr[i][0]==ch)
		{
			vis[i]++;
			an=tr[i].size();
			dfs(i);
			vis[i]=0;
		}
	}
	cout<<ans;
	return 0;
 } 
题目描述 给出一组单词,按照以下规则进行单词接龙: 1.首先任意选定一个单词作为开头; 2.接下来每次接的单词必须满足:它的首字母与上一个单词的尾字母相同; 3.同一个单词不能重复使用; 4.当无法继续接下去时,游戏结束。 你的任务是编一个程序,计算出在给定单词集合中,最长的单词接龙长度。 输入格式 输入文件的第一行为一个整数n,表示单词的个数。 接下来n行,每行一个单词,由小字母组成,长度不超过20个字符。 输出格式 输出文件仅一行,包含一个整数,表示最长的单词接龙长度。 数据范围 1≤n≤500 输入样例 5 ab bc cd de ef 输出样例 5 提示 输入样例2: 6 a b c d e f 输出样例2: 1 输入样例3: 4 aa bb cc dd 输出样例3: 2 输入样例4: 4 a b ba ab 输出样例4: 2 输入样例5: 3 a aa aaa 输出样例5: 1 输入样例6: 3 a aa ab 输出样例6: 2 输入样例7: 4 a aa aaa aaaa 输出样例7: 1 输入样例8: 4 a aa aaa baaa 输出样例8: 2 输入样例9: 4 a aa baa ab 输出样例9: 2 输入样例10: 4 a aa baa ba 输出样例10: 2 输入样例11: 4 a aa baa bb 输出样例11: 2 输入样例12: 4 a aa baa bc 输出样例12: 2 输入样例13: 4 a aa baa bca 输出样例13: 2 输入样例14: 4 a aa baa bcb 输出样例14: 2 输入样例15: 4 a aa baa bcc 输出样例15: 2 输入样例16: 4 a aa baa bcdc 输出样例16: 2 输入样例17: 4 a aa baa bcde 输出样例17: 2 输入样例18: 4 a aa baa bcdef 输出样例18: 2 输入样例19: 4 a aa baa bcdefg 输出样例19: 2 输入样例20: 4 a aa baa bcdefgh 输出样例20: 2 输入样例21: 4 a aa baa bcdefghi 输出样例21: 2 输入样例22: 4 a aa baa bcdefghij 输出样例22: 2 输入样例23: 4 a aa baa bcdefghijk 输出样例23: 2 输入样例24: 4 a aa baa bcdefghijkl 输出样例24: 2 输入样例25: 4 a aa baa bcdefghijklm 输出样例25: 2 输入样例26: 4 a aa baa bcdefghijklmn 输出样例26: 2 输入样例27: 4 a aa baa bcdefghijklmno 输出样例27: 2 输入样例28: 4 a aa baa bcdefghijklmnop 输出样例28: 2 输入样例29: 4 a aa baa bcdefghijklmnopq 输出样例29: 2 输入样例30: 4 a aa baa bcdefghijklmnopqr 输出样例30: 2 输入样例31: 4 a aa baa bcdefghijklmnopqrs 输出样例31: 2 输入样例32: 4 a aa baa bcdefghijklmnopqrst 输出样例32: 2 输入样例33: 4 a aa baa bcdefghijklmnopqrstu 输出样例33: 2 输入样例34: 4 a aa baa bcdefghijklmnopqrstuv 输出样例34: 2 输入样例35: 4 a aa baa bcdefghijklmnopqrstuvw 输出样例35: 2 输入样例36: 4 a aa baa bcdefghijklmnopqrstuvwx 输出样例36: 2 输入样例37: 4 a aa baa bcdefghijklmnopqrstuvwxy 输出样例37: 2 输入样例38: 4 a aa baa bcdefghijklmnopqrstuvwxyz 输出样例38: 2 输入样例39: 4 a aa baa bcdefghijklmnopqrstuvwxyzA 输出样例39: 2 输入样例40: 4 a aa baa bcdefghijklmnopqrstuvwxyzAB 输出样例40: 2 输入样例41: 4 a aa baa bcdefghijklmnopqrstuvwxyzABC 输出样例41: 2 输入样例42: 4 a aa baa bcdefghijklmnopqrstuvwxyzABCD 输出样例42: 2 输入样例43: 4 a aa baa bcdefghijklmnopqrstuvwxyzABCDE 输出样例43: 2 输入样例44: 4 a aa baa bcdefghijklmnopqrstuvwxyzABCDEF 输出样例44: 2 输入样例45: 4 a aa baa bcdefghijklmnopqrstuvwxyzABCDEFG 输出样例45: 2 输入样例46: 4 a aa baa bcdefghijklmnopqrstuvwxyzABCDEFGH 输出样例46: 2 输入样例47: 4 a aa baa bcdefghijklmnopqrstuvwxyzABCDEFGHI 输出样例47: 2 输入样例48: 4 a aa baa bcdefghijklmnopqrstuvwxyzABCDEFGHIJ 输出样例48: 2 输入样例49: 4 a aa baa bcdefghijklmnopqrstuvwxyzABCDEFGHIJK 输出样例49: 2 输入样例50: 4 a aa baa bcdefghijklmnopqrstuvwxyzABCDEFGHIJKL 输出样例50: 2 输入样例51: 4 a aa baa bcdefghijklmnopqrstuvwxyzABCDEFGHIJKLM 输出样例51: 2 输入样例52: 4 a aa baa bcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMN 输出样例52: 2 输入样例53: 4 a aa baa bcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNO 输出样例53: 2 输入样例54: 4 a aa baa bcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP 输出样例54: 2 输入样例55: 4 a aa baa bcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQ 输出样例55: 2 输入样例56: 4 a aa baa bcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQR 输出样例56: 2 输入样例57: 4 a aa baa bcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRS 输出样例57: 2 输入样例58: 4 a aa baa bcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRST 输出样例58: 2 输入样例59: 4 a aa baa bcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTU 输出样例59: 2 输入样例60: 4 a aa baa bcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUV 输出样例60: 2 输入样例61: 4 a aa baa bcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVW 输出样例61: 2 输入样例62: 4 a aa baa bcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWX 输出样例62: 2 输入样例63: 4 a aa baa bcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXY 输出样例63: 2 输入样例64: 4 a aa baa bcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ 输出样例64: 2 输入样例65: 4 a aa baa bcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZA 输出样例65: 2 输入样例66: 4 a aa baa bcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZAB 输出样例66: 2 输入样例67: 4 a aa baa bcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZABC 输出样例67: 2 输入样例68: 4 a aa baa bcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZABCD 输出样例68: 2 输入样例69: 4 a aa baa bcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZABCDE 输出样例69: 2 输入样例70: 4 a aa baa bcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZABCDEF 输出样例70: 2 输入样例71: 4 a aa baa bcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFG 输出样例71: 2 输入样例72: 4 a aa baa bcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGH 输出样例72: 2 输入样例73: 4 a aa baa bcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHI 输出样例73: 2 输入样例74: 4 a aa baa bcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJ 输出样例74: 2 输入样例75: 4 a aa baa bcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJK 输出样例75: 2 输入样例76: 4 a aa baa bcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKL 输出样例76: 2 输入样例77: 4 a aa baa bcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLM 输出样例77: 2 输入样例78: 4 a aa baa bcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMN 输出样例78: 2 输入样例79: 4 a aa baa bcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNO 输出样例79: 2 输入样例80: 4 a aa baa bcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOP 输出样例80: 2 输入样例81: 4 a aa baa bcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQ 输出样例81: 2 输入样例82: 4 a aa baa bcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQR 输出样例82: 2 输入样例83: 4 a aa baa bcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRS 输出样例83: 2 输入样例84: 4 a aa baa bcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRST 输出样例84: 2 输入样例85: 4 a aa baa bcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTU 输出样例85: 2 输入样例86: 4 a aa baa bcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUV 输出样例86: 2 输入样例87: 4 a aa baa bcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVW 输出样例87: 2 输入样例88: 4 a aa baa bcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWX 输出样例88: 2 输入样例89: 4 a aa baa bcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXY 输出样例89: 2 输入样例90: 4
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值