洛谷P1019 单词接龙

链接:https://www.luogu.org/problemnew/show/P1019

题意:给你n个字符串,如果一个字符串的后缀是另一个字符串的前缀,那么这个字符串就可以与另一个字符串接龙,但是不能是前面一个串是后面那个串的字串,或者是后面的那个串是前面串的字串。如果一个串的前缀也是它的后缀,那么自己跟自己也可以接龙,但是每个串在最终接龙后的传中最对只能出现两次。最后询问最长接龙的长度。
思路:直接n^2暴力预处理串与串之间的关系,然后dfs搜索。

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

int p[25][25];//表示串x后面接串y在哪个位置
char s[25][100];
int vis[25];//判重,如果多余两次就不行了
int n;
int ans;
void dfs(int x,int num)
{
    ans=max(ans,num);
    for(int i=1;i<=n;i++)
    {
        if(vis[i]==2||(p[x][i]==0&&x!=0))continue;
        vis[i]++;
        dfs(i,num+strlen(s[i])-(strlen(s[x])-p[x][i]));
        vis[i]--;
    }
}

int main()
{

    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        scanf("%s",*(s+i));
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            int len=strlen(*(s+i));
            for(int k=len-1;k>=1;k--)//前面的串从后面往前
            {
                if(len-k>(int)strlen(s[j]))break;//如果找的长度比后面的串要长了,就退出。
                bool ff=false;
                if(s[i][k]==s[j][0])//如果有一个位置相同,那么就找后面那些部分是不是全部相同
                {
                    ff=false;
                    for(int l=k+1;l<len;l++)
                    {
                        if(s[i][l]!=s[j][l-k])
                        {
                            ff=true;
                            break;//求得是最长的,所以公共部分要尽可能短,第一次找到符合条件的位置就可以了
                        }
                    }
                    if(!ff)
                    {
                        p[i][j]=k;
                        break;
                    }
                }
            }
        }
    }
    ans=0;
    char sx[2];
    scanf("%s",sx);
    for(int i=1;i<=n;i++)
        if(s[i][0]==sx[0])vis[i]++,dfs(i,strlen(s[i])),vis[i]--;
    printf("%d\n",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、付费专栏及课程。

余额充值