当前位置:网站首页>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
边栏推荐
- Google Earth engine - Classification and processing of UAV images
- Is it safe to open a fund account online now?
- [cute new problem solving] sum of four numbers
- 国科大. 深度学习. 期末试题与简要思路分析
- Preview of authtalk phase I | comprehensive dismantling of multi tenant solutions
- 现在网上办理基金开户,身份证信息安全吗?
- SDL image display
- 最大堆与堆排序和优先队列
- Encrypt Ogg users
- Leetcode 1296. 划分数组为连续数字的集合(已解决)
猜你喜欢

数据填报、报表展示哪家强?亿信ABI给你答案

Deep understanding of transaction isolation levels

状态机练习

SBOM (software bill of materials)

Chang'an chain learning research - storage analysis wal mechanism

How to quickly realize Zadig single sign on on authoring?

session管理

Google Earth Engine——无人机影像进行分类处理

Characteristics of DMA mode

抽象類與派生類
随机推荐
009 面试题 SQL语句各部分的执行顺序
定时任务,vim直接创建修改用户
44、使用OrienMask进行实例分割目标检测,并进行mnn部署和ncnn部署
Chang'an chain learning research - storage analysis wal mechanism
Labview32-bit and 64 bit compatibility
学习记录[email protected]之moveActivityIdTo任务回退特殊案例分析
SBOM(Software Bill of Materials,软件物料清单)
Icml2022 | géométrie multimodale Contrastive Representation Learning
Several points to be analyzed in the domestic fpga/dsp/zynq scheme
kube-proxy & Service & Endpoint
【萌新解题】四数之和
Leetcode 1275. 找出井字棋的获胜者
The first step of agile: turn "iteration" into "sprint" and start!
中断的分类
CSRF protection mechanism
TDesign CompositionAPI 重构之路
实习是步入社会的一道坎
BigScience 开源 Bloom 的自然语言处理模型
国科大.深度学习.期末复习知识点总结笔记
Which company is better in data filling and report presentation? Yixin ABI gives you the answer