poj 2337 && zoj 1919 欧拉回路+连通性判断
题目要求按字典序排列,而且可能有重边
所以一开始就将数组从大到小排列,那么我将字符串加入链表时就会令小的不断前移,大的被挤到后面
这里有一点问题就是我一开始使用的是qsort:
int cmp(const void *s1 , const void *s2)
{
return strcmp((char*)s1 , (char*)s2)<0;
}
qsort(str , n , sizeof(str[0]) , cmp)
poj一直wa,试了发zoj却过了,可能是编译器原因吧,然后将字符串放入了结构体重新进行排序,poj才给过
寻找欧拉回路的关键代码
void dfs(int u , int id)
{
for(int i=first[u] ; i!=-1 ; i=e[i].next){
if(e[i].flag){
e[i].flag=false;
dfs(e[i].y , e[i].id);
rec[top2++]=e[i].id;
}
}
}
将其逆序输出即可
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define N 1010
int n , in[N] , out[N] , cnt , st , la;
int first[N] , k;
int top1 , top2;
int rec[N]; struct Str{
char str[];
}str[N]; bool cmp(Str s1 , Str s2)
{
return strcmp(s1.str , s2.str)>;
} struct Edge{
int y , next , id;
bool flag;
}e[N*]; Edge stack[N]; void add_edge(int x,int y ,int id)
{
e[k].y=y , e[k].id = id , e[k].flag=true , e[k].next=first[x];
first[x]=k++;
} void dfs(int u , int id)
{
for(int i=first[u] ; i!=- ; i=e[i].next){
if(e[i].flag){
e[i].flag=false;
dfs(e[i].y , e[i].id);
rec[top2++]=e[i].id;
}
}
} void Fleury()
{
top1 = top2 = ;
dfs(st , -);
} int main()
{
// freopen("a.in" , "r" , stdin);
int T;
scanf("%d" , &T);
while(T--)
{
scanf("%d" , &n);
for(int i= ; i<n ; i++) scanf("%s" , str[i].str);
sort(str , str+n , cmp);
// for(int i=0 ; i<n ; i++) printf("%s\n" , str[i]);
memset(first , - , sizeof(first));
memset(in , , sizeof(in));
memset(out , , sizeof(out));
k=;
for(int i= ; i<n ; i++){
int len = strlen(str[i].str);
int a = str[i].str[]-'a' , b = str[i].str[len-]-'a';
add_edge(a , b , i);
in[b]++ , out[a]++;
}
cnt=,st=- , la=-; bool flag=true;
for(int i=;i<;i++){
if(abs(out[i]-in[i])>){
flag=false;
break;
}
if(out[i]!=in[i]){
cnt++;
if(st==- && out[i]==in[i]+) st=i;
else if(la==- && in[i]==out[i]+) la=i;
else{
flag=false;
break;
}
}
}
if(!flag || (cnt!= && cnt!=)){printf("***\n");continue;} if(st<){
for(int i= ; i< ; i++)
if(out[i]){
st = i;
break;
}
}
Fleury();
// cout<<top2<<endl;
if(top2<n){printf("***\n");continue;}
for(int i=top2- ; i> ; i--) printf("%s." , str[rec[i]].str);
printf("%s\n" , str[rec[]].str);
}
return ;
}
poj 2337 && zoj 1919 欧拉回路+连通性判断的更多相关文章
- POJ 2337 Catenyms (欧拉回路)
Catenyms Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8173 Accepted: 2149 Descript ...
- poj 2337(单向欧拉路的判断以及输出)
Catenyms Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11648 Accepted: 3036 Descrip ...
- UVA12188-Inspector's Dilemma(欧拉回路+连通性判断)
Problem UVA12188-Inspector's Dilemma Time Limit: 3000 mSec Problem Description In a country, there a ...
- poj 1300 Door Man 欧拉回路
题目链接:http://poj.org/problem?id=1300 You are a butler in a large mansion. This mansion has so many ro ...
- HDU 1878 欧拉回路(判断欧拉回路)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1878 题目大意:欧拉回路是指不令笔离开纸面,可画过图中每条边仅一次,且可以回到起点的一条回路.现给定一 ...
- POJ 2337 Catenyms (欧拉图)
本文链接http://i.cnblogs.com/EditPosts.aspx?postid=5402042 题意: 给你N个单词,让你把这些单词排成一个序列,使得每个单词的第一个字母和上一个字单词的 ...
- POJ 1562 && ZOJ 1709 Oil Deposits(简单DFS)
题目链接 题意 : 问一个m×n的矩形中,有多少个pocket,如果两块油田相连(上下左右或者对角连着也算),就算一个pocket . 思路 : 写好8个方向搜就可以了,每次找的时候可以先把那个点直接 ...
- poj 1041(字典序输出欧拉回路)
John's trip Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8641 Accepted: 2893 Spe ...
- POJ 3076 / ZOJ 3122 Sudoku(DLX)
Description A Sudoku grid is a 16x16 grid of cells grouped in sixteen 4x4 squares, where some cells ...
随机推荐
- [转]FaceBook ATC 弱网测试工具环境搭建
工具简介 ATC是FaceBook开源的移动网络测试工具Augmented Traffic Control(ATC),能够方便的让我们模拟各种网络环境进行测试. ATC有两个最吸引人的特点: 在手机上 ...
- LN : leetcode 730 Count Different Palindromic Subsequences
lc 730 Count Different Palindromic Subsequences 730 Count Different Palindromic Subsequences Given a ...
- 设计模式(3)-- 原型模式 (clone分析)
原型模式:用原型实例指定创建对象的种类,并且通过拷贝这些原型来创建对象. 在java中有语言级别的支持:clone 在java中使用原型模式是非常简单的事情,实现Cloneable接口,调用Objec ...
- Sql Server 2008R2升级 Sql Server 2012 问题
环境: Windows server 2008 r2 Standard +SqlServer2008R2 内网环境需要升级为SQL server 2012 升级安装时提示版本不支持 网上查询相关问题 ...
- innerHTML的性能问题
看到一遍文章When innerHTML isn’t Fast Enough,反应了innerHTML在操作量大了以后的性能下降的问题. 并且给出了一个replaceHTML的函数 function ...
- (转)Spring4.2.5+Hibernate4.3.11组合开发
http://blog.csdn.net/yerenyuan_pku/article/details/52887573 搭建和配置Spring与Hibernate整合的环境 今天我们来学习Spring ...
- macos openssl 生成rsa证书 -mark
创建私钥 openssl genrsa -out rsa_private_key.pem 1024 创建无密码私钥 openssl pkcs8 -topk8 -inform PEM –nocrypt ...
- 如何在一次请求中通过JS中获取Url中的参数
从A跳转到B,携带参数 例如: /pc/B.jsp?item=123456 B页面在js可以直接用 var item='${param.item}'; 这样就拿到啦 还有一种方法 定义一个函数 f ...
- CAD交互绘制带周长面积的矩形框(网页版)
主要用到函数说明: _DMxDrawX::DrawLine 绘制一个直线.详细说明如下: 参数 说明 DOUBLE dX1 直线的开始点x坐标 DOUBLE dY1 直线的开始点y坐标 DOUBLE ...
- Markdown的安装和语法
步骤: 1.打开webstorm,File-->Setting-->输入plugin-->Install JetBrains plugin-->输入markdown--> ...