poj1386 Play on Words

【信奥赛一本通】1116:最长平台(详细代码) 【信奥赛一本通】已知一个已经从小到大排序的数组,这个数组的一个平台(Plateau)就是连续的一串值相同的元素,并且这一串元素不能再延伸。例如,在 1,2,2,3,3,3,4,5,5,6中1,2-2,3-3-3,4,5-5,6都是平台。试编写一个程序,接收一个数组,把这个数组最长的平台找出来。在上面的例子中3-3-3就是最长的平台。1.【题目描述】2.【代码】 1.【题目描述】 【题目描述】 已知一个已经从小到大排序的数组,这个数组的一个平台(Plateau)就是连续的一串值相同的元素,并且这一串元素不能再 阅读详情

Play on Words
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 8757 Accepted: 3059

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

Source





欧拉通路+连通图判断,简单题,但是令人匪夷所思的是尼玛居然并查集能敲错……orz

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;

#define MAXN 30

int pnt[MAXN];
int begin[MAXN];
int end[MAXN];

int Find(int t)
{
    if (pnt[t]==t) return t;
    pnt[t]=Find(pnt[t]);
    return pnt[t];
}

void Union(int x,int y)
{
    x=Find(x);
    y=Find(y);
    if (x==y) return;
    pnt[x]=y;
}

int main()
{
    int i,j,n,T;
    char str[1005];
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        for (i=0;i<26;i++)
        {
            pnt[i]=i;
        }
        memset(begin,0,sizeof(begin));
        memset(end,0,sizeof(end));
        for (i=0;i<n;i++)
        {
            scanf("%s",str);
            int first=str[0]-'a';
            int last=str[strlen(str)-1]-'a';
            Union(first,last);
            begin[first]++;
            end[last]++;
        }
        int cnt=0;
        for (i=0;i<MAXN;i++)
        {
          //  printf("%d %d\n",begin[i],end[i]);
            if (begin[i]!=end[i]) cnt++;
            if (abs(begin[i]-end[i])>1) break;
        }
        if ((cnt!=0 && cnt!=2) || i<MAXN)
        {
            printf("The door cannot be opened.\n");
            continue;
        }
        int first=0;
        int p;
        for (i=0;i<MAXN;i++)
        {
            if (begin[i]==0 && end[i]==0) continue;
            if (first==0)
            {
                p=Find(i);
                first=1;
            }
            else
            {
                if (Find(i)!=p) break;
            }
        }
        if (i<MAXN)
        {
            printf("The door cannot be opened.\n");
        }
        else
        {
            printf("Ordering is possible.\n");
        }
    }
    return 0;
}


最长平台(信息学奥赛一本通-T1116) #include<iostream> using namespace std; int main() { int n,x,i,j; int sum=0; int flag=0; int max=0; int len=0; int min=99999; int a[10001],b[10001]; a[0]=-3456497;//保证a[0]与a... 阅读详情

相关推荐

信息学奥赛一本通C++语言——1116:最长平台

【题目描述】 已知一个已经从小到大排序的数组,这个数组的一个平台(Plateau)就是连续的一串值相同的元素,并且这一串元素不能再延伸。例如,在 1,2,2,3,3,3,4,5,5,6中1,2−2,3−3−3,4,5−5,6都是平台。试编写一个程序,接收一个数组,把这个数组最长的平台找出来。在上面的例子中3−3−3就是最长的平台。 【输入】 第一行有一个整数n,为数组元素的个数。第二行有n个整数,...

子航OIer 1951

信息学奥赛一本通:1116:最长平台

试编写一个程序,接收一个数组,把这个数组最长的平台找出来。第一行有一个整数n,为数组元素的个数。第二行有n个整数,整数之间以一个空格分开。if(a[i]==a[i-1])//若当前元素与之前一个元素相同。//保证a[0]与a[1]不相同。)就是连续的一串值相同的元素,并且这一串元素不能再延伸。//平台长度重归为1。已知一个已经从小到大排序的数组,这个数组的一个平台(if(len>max)//记录最大平台值。cin>>a[i];输出最长平台的长度。

ffsdfg的博客 1421

数据仓库实践:从ITR流程讲指标体系建设

从 ITR 流程或者其他客服反馈流程的考核过程可见,实际工作中不可能只通过单个指标评价一个业务过程,需要从多个维度梳理业务,并把多个高度相关的指标组织起来,以避免只考虑其中某个指标从而摧毁整个业务过程的结果,指标体系=指标+体系。

the_singing_towers的博客 2284

信息学奥赛一本通 1116:最长平台 | OpenJudge NOI 1.9 12:最长平台 | 洛谷 B2097 最长平台

