UVa10129 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 阅读详情

        题意:输入n个单词,如果单词a的最后一个字母和单词b的第一个字母一样,这两个单词就可以连接起来。问是否可以把所有的单词连接起来。

        思路:欧拉通路。对于有向图,欧拉通路存在有两个必要条件,一是图连通,二是所有顶点的入度和出度相等,或者有两个顶点例外,一个出度比入度大1,另一个入度比出度大1。统计每个字母在单词首尾出现的次数,可以解决条件二,然后根据这一步的结果,找一个起点,对图进行dfs,如果能访问到所有的顶点,说明图连通。


#include <iostream>         
#include <stdio.h>         
#include <cmath>         
#include <algorithm>         
#include <iomanip>         
#include <cstdlib>         
#include <string>         
#include <memory.h>         
#include <vector>         
#include <queue>         
#include <stack>         
#include <map>       
#include <set>       
#include <ctype.h>         
#define INF 1000000010     
#define ll long long     
#define max3(a,b,c) max(a,max(b,c))     
#define MAXN 1000

using namespace std;

char str[1024];

int rudu[26];
int chudu[26];

int g[680][680];
int cnt[680];

inline int hash(char a,char b){
	return (a-'a')*26+b-'a';
}

void dfs(int x){
	cnt[x]--;
	for(int i=0;i<676;i++){
		if( (x%26==i/26)&&cnt[i] ) {
			dfs(i);
		}
	}
}

int main(){	
	int t;
	scanf("%d",&t);
	while(t--){
		memset(rudu,0,sizeof(rudu));
		memset(chudu,0,sizeof(chudu));
		memset(cnt,0,sizeof(cnt));
		
		int n;
		scanf("%d",&n);
		for(int i=1;i<=n;i++){
			scanf("%s",str);
			++rudu[str[0]-'a'];
			++chudu[ str[strlen(str)-1]-'a' ];
			
			++cnt[ hash(str[0],str[strlen(str)-1]) ];
		}
		
		bool ok=false;
		vector<int> test;
		for(int i=0;i<26;i++){
			if( rudu[i]-chudu[i]!=0 ){
				test.push_back(i);
			}
		}
		
		int start;
		if(test.size()==0){
			ok=true;
			for(int i=0;;i++)
				if(cnt[i]){
					start=i;
					break;
				}
		}
		if(test.size()==2&& (rudu[test[0]]-chudu[test[0]]==1) && (chudu[test[1]]-rudu[test[1]]==1) ){
			ok=true;
			for(int i=26*test[0];;i++)
				if(cnt[i]){
					start=i;
					break;
				}
		}
		if(test.size()==2&& (rudu[test[0]]-chudu[test[0]]==-1) && (chudu[test[1]]-rudu[test[1]]==-1) ){
			ok=true;
			for(int i=26*test[1];;i++)
				if(cnt[i]){
					start=i;
					break;
				}
		}
		
		if(ok){
			dfs(start);
			for(int i=0;i<676;i++){
				if(cnt[i])ok=false;
			}
		}
		
		if(ok){
			cout<<"Ordering is possible."<<endl;
		}else{
			cout<<"The door cannot be opened."<<endl;
		}
	}
}


笔记——Oracle19c Oracle阅读笔记 阅读详情

相关推荐

ISO7816协议深度解析-简单易懂协议详解(一)-- 复位,字符帧,及ATR

1. 摘要 IC卡必须支持T=0或T=1的协议,但不是同时支持这两种协议,而终端则必须同时支持T=0和T=1的协议。 • T=0通讯协议是异步半双工字符传输协议; • T=1通讯协议是异步半双工块传输协议; 在ISO7816-3标准中,具体规定了这两种协议:T0和T1。 IC卡所用的协议在TD1中指定,如果在复位应答信息中没有TD1,则表示用T=0的协议进行通讯。在复位应答后,IC卡和终端之间即用IC卡指定的协议进行通讯。 2. 硬件接口 1)VCC:电源输入(A类:5V, B类:3V) 2)GND:地 3

代码打工人的博客 4万+

UVA10129 Play on Words —— 欧拉回路

题目链接:https://vjudge.net/problem/UVA-10129 代码如下: // UVa10129 Play on Words // Rujia Liu // 题意:输入n个单词,是否可以排成一个序列,使得每个单词的第一个字母和上一个单词的最后一个字母相同 // 算法:把字母看作结点,单词看成有向边,则有解当且仅当图中有欧拉路径。注意要先判连通 #include #

