码迷,mamicode.com
首页 > 其他好文 > 详细

2019.1.21 单词接龙

时间:2019-01-21 12:12:07      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:word   ring   ssi   style   https   for   cin   cstring   i++   

题目传送门

主要就是简单的字符串+深搜

每次枚举可以接下去的单词 再枚举最后取i位作为后缀

枚举不到单词就取长度

上代码

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int n,len,book[25];
string word[25],fir,ne;
bool judge(string str,string left)//判断是否可以接下去
{
    int l=str.size();
    for(int i=0;i<l;i++)
        if(str[i]!=left[i])return false;
    return true;
}
void dfs(string now,int l)
{
    for(int i=1;i<=n;i++)
    {
        if(judge(now,word[i])&&book[i])
        {
            book[i]--;
            int le=word[i].size();
            int l1=l+le-now.size();//加上单词后的新长度
            for(int j=le-1;j>=1;j--)//枚举前缀
            {
                ne.assign(word[i],j,le-j+1);
                dfs(ne,l1);
            }
            book[i]++;
        }
    }
    len=max(l,len);//取长度
    return;
}
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)book[i]=2;
    for(int i=1;i<=n;i++)cin>>word[i];
    cin>>fir;
    dfs(fir,1);
    printf("%d",len);
    return 0;
}

 

2019.1.21 单词接龙

标签:word   ring   ssi   style   https   for   cin   cstring   i++   

原文地址:https://www.cnblogs.com/qxds/p/10297845.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!