P1019 单词接龙

题目描述

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

输入输出格式

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

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

输入输出样例

输入样例#1: 复制
5
at
touch
cheat
choose
tact
a
输出样例#1: 复制
23
说明

(连成的“龙”为atoucheatactactouchoose)

NOIp2000提高组第三题

#include <iostream>
#include <cstring>
#include <string>
using namespace std;
string a[22];//储存数组 
int flag[22]={0};//标记是否用过 
int n,maxn=0;
string g;
void dfs(int x,string t){



    int i=0,j,k;
    int h=0,l1,l2;

    int e=t.length();

    if(e>maxn){

        maxn=e;

//      cout<<t<<endl;
    }

        //  cout<<"???"<<endl;


            l1=t.length();
            string t2=t;

        for(i=1;i<=n;i++){

            h=0;

            l2=a[i].length();

//          cout<<i<<" "<<a[i]<<endl;


        if(t2==a[3]){


                        int y;

                        }

            if(e==0){

                    if(a[i][0]==g[0]){

                    t=a[i],flag[i]++;

                    dfs(x+1,t);

                    t=t2,flag[i]--;

                }else continue;

            }else{

                if(flag[i]<2){

                    //重合长度

        //          string g,l;//新单词除去重合长度的长度

                    if(l1>=l2) j=l1-l2;
                    else j=0;

                    int p=0;//最后重合位置 

                    for(;j<l1;j++){

                        if(a[i][0]==t[j]){

                            h=1;
                            p=0;
            //              if(i==3) cout<<a[i][0]<<" "<<t<<endl;

                            for(k=1;k<l2&&j+k<l1;k++){


                                if(a[i][k]==t[j+k]) h++,p=k;

                                else h=0;




                                }

                            }


                        }

                    if(a[i][p-h]!=t[l1]) h=0;

                    if(h==l2) continue;

                    else{

                        if(h==0) continue;



                        for(j=h;j<l2;j++){

                             t=t+a[i][j];

                            if(t2==a[3]){

//                              cout<<t<<" "<<j<<endl;

                            }

                        }



                            flag[i]++;

//                          cout<<t<<endl;

                            dfs(x+1,t);

                            t=t2,flag[i]--;


                    }

                    }


                }


            }



    }

using namespace std;
int main()
{

    string t,r="name";

    cin>>n;

    int i;

    for(i=1;i<=n;i++){

        cin>>a[i];

    }

    cin>>g;

//  cout<<"???"<<endl;

    dfs(1,t);

    cout<<maxn<<endl;

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值