A man can be destroyed, but not defeated. 429

骗分导论 CSP、NOIP、蓝桥杯、PAT、USACO等竞赛的算法骗分技巧 轻松省三

问题来了,那么多座桥,炸一座就破坏岛屿的联系,可能性微乎其微(除非特别设计数据)。它们的价值极大,不仅能初步帮你检验程序的对错,而且,如果你不会做这道题,你就可以直接输出样例!在OI考试中最后难的题数据最后的几个,他们往往不会创造新的数据,此时就可以直接输出最后几个数据了。需要运用《骗分导论》第3章中学到的知识,先写出朴素算法,然后造一些数据,可能就会发现规律。有些时候,问题的答案可能很有特点:对于大多数情况,答案是一样的。据说,在NOIP2013中,有人最后一题不会,愤然打了个随机数,结果得了。

星卯教育-信奥教练tony 6405

uva 10129 Play on Words

欧拉图相关问题

最爱她! 403

STM32F407单一定时器输出4路不同频率及占空比的PWM波

STM32F407单一定时器输出4路不同频率及占空比的PWM波

qq_43868701的博客 4919

OpenCV 开闭运算

开运算和闭运算是将腐蚀和膨胀按照一定的次序进行处理。 但这两者并不是可逆的,即先开后闭并不能得到原来的图像。 开运算 开运算是先腐蚀后膨胀,其作用是:分离物体,消除小区域。特点:消除噪点,去除小的干扰块,而不影响原来的图像。 闭运算 闭运算与开运算相反,是先膨胀后腐蚀,作用是消除/“闭合”物体里面的孔洞,特点:可以填充闭合区域。 cv.morphologyEx(img, op, kernel) img: 要处理的图像 op: 处理方式:若进行开运算,则设为cv.MORPH_OPEN,若进行闭

꧁༺ʚvv༒catɞ༻꧂的博客 949

Play on Words UVA-10129

问题描述 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...

liuYinXin的博客 342

S32DS使用完整指南:S32K CAN通信模块配置步骤

深入讲解S32DS使用过程中S32K芯片的CAN通信模块配置步骤,涵盖关键设置与常见问题。掌握s32ds使用技巧,提升开发效率,实现稳定可靠的车载网络通信。

weixin_42592399的博客 981

Play on Words UVa10129

欧拉回路模板题,wa了半天才过就很难受。首先题目给了一个有向图,对于一个有向图的欧拉回路,要求每个节点的入度等于出度或者存在两个点入度出度相差1。然后将图恢复为无向图,使用dfs判断图的联通性。P.S. 学习队列.push(并查集)。。一开始试了好几种写法,所以ac代码有很多历史遗留产物,比如have数组。。#include &lt;iostream&gt; #include &lt;cstdio...

比宇宙更远的地方- 335

uniapp 全局弹窗

功能详解1.有的组件需要全局调用,不想要在一个页面里面一个个引入,现在就需要写入到全局中,直接全局触发。2.通过 $rechargePop就能全局触发 充值的弹窗实现步骤一、先把组件封装好,如 rechargePop组件位置 components/RechargePop/index.vue ,就定义一个methods open方法就可以 二、在同级别路径下定义一个 index.js 三、引入到main.js 四、全局调用this.$rechargePop(); 就能弹出那个弹窗

weixin_42066070的博客 657

全网最详细教程,手把书教你使用YOLOv10训练自己的数据集和推理(附YOLOv10网络结构图)

YOLOv10,由清华大学多媒体智能组只开发,是一款亳秒级实时端到端目标检测的开源模型。该模型在保持性能不变的情况下,与YOLOv9相比,延迟减少了46%,参数减少了25%,非常适合需要快速检测物体的应用,如实时视频分析、自动驾驶和智能医疗等领域。这些改进使得YOLOv10在实时物体检测领域达到了新的技术水平。

在职AI算法工程师,擅长计算机视觉,YOLO目标检测、分割等,擅长web、pyqt界面可视化,好内容持续更新中,来这里跟大家一起学习,共同进步 1万+

uva 10129 Play on Words(欧拉路)

uva 10129 Play on Words Description 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 th

总有那么一群人喜欢盯着我的博客看,估计这是他们见过最长的博客名,我必须得告诉他们博客名最长可以写多长 678

手把手教你 Ubuntu 24.04 离线安装 Ollama

