洛谷P1019 单词接龙

题目描述

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

输入输出格式

输入格式:

 

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

 

输出格式:

 

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

 

 

本题我本来打算在深搜的时候在处理两个单词的相交部分,但发现预处理一下会更好

要注意使用string变量求其长度与char变量求长度有所不同;

string a;  int length=a.size();     //注意此处不是size(a),一开始我在VScode上编译竟没报错,然后提交洛谷评测,成功CE。。。

char a;  int length=strlen(a);

 

 

代码如下:

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<cstdlib>
 6 #include<string> 
 7 using namespace std;
 8 #define maxn 1010
 9 #define ll long long
10 #define IL inline
11 #define clear(a) memset(a,0,sizeof a)
12 IL void read(int &x)
13 {
14     x = 0;int f = 1;char ch = getchar();
15     while(ch<'0'||ch>'9'){if(ch=='-')f = -f;ch = getchar();}
16     while(ch>='0'&&ch<='9'){x = x * 10 + ch - '0', ch = getchar();}x *= f;
17 }
18 
19 int n, ans;
20 char head;
21 int color[maxn], used[maxn];
22 int contact[maxn][maxn];
23 string z[maxn];
24 
25 void solve(int x,int y)
26 {
27     for (int i = 1; i <= min(z[x].size(), z[y].size()) - 1; i++)
28     {
29         int lazy = 0;
30         int pry = 0;
31         for (int j = z[x].size() - i; j <= z[x].size() - 1;j++)
32         {
33             if(z[x][j]==z[y][pry])
34                 pry++;
35             else
36             {
37                 lazy = 1;
38                 break;
39             }
40         }
41         if(lazy)
42             continue;
43         else
44         {
45             contact[x][y] = i;
46             break;
47         }
48     }
49 }
50 
51 void dfs(int id,int length)
52 {
53     ans = max(ans, length);
54     for (int i = 1; i <= n;i++)
55     {
56         if(color[i]>=2)
57             continue;
58         if(contact[id][i])
59         {
60             color[i]++;
61             dfs(i, length+z[i].size()-contact[id][i]);
62             color[i]--;
63         }
64     }
65     ans = max(ans, length);
66 }
67 
68 int main()
69 {
70     read(n);
71     for (int i = 1; i <= n;i++)
72         cin >> z[i];
73     cin >> head;
74     for (int i = 1; i <= n;i++)
75         for (int j = 1; j <= n;j++)
76             solve(i, j);
77     for (int i = 1; i <= n; i++)
78         if (z[i][0] == head)
79         {
80             color[i] = 1;
81             dfs(i, z[i].size());
82             color[i] = 0;
83         }
84     cout << ans;
85     return 0;
86 }

 

转载于:https://www.cnblogs.com/KGW-/p/10367997.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/278333.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【Java】接口(interface)VS抽象类

接口&#xff08;interface&#xff09;可以说成是抽象类的一种特例&#xff0c;接口中的所有方法都必须是抽象的。接口中的方法定义默认为public abstract类型&#xff0c;接口中的成员变量类型默认为public static final。另外&#xff0c;接口和抽象类在方法上有区别&#x…

latex插入gif_如何将照片和GIF插入Google幻灯片

latex插入gifUsing text (and only text) in your Google Slides presentation is a great way to lose the attention of your audience. Inserting photos and animated GIFs can immediately spice things up by emphasizing the important points you make. 在Google幻灯片…

子矩阵

题目描述 给出如下定义&#xff1a; 子矩阵&#xff1a;从一个矩阵当中选取某些行和某些列交叉位置所组成的新矩阵&#xff08;保持行与列的相对顺序&#xff09;被称为原矩阵的一个子矩阵。例如&#xff0c;下面左图中选取第2、4行和第2、4、5列交叉位置的元素得到一个2*3的子…

springboot入门(一)--快速搭建一个springboot框架

原文出处 前言在开始之前先简单介绍一下springboot&#xff0c;springboot作为一个微框架&#xff0c;它本身并不提供Spring框架的核心特性以及扩展功能&#xff0c;只是用于快速、敏捷地开发新一代基于Spring框架的应用程序&#xff0c;总的来说springboot不是为了要替代Sprin…

q-dir 打不开文件_Q-Dir –多窗格文件管理器

q-dir 打不开文件Sometimes when looking through a file manager, it would be nice to have more than a dual-pane view. Now you can manage your files with up to four viewing panes at once with Q-Dir. 有时&#xff0c;在查看文件管理器时&#xff0c;拥有多个双窗格…

用面向对象的方法写敲门砖

