uva 10129 poj 1386 hdu 1116 zoj 2016 play on words
//本来是想练一下欧拉回路的,结果紫书上那题是大水题!!!!!
题意:给出n个单词,是否可以把单词排列成每个单词的第一个字母和上一个单词的最后一个字母相同
解:欧拉通路存在=底图联通+初度!=入度的点至多只有两个(分别为入点和出点)
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<queue>
#include<vector>
#include<map>
#include<stack>
#include<string> using namespace std; int T,n;
int in[],out[];
int f[];
char s[]; int gf(int x){
if (x==f[x]) return f[x];
f[x]=gf(f[x]);
return f[x];
} bool solve(){
memset(in,,sizeof(in));
memset(out,,sizeof(out));
for (int i=;i<;i++){
f[i]=i;
}
scanf("%d",&n);
for (int i=;i<n;i++){
scanf("%s",s);
int f1=gf(s[]-'a');
int f2=gf(s[strlen(s)-]-'a');
if (f1!=f2){
f[f1]=f2;
}
in[s[]-'a']++;
out[s[strlen(s)-]-'a']++;
}
int pre=-;
for (int i=;i<;i++){
if (in[i]!= || out[i]!=){
if (pre==-)
pre=gf(i);
else{
if (pre!=gf(i)) return false;
pre=gf(i);
}
}
}
for (int i=;i<;i++){
in[i]=in[i]-out[i];
}
sort(in,in+);
for (int i=;i<;i++) if (in[i]!=) return false;
if (abs(in[])> || abs(in[])> || in[]+in[]!=) return false;
if (in[]+in[]!=) return false;
return true;
} int main(){
scanf("%d",&T);
for (int cas=;cas<=T;cas++){
if (solve())
printf("Ordering is possible.\n");
else
printf("The door cannot be opened.\n");
}
return ;
}
/*
3
2
acm
ibm
3
acm
malform
mouse
2
ok
ok
*/
uva 10129 poj 1386 hdu 1116 zoj 2016 play on words的更多相关文章
- POJ 1386&&HDU 1116 Play on Words(我以后再也不用cin啦!!!)
Play on Words Some of the secret doors contain a very interesting word puzzle. The team of archaeolo ...
- POJ 1564(HDU 1258 ZOJ 1711) Sum It Up(DFS)
题目链接:http://poj.org/problem?id=1564 题目大意:给定一个整数t,和n个元素组成的集合.求能否用该集合中的元素和表示该整数,如果可以输出所有可行解.1<=n< ...
- POJ 2062 HDU 1528 ZOJ 2223 Card Game Cheater
水题,感觉和田忌赛马差不多 #include<cstdio> #include<cstring> #include<cmath> #include<algor ...
- POJ 2289 Jamie's Contact Groups / UVA 1345 Jamie's Contact Groups / ZOJ 2399 Jamie's Contact Groups / HDU 1699 Jamie's Contact Groups / SCU 1996 Jamie's Contact Groups (二分,二分图匹配)
POJ 2289 Jamie's Contact Groups / UVA 1345 Jamie's Contact Groups / ZOJ 2399 Jamie's Contact Groups ...
- POJ 2104&HDU 2665 Kth number(主席树入门+离散化)
K-th Number Time Limit: 20000MS Memory Limit: 65536K Total Submissions: 50247 Accepted: 17101 Ca ...
- POJ 1386 Play on Words(欧拉图的判断)
Play on Words Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 11838 Accepted: 4048 De ...
- POJ 2296 Map Labeler / ZOJ 2493 Map Labeler / HIT 2369 Map Labeler / UVAlive 2973 Map Labeler(2-sat 二分)
POJ 2296 Map Labeler / ZOJ 2493 Map Labeler / HIT 2369 Map Labeler / UVAlive 2973 Map Labeler(2-sat ...
- poj 1251 poj 1258 hdu 1863 poj 1287 poj 2421 hdu 1233 最小生成树模板题
poj 1251 && hdu 1301 Sample Input 9 //n 结点数A 2 B 12 I 25B 3 C 10 H 40 I 8C 2 D 18 G 55D 1 E ...
- POJ 1860 Currency Exchange / ZOJ 1544 Currency Exchange (最短路径相关,spfa求环)
POJ 1860 Currency Exchange / ZOJ 1544 Currency Exchange (最短路径相关,spfa求环) Description Several currency ...
随机推荐
- javascript 数据结构和算法读书笔记 > 第三章 列表
1. 结构分析 列表首先要有以下几个属性: listSize 长度 pos 当前位置 dataStore 数据 我们要通过以下方法对上面三个属性进行操作: length() 获取长度 | getPos ...
- 通过读取excel数据和mysql数据库数据做对比(一)-win环境准备
要想操作excel和mysql首先需要安装python,然后是安装excel和mysql插件: 第一步安装python: 直接百度搜索,下载安装就可以了. 第二步安装excel插件: 首先到这个htt ...
- linux查看网卡个数及速度
# lspci | grep Ethernet03:00.0 Ethernet controller: Broadcom Corporation NetXtreme II BCM5708 Gigabi ...
- Memcache缓存系统原理
在Web服务开发中,服务端缓存是服务实现中所常常采用的一种提高服务性能的方法.其通过记录某部分计算结果来尝试避免再次执行得到该结果所需要的复杂计算,从而提高了服务的运行效率. 除了能够提高服务的运行效 ...
- 论山寨手机与Android联姻 【4】手机产业链
前文说到,生产手机以前,制造厂家需要预先得到软硬件的产品级设计方案,然后按照设计方案亦步亦趋地做,就可以制造出手机了.软硬件的产品级设计包括以下内容, 1. 主板设计,或者Gerber文件,或者PCB ...
- android4.0 禁止横竖屏切换使用 android:configChanges="orientation|keyboardHidden"无效
android4.0 禁止横竖屏切换使用 android:configChanges="orientation|keyboardHidden"无效 在之前的版本中都是在Man ...
- 网易云课堂_程序设计入门-C语言_第三周:循环_1奇偶个数
1 奇偶个数(5分) 题目内容: 你的程序要读入一系列正整数数据,输入-1表示输入结束,-1本身不是输入的数据.程序输出读到的数据中的奇数和偶数的个数. 输入格式: 一系列正整数,整数的范围是(0,1 ...
- 饿了么移动APP的架构演进(转)
原文:http://www.jianshu.com/p/2141fb0dc62c 文/圣迪(简书作者)原文链接:http://www.jianshu.com/p/2141fb0dc62c著作权归作者所 ...
- 怎样解决Ubuntu发热严重地问题
刚装ubuntu的时候那是相当地热,hot.直接地原因是没有对应地显卡驱动,然后在software update里面找到Nivida地最新驱动,兴高採烈地装上试一试.一点用处没有! 在网上搜了搜,有一 ...
- C# 与 VB.NET 对比
C# 与 VB.NET 对比 2008-06-20 15:30 by Anders Cui, 1462 阅读, 3 评论, 收藏, 编辑 Table of Contents 1.0 Int ...