【题目链接】 ybt 1116:最长平台 OpenJudge NOI 1.9 12:最长平台 洛谷 B2097 最长平台 【题目考点】 1. 数组中做统计 2. 求最大值 【解题思路】 解法1:遍历并计数 设临时统计长度len(初值为0),最大长度maxLen(初值0),上一个数字lastNum(初值设为与要输入的数字都不同的数字,如-1) 将数据输入整型数组a 遍历数组a 如果当前查看的元素a[i]与上一个数字lastNum相同,那么len增加1 如果当前查看的元素a[i]与上一个数字lastNum不

君义NOIP的博客 7859

GB28181协议实战:5分钟搞定海康摄像头SIP注册失败问题(附Wireshark抓包分析)

本文详细解析了海康摄像头在GB28181协议下SIP注册失败的7个关键原因及解决方案,包括网络可达性、鉴权参数、NAT穿越等核心问题。通过Wireshark抓包分析,提供快速定位方法,帮助工程师高效解决SIP注册失败问题,提升实时图像联调效率。

weixin_29212867的博客 576

网格自适应_Comsol案例解析17:自适应网格的使用

本期导读自适应网格细化有许多优势,比如可以不用对网格进行全局加密,增加计算精度,又会降低求解自由度。本期仍然以气泡上升案例为例,在两相流模块中使用自适应网格。【打开案例模型】【修改网格】适当让气泡边界加密,注意,曲率因子越小,曲线上的网格越密【添加自适应网格】使用默认的参数即可,这里的误差指示器用的是水平集phi。可以根据具体求解模型选择适当的自变量作为误差指示器。【口诀】默认的一般是最...

weixin_28726871的博客 5694

[POJ 1386] Play on Words

[题目链接] http://poj.org/problem?id=1386 [算法] 将每个单词的首字母向尾字母连一条有向边,判断欧拉路径是否存在,即可 [代码] #include <algorithm> #include <bitset> #include <cctype&g...

148

Modbus 通信协议详解

Modbus是由 Modicon(现为施耐德电气公司的一个品牌)在 1979 年发明的,是全球第一个真正用于工业现场的总线协议。ModBus网络是一个工业通信系统,由带智能终端的可编程序控制器和计算机通过公用线路或局部专用线路连接而成。其系统结构既包括硬件、亦包括软件。它可应用于各种数据采集和过程监控。为更好地普及和推动Modbus在基于以太网上的分布式应用,目前施耐德公司已将Modbus协议的所有权移交给 IDA(,分布式自动化接口)组织,并成立了 Modbus-IDA 组织。

Projectsauron 的博客 7万+

POJ 1386 Play On Words 【并查集】

原题地址:点击打开链接 其实本来想做关于欧拉回路的题的,不知道SOJ哪题是欧拉回路的,GOOGLE一下发现说POJ的1386要用欧拉回路。 但是吧……但是吧我怎么做就怎么感觉这题不是欧拉回路。 欧拉通路应该是不重复地经过所有的边1次,即一笔画,欧拉通路和欧拉回路是有点不同的,欧拉通路经过所有的边但不一定(一定不?)回到原点,而欧拉回路则是会回到原点的。 这条题目网上很多人写解答,都是说欧拉

fanfanK的专栏 1342

PPO实战:如何用强化学习让小车爬上MountainCarContinuous-v0山顶(附完整代码)

本文提供了使用PPO算法解决MountainCarContinuous-v0环境的完整实战教程。针对该环境的连续动作空间和稀疏奖励挑战,文章详细解析了PPO智能体的网络架构、状态归一化、GAE优势估计等核心实现,并附带了可直接运行的PyTorch代码。同时分享了关键的调参技巧和实战避坑指南,帮助读者高效训练智能体完成小车爬坡任务。

linux的博客 980

Ubuntu Server 22.04.5系统安装教程

该教程主要是演示在服务器上安装Ubuntu server 22.04系统的详细步骤,主要包含,安装系统前的准备(镜像的下载,启动盘的制作),安装系统时的语言选择,网络设置,切换镜像源(可根据实际需求来配置,本文使用清华镜像源),手动分区,用户设置,ssh服务安装等。以上就是关于Ubuntu Server 22.04系统安装的详细过程。

逆水行舟,不进则退。 7845

Poj1386 Play on Words

原题网址:http://poj.org/problem?id=1386Play on WordsTime Limit: 1000MS Memory Limit: 10000K Total Submissions: 11417 Accepted: 3891 DescriptionSome of the secret doors contain a very interest