一道名为"敲门砖"的面试题: 用面向对象的方法写,点击列表内,子元素的子标签, 来删除子元素 敲门砖考点: 递归(删除标签, 需要找到列表的直属子标签, 需要通过递归层层往上找, parentNode)冒泡(只需为顶级父元素addEventListener绑定事件, 并通过e.target区分子标签, …

windows10加载动画_如何关闭动画并使Windows 10看起来更快

windows10加载动画Windows 10 fades and window animations are pure eye candy, but waiting for them to load can make your PC seem a bit slow. If you’d like an instant response, you can disable Windows 10’s animations for a snappier desktop experience. Windo…

JData大数据竞赛18年赛题-如期而至-用户购买时间预测

年前做的&#xff0c;也是学习别人的作品作为记录 一、赛题 表1&#xff1a;sku基本信息表&#xff08;jdata_sku_basic_info&#xff09; 表2&#xff1a;用户基本信息表&#xff08;jdata_user_basic_info&#xff09; 表3&#xff1a;用户行为表&#xff08;jdata_user_acti…

LNMP架构(二)

2019独角兽企业重金招聘Python工程师标准>>> 一 Nginx安装 1、切换目录 # cd /usr/local/src 2、下载 # wget http://nginx.org/download/nginx-1.12.1.tar.gz 3、解压 # tar xzvf nginx-1.12.1.tar.gz 4、切换到nginx目录下 # cd nginx-1.12.1/ 5、编译 # ./confi…

edge无法上网dns_如何在Microsoft Edge中通过HTTPS启用DNS

edge无法上网dnsMicrosoft will one day enable DNS over HTTPS (DoH) for all Windows applications, but you can enable it in the new version of Microsoft Edge today with a hidden flag. DoH will improve your security and privacy online, but it isn’t yet enable…

UIButton小结

前言 本来没有打算写这篇文章的, 主要是因为在工作中遇到一些同事再用 有UIButton的时候, 有些很基本的,系统API提供的都不知道, 例如 如何让UIButton的文字居上,居左, 居右, 居下对其等一些基本点, 为此我特地写了一下UIButton小结 UIButton回顾 继承关系 NSObject -> UIRe…

Channel Allocation HDU1373

染色问题&#xff1a;相邻不能染同一种颜色 最少需要的颜色的数量最大团点的数量 #include<bits/stdc.h> using namespace std;#define N 27int n; int mp[N][N]; int ans; int alt[N][N]; int Max[N];bool dfs(int cur,int tot)//cur是s1集合的个数 {if(0cur){if(tot>…

satis原理浅析

什么是satis 我们一般是从packagist获取composer包的&#xff0c;但这些都是公开的。那如果我们想创建自己的私有库呢&#xff0c;比如企业就会有这方便的需要&#xff0c;那我们就可以用satis来创建自己的私有库。 Satis 是一个静态的 composer 资源库生成器。它像是一个超轻量…

HDU - 5686-Problem B (递推+高精)

度熊面前有一个全是由1构成的字符串&#xff0c;被称为全1序列。你可以合并任意相邻的两个1&#xff0c;从而形成一个新的序列。对于给定的一个全1序列&#xff0c;请计算根据以上方法&#xff0c;可以构成多少种不同的序列。 Input 这里包括多组测试数据&#xff0c;每组测试数…

c#写字板实现加粗功能_Windows 7中写字板和绘画中的新功能

c#写字板实现加粗功能WordPad and Paint are often overlooked accessories included in all versions of Windows since 95. They are still included in Windows 7 and now have a new look with some enhanced features. Here we will take a look at some of the new impro…

浏览器加载静态资源文件异常解决办法

2019独角兽企业重金招聘Python工程师标准>>> 1 使用chrome浏览器加载静态资源文件(css、js等)异常导致cssh和js文件不生效&#xff0c;具体报错如下: Resource interpreted as Stylesheet but transferred with MIME type text/html 原因应该是网页文档类型不一致导…

POJChallengeRound2 Guideposts 【单位根反演】【快速幂】

题目分析&#xff1a; 这题的目标是求$$ \sum_{i \in [0,n),k \mid i} \binom{n}{i}G^i $$ 这个形式很像单位根反演。 单位根反演一般用于求&#xff1a;$ \sum_{i \in [0,n),k \mid i} \binom{n}{i}f(x)^i $ 推理过程略&#xff0c;实际上也就是交换求和符号的事情。 接着就变…

用Emesene替换Windows Live Messenger

Tired of Windows Live Messenger bloat and wishing that there was a simpler and cleaner replacement that would let you use your live.com and hotmail.com accounts? Look no further, now you can have all that messenger goodness with Emesene! 厌倦了Windows Liv…

python爬虫笔记(七):实战(三)股票数据定向爬虫

目标分析及描述 #CrawBaiduStocksA.py import requests from bs4 import BeautifulSoup import traceback import redef getHTMLText(url):try:r requests.get(url)r.raise_for_status()r.encoding r.apparent_encodingreturn r.textexcept:return ""def getStockL…

myeclipse和maven的clean和build

转&#xff1a; 详解myeclipse和maven的clean和build 2018年04月20日 11:33:34 群星坠 阅读数&#xff1a;3529 版权声明&#xff1a;本文为博主原创文章&#xff0c;未经博主允许不得转载。 https://blog.csdn.net/qq_35603331/article/details/80002723MyEclipse是一个被广为…