本文介绍 Ubuntu 下离线安装 Ollama 并使用 Ollama 部署 llama3.1-70b 大模型

举世誉之而不加劝,举世非之而不加沮,定乎内外之分,辩乎荣辱之境,斯已矣。 1018

UVA - 10129 Play on Words

Play on Words Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description dfs实现的欧拉回路,需要注意很多判断条件判断是不是回路 #include

MR CODER 348

OV9734 DATASHEET,完整规格书

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

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

uva 10129 Play on Words

uva 10129 Play on Words 欧拉回路,这题是有向图 判断方法是 点出入度都相等或者 有2个点出入度相差1,其他点都相等 思路:有100000个字符串,看似很大,其实可以只存字母的出入度,因为字母只有'a' - 'z'  26个, 只要开26的数组即可,然后判断度数是否符合条件 #include #include int t; int n; ch

Remilia's 1320

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

Play on Words UVA - 10129 欧拉回路

Play on Words UVA - 10129  题目类型: 欧拉道路 题目: 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 ...

Nicolas的博客 481

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

UVA 10129 Play on Words

UVA 10129 Play on Words题目大意:判断首字母与末尾字母形成的若安路是否形成欧拉通路解题思路:判断是否连通,然后判断度数是否为偶数(起点和终点入度和出度可以相差1)#include <stdio.h> #include <iostream> #include <string.h> #include <stack> using namespace std; stack<int> s

ZeroLH00的博客 304

UVa 10129 Play on Words

好久不谢d

kzzhr 937

UVA 10129 - Play on Words (单词) By SuCicada

例题6-16 单词(Play On Words, UVa 10129) 输入n(n≤100000)个单词,是否可以把所有这些单词排成一个序列,使得每个单词的 第一个字母和上一个单词的最后一个字母相同(例如acm、malform、mouse)。每个单词最多包含1000个小写字母。输入中可以有重复单词。 Sample Input 3 2 acm ibm 3 acm malform mouse 2 ok ok Sample Output The door cannot be opened. Ordering i.

SuCicada 小米的蝉 476

uva 10129 - Play on Words

需要了解几个知识:并查集(见《算法竞赛入门经典》第11章。)把26个字母当做节点,每个单词表示一条边。用这样的方式建图,比较入度与出度的大小关系即可。见《算法竞赛入门经典》第六章欧拉回路相关知识。另外,如果读取的字符数组开的不够大,会引起runtime error错误。 #include #include #include const int maxn=1010,maxm=30; char s

LeanCloud-lzwjava 1375

uva 10129 Play on Words

把每个单词的第一个和最后 一个字符作为一条有向边,26个字母作为节点构造有向图,如果该图中存在欧拉回路或者欧拉路,就符合题设要求,反之不符合要求,可以先用并查集判断有向图是不是弱连通的,然后把所有入度等于出度的点取出来,如果取出来的点的个数为0,说明存在欧拉回路,如果取出来的点的个数不等于0也不等于2,肯定没有欧拉路存在了,如果取出来的点的个数等于2,还要判断这两个点的入度和出度,是不是一个入度比

皓首不倦 不负少年 671

UVA - 10129 Play on Words(有向欧拉道路存在性)

题目链接:http://vjudge.net/problem/UVA-10129 题意:给定n个单词,问能否将其按前一个单词的最后一个字母和后一个单词的第一个字母相同的连接方式将其连成一串。 分析:对于单词只考虑第一个字母和最后一个字母即可。将首尾字母看成节点,单词看成边,问题就变成是否存在一条路径恰好将所有的边走一遍,这就是欧拉道路的定义,所以这是一条判断是否是欧拉道路的简单题目。方法可

smwqd_yehua_cx的博客 609

UVa10129 例题 6-16 单词(Play On Words)

原题链接: UVa-10129 题目大意:   输入n(n 解题思路:  可以根据紫书后面提供的思路进行解决。将单词的首尾字母当做点,每个单词当做边。然后这道题就变成了一个图是否存在欧拉通路。要解决欧拉通路问题有两种方法一种是DFS,另一种是利用并查集。我用的是前一种方法(因为并查集我并不会┑( ̄Д  ̄)┍)。  思路也是和紫书上的一样的,先判断底图(把有向图变成无向图)是否

Miracle42的博客 1459

Play on Words(UVA - 10129)

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

不知所云的博客 365
上一篇: UVa10305 Ordering Tasks
下一篇: poj1077 Eight
squee_spoon
博客等级 码龄14年 61粉丝 455原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值