洛谷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.pswp.cn/news/278333.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/278333.shtml
英文地址,請注明出處:http://en.pswp.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是一個被廣為…