当前位置:网站首页>A - Play on Words
A - Play on Words
2022-07-17 22:20:00 【zjsru_Beginner】
题目描述:
Some of the secret doors contain a very interesting word puzzle. The team of archaeologists has to solve it to open that doors. Because there is no other way to open the doors, the puzzle is very important for us. There is a large number of magnetic plates on every door. Every plate has one word written on it. The plates must be arranged into a sequence in such a way that every word begins with the same letter as the previous word ends. For example, the word ‘acm’ can be followed by the word ‘motorola’. Your task is to write a computer program that will read the list of words and determine whether it is possible to arrange all of the plates in a sequence (according to the given rule) and consequently to open the door.
Input
The input consists of T test cases. The number of them (T) is given on the first line of the input file.
Each test case begins with a line containing a single integer number N that indicates the number of
plates (1 ≤ N ≤ 100000). Then exactly N lines follow, each containing a single word. Each word
contains at least two and at most 1000 lowercase characters, that means only letters ‘a’ through ‘z’ will
appear in the word. The same word may appear several times in the list.
Output
Your program has to determine whether it is possible to arrange all the plates in a sequence such that
the first letter of each word is equal to the last letter of the previous word. All the plates from the
list must be used, each exactly once. The words mentioned several times must be used that number of
times.
If there exists such an ordering of plates, your program should print the sentence ‘Ordering is
possible.’. Otherwise, output the sentence ‘The door cannot be opened.’
Sample Input
3
2
acm
ibm
3
acm
malform
mouse
2
ok
ok
Sample Output
The door cannot be opened.
Ordering is possible.
The door cannot be opened.
题目大意:
输入若干组单词,如果a单词末尾字母与b单词首字母相同,则a和b可以相接,问这若干组是否可以连成一条线。
思路:
类似于小时候玩的一笔画问题,
我们把输入的每一组单词看成图的一个边,首尾字母看成是一个点,此题即是一道判断欧拉通路的问题了,因为需要判断联通,可以用并查集解决。
AC代码:
#include<iostream>
#include<string.h>
using namespace std;
int enter,n,book[110];
char code[1010];
int dp[110],u[110],v[110];
int find(int x);
void dfs(int x,int y);
int main()
{
cin >> enter;
while(enter --)
{
for(int i = 0; i < 26; i ++)
{
dp[i] = i;
}
cin >> n;
memset(book,0,sizeof(book));
memset(u,0,sizeof(u));
memset(v,0,sizeof(v));
for(int i = 0; i < n; i ++)
{
cin >> code;
int l = strlen(code);
int a = code[0] - 'a';
int b = code[l-1] - 'a';
book[a] = 1;
book[b] = 1;
u[a] ++;
v[b] ++;
dfs(a,b);
}
int a = 0,db = 0;
int flag = 1;
for(int i = 0; i < 26; i ++)
{
if(!flag)
break;
if(!book[i])
continue;
if(u[i] - v[i] > 1 || u[i] - v[i] < -1)
flag = 0;
if(u[i] - v[i] == 1)
{
a ++;
if(a > 1)
{
flag = 0;
}
}
if(u[i] - v[i] == -1)
{
db ++;
if(db > 1)
flag = 0;
}
}
int xx = 0;
for(int i = 0; i < 26; i ++)
if(book[i] && dp[i] == i)
{
xx ++;
if(xx > 1)
{
flag = 0;
break;
}
}
if(!flag)
cout << "The door cannot be opened." << endl;
else
cout << "Ordering is possible." << endl;
}
return 0;
}
int find(int x)
{
if(x == dp[x])
return x;
else
return find(dp[x]);
}
void dfs(int x,int y)
{
int dx = find(x);
int dy = find(y);
if(dx != dy)
dp[dy] = dx;
}
//name:MuaCherish
边栏推荐
- Leetcode 1296. 划分数组为连续数字的集合(已解决)
- ICML2022 | 几何多模态对比表示学习
- 1. Basic concepts of DBMS
- 实习是步入社会的一道坎
- SDL image display
- [mqtt from getting started to improving series | 06] subscribe subscription workflow of mqtt3.1.1
- Authing practice | unified management solution for manufacturing identity authentication
- Is it safe to buy funds in a securities account? I want to make a fixed investment in the fund
- 状态机练习
- Notes on random nodes of force buckle 382 linked list
猜你喜欢

MySQL read / write separation

MongoDB分片集群搭建

High performance pxie data preprocessing board based on kinex ultrascale series FPGA (ku060 +fmc sub card interface)

状态机练习

3U VPX cooling conduction high performance srio/ Ethernet data exchange board

揭开服务网格~Istio Service Mesh神秘的面纱

ospf-LSA

定时任务,vim直接创建修改用户

Preview of authtalk phase I | comprehensive dismantling of multi tenant solutions

Chang'an chain learning research - storage analysis wal mechanism
随机推荐
现场可程式化逻辑闸阵列 FPGA
SQL wrong questions set of Niuke brush questions
MySQL index (I)
Sliding window maximum problem
3438. Number system conversion
国科大. 深度学习. 期末试题与简要思路分析
见鬼,U盘空间怎么少了,原来是EFI分区搞的鬼,删除它
SBOM (software bill of materials)
Labview32-bit and 64 bit compatibility
Zabbix实现对Redis的监控
PKI:TLS握手
Damn it, why is there less space on the USB flash drive? It's the EFI partition. Delete it
Read the paper: temporary graph networks for deep learning on dynamic graphs
Database SQL Server
Pyside2 drawing embedded in Matplotlib
2021.07.13【B站】是这样崩的
Field programmable logic gate array FPGA
PCIe Cameralink signal generator (Cameralink image analog source)
【萌新解题】四数之和
[flask introduction series] exception handling