Play on Words

UVA10129 Play On Words 题解 problem 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 magneti 阅读详情

题目:

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 Nthat indicates the number of plates (1 <= N <= 100000). Then exactly Nlines 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.

 

题意:

给出一些单词,问是否能组成一个序列,让序列的前一个单词的尾字母与后一个单词的头字母相同;

 

思路:

这道题其实就是问是否存在欧拉通路;

无向图G存在欧拉通路的条件是:G为连通图,并且G只有两个奇度节点或者无奇度节点。

只需判断图是否连通,和奇度节点的数量。

 

代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 100;

int T,n,G[maxn][maxn],c[maxn],in[maxn],out[maxn];
int vis[maxn];

void dfs(int u)
{
    vis[u]=1;
    for (int v=0; v<26; v++)
        if (!vis[v]&&G[u][v])dfs(v);
}

int main()
{
    char s[2000];
    scanf("%d", &T);
    while (T--)
    {
        memset(vis,1,sizeof(vis));
        memset(G,0,sizeof(G));
        memset(in,0,sizeof(in));
        memset(out,0,sizeof(out));
        scanf("%d",&n);
        for (int i=0; i<n; i++)
        {
            scanf("%s",s);
            int len=strlen(s);
            int r=s[0]-'a';
            int t=s[len-1] - 'a';
            G[r][t] = 1;
            in[t]++;
            out[r]++;
            vis[r] = vis[t] = 0;
        }
        int flag=0;
        int p=0,cnt1=0,cnt2=0,cnt3 = 0;
        for (int i=0; i<26; i++)
        {
            if (out[i]==in[i])
                continue;
            if (out[i]==in[i]+1)
            {
                cnt1++;
                p=i;
            }
            else if(in[i]==out[i]+1)
                cnt2++;
            else
                cnt3++;
        }
        if (cnt3>0)
        {
            printf("The door cannot be opened.\n");
            continue;
        }
        if (cnt1==1&&cnt2==1||cnt1==0&&cnt2==0)
            flag=1;
        else
            flag=0;
        dfs(p);
        for (int i=0; i<26; i++)
            if(!vis[i]) flag = 0;
        if (flag) printf("Ordering is possible.\n");
        else
            printf("The door cannot be opened.\n");
    }
    return 0;
}

 

保研考研机试攻略:第一章——从零开始 🧊保研考研机试攻略:第一章——从零开始 阅读详情

相关推荐

【雷达信号处理实战】从原理到代码:脉冲多普勒(PD)处理全流程解析与MATLAB仿真

本文深入解析了脉冲多普勒(PD)雷达信号处理的核心原理与全流程。通过剖析多普勒效应、脉冲压缩与慢时间FFT等关键技术,并结合详细的MATLAB仿真代码,手把手指导如何从信号建模、回波模拟到最终生成距离-多普勒谱,实现运动目标的精确测距与测速,直观展示了相参积累带来的显著性能增益。

pca5navigator的博客 951

Play on Words——玩文字游戏

Problem Description 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 do...

一个和代码打交道の小孩 1130

OCEAN 电路仿真

介绍利用 Cadence OCEAN 进行电路脚本仿真的方法

weixin_43249158的博客 4402

Play on Words(并查集+欧拉路+欧拉回路)

hdu-1116传送门 数据: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 Nt...

嵩韵儿 279

Dify视觉模型节点避坑指南:我用Qwen2.5-VL做OCR识别时踩过的3个坑(及解决方案)

本文分享了使用Dify平台和Qwen2.5-VL视觉模型进行OCR票据识别时遇到的三个常见问题及解决方案,包括文件变量传递、结构化输出和条件分支判断失效。通过实战经验,帮助开发者优化多模态模型在财务自动化中的应用,提升OCR识别精度和效率。

weixin_29284885的博客 401

告别Keil4!手把手教你用Keil C51 V9.61编译51单片机代码(附最新激活方法)

本文详细介绍了Keil C51 V9.61在51单片机开发中的全栈升级指南,包括编译速度提升、调试器性能优化和现代开发支持等核心优势。通过智能安装方案、项目迁移实战和新特性深度应用,帮助开发者告别Keil4,大幅提升开发效率。附有最新激活方法和实用技巧,适用于嵌入式开发工程师。