MintGreen 507

集成calibre到cadence virtuoso环境中 load: can‘t access file - “/opt/eda/Mentor/lib/calibre.skl“

项目场景: 设想是将Calibre集成到cadence virtuoso中,但网上的解决办法尝试过在本机上实验都有点问题,因此想记录下来我的解决办法仅供参考。 问题描述: 安装好cadence, calibre之后打开virtuoso,CIW窗口显示 *Error* load: can't access file - "/opt/eda/Mentor/lib/calibre.skl" 问题是出在calibre.skl的加载路径出错,尝试网上的在工程目录下修改.cdsinit文件,添加calibre安装

weixin_52714276的博客 1万+

poj1386 Play on words

Play on words 题目背景: poj1386 分析:题意应该比较好理解,重点是怎么实现,我们可以发现,他需要我们求的是一条欧拉路径,并且我们可以发现,其实对于一个单词而言中间的字母是没有用的,只有两头的有用,然后我们的思路就比较清晰了,从每一个单词的首字母向尾字母连边,之后就形成了一个有26个点的有向图,然后我们针对这个图求解是否存在欧拉路径或欧拉回路即可。 Source:

scarlyw的博客 286

Python爬虫实战:利用Playwright与异步技术高效抓取链家十万级二手房数据

在开始编写代码之前,我们首先需要为项目选择合适的技术栈。1. 传统方案:Requests + BeautifulSoup优点: 简单、轻量、学习成本低。直接发送HTTP请求获取HTML,然后解析。缺点: 对于大量由JavaScript动态渲染的内容无能为力。现代网站(包括链家)大量使用JS来加载数据和实现交互,简单的HTTP请求无法获取到完整的页面内容。此外,容易被基于IP或请求头特征的反爬机制拦截。2. 进阶方案:Selenium优点。

2201_76125261的博客 898

RU DOS版、EFI版32bit/64bit BIOS常用工具

DOS版、EFI版的RU工具,可用来查看IO、MEM、PCI、SMBIOS等资源。亲测在EFI SHELL下可用。

POJ 1386 Play on Words(判定图欧拉通路是否存在)

POJ 1386 Play on Words(判定图欧拉通路是否存在) http://poj.org/problem?id=1386 题意:给你多个单词,问你能不能将所有单词组成这样一个序列:序列的前一个单词的尾字母与后一个单词的头字母相同. 分析:把每个单词看成一条有向边,把26个字母看成是图的节点.这就是一个问你有向图是否存在欧拉通路/回路的问题.        有向图存在欧拉路必须满

code 1294

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

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

POJ 1386 Play on Words

欧拉(回)路

sdfzrlt的博客 495

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

POJ1386 Play on Words 欧拉路径

题目链接:http://poj.org/problem?id=1386 题目大意:给你n个单词,判断把这些单词是否能全部连接起来。(当一个单词的首字母等于上个单词的尾字母时两个单词才能连接)。 分析:对于每一个单词,我们只需要纪录下它的首字母和尾字母即可,然后单词接龙就变为了我们熟知的欧拉路径问题了。一个有向图存在欧拉路径,当且仅当该图是连通的,且所有顶点的度数之和为0,或仅存

AC_Gibson的专栏 997

POJ1386 Play on Words

Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Description Some of the secret doors contain a very interesting word puzzle. The team ...

dezhen7015的博客 181

poj Play on Words(1386)

Play on Words Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 9496   Accepted: 3284 Description Some of the secret doors contain a very interesting word pu

码码要洗手 703

【POJ1386】Play on Words

    先附题点击打开链接算法:    单词建图+欧拉路径判断AC程序:#include&lt;iostream&gt; #include&lt;cstdio&gt; #include&lt;cstring&gt; using namespace std; int degree[30]; int father[30];int ran[30]; bool hashs[30]; void inite(...

ZGS_WZY的博客 287

Play on Words POJ - 1386

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

Qtt110的博客 236

POJ - 1386 Play on Words

我真的是呵呵这道题了 //第二次更新,发现我是sb,并查集写错了,哈哈哈哈哈,之前能过真是奇迹 void unionn(int x,int y) { int fx=find(x); int fy=find(y); if(fx!=fy) f[fy]=fx; } 欧拉通路,有向图,并查集+判断入度出度(只能存在一对abs(入度-出度)==1)的,其他的都是...

swust5120171204的博客 188

poj 1386 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 puzz

zyq_19960204的博客 718
上一篇: poj1410 Intersection
下一篇: poj1329 Circle Through Three Points
magicnumber
博客等级 码龄18年 119粉丝 196原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值
⌂ 换网址