weixin_26886855的博客 419

Play on Words (判断是否存在欧拉通路)

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 impo...

自渡的博客 1543

离线安装samba与配置(.tar方式安装)

个人记录

小道士写程序 7722

UVA - 10129Play on Words(欧拉路)

UVA - 10129Play on Words Some of the secret doors contain a very interesting word puzzle. The team of archaeologists has to solveit to open that doors. Because there is no other way to open the doors...

weixin_34000916的博客 233

百度智能云训练图像分割模型+python调用模型并分割图像

目录 训练模型阶段 发布图像分割API 调用图像分割API 创建应用后显示以下两个代码 在pycharm中可以调用API mask解码 依赖库pycocotools安装步骤: 依赖库urllib2的安装 源码下载: 最近上课听老师介绍了一个训练图像分割模型的网站,把数据集上传之后做图像标注,然后进行训练就能得到准确率等指标。 训练模型阶段 图像分割:https://ai.baidu.com/easydl/vision/ 图像分割-->创建模型-->上传数据--&gt

icecreamdinner的博客 1388

双重分组进位跳跃进位链

跳跃进位链

qq_62260432的博客 940

HDU 1116 Play on Words

HDU 1116 Play on Words Problem Description 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 wa

long-long-ago 476

Apollo Planning决策规划算法代码详细解析 (1):Scenario选择

本文重点讲解Apollo代码中怎样配置Scenario以及选择当前Scenario,Scenario决策是Apollo规划算法的第一步,本文会对代码进行详细解析,也会梳理整个决策流程,码字不易,喜欢的朋友们麻烦点个关注与赞。 在本文你将学到下面这些内容: 规划器planer的种类; 规划器planer的主要函数及逻辑; 场景管理类ScenarioManager的运行机制; 场景注册方法; 场景决策流程,如何选择当前场景 详细的apollo决策规划代码分析 代码具体过程如下: 1、Scenar

自动驾驶Player的博客 1万+

uva 10129 Play on Words

一样的题目,看poj 1386 Play on Words 即可

weixin_34101229的博客 90

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

UVA Play on Words

题意:按照一个单词的最后一个字母和另一个字母的第一个字母就可以相连,是否可以实现把所有的单词按照这种方式连接起来(第一个单词的首字母可以不与最后一个单词的最后一个字相同)能实现输出Ordering...,不能实现输出The ...... #include #include #include #include #include #include int map

叶孤心的专栏 697

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

Play On Words

Description 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 puzzl

DoriLeigh的专栏 566

hdu play on words

解题思路: 一开始没有搞出来,和正确的思路很接近,想用结构体,存一个单词的第一个字母和最后一个字母。然后把它们连起来,但是在判断首尾相连的时候没有想出来怎么写。而且在输入的问题上,以为一个单词就应该开一个数组,因为最后要输出这些单词,所以要先存起来,但似乎太麻烦了,一般如果你觉得这道题目太麻烦了,那基本都是错的了。因为这也不是模拟题。是有对应的方法的。 后来看到一个大神的解题报告,里面用了欧拉

hnust_疯子 1113

10129 Play on Words

代码: // 题意:输入n个单词,是否可以排成一个序列,使得每个单词的第一个字母和上一个单词的最后一个字母相同 // 算法:把字母看作结点,单词看成有向边,则有解当且仅当图中有欧拉路径。注意要先判连通 #include #include #include using namespace std;   const int maxn = 1000 + 5;   // 并查集(代码摘

gaoxiangnumber1——Gao Xiang's Blog 421

[HDU1116] Play on Words

传送门:Play on Words 把出现的单词的第一个字母与最后一个字母分别视为以其命名的一个顶点,并在两点之间连一条以第一个字母到最后一个字母的有向边,构建一个图,然后判断是否构成欧拉图即可。 #include <bits/stdc++.h> #define ll long long #define INF 0x3f3f3f3f using namespace std; c...

坤斤拷的博客 438
上一篇: 确定比赛名次
下一篇: Wormholes
admin__
博客等级 码龄8年 3粉丝 79原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值