题目要求按字典序排列,而且可能有重边

所以一开始就将数组从大到小排列,那么我将字符串加入链表时就会令小的不断前移,大的被挤到后面

这里有一点问题就是我一开始使用的是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 欧拉回路+连通性判断的更多相关文章

  1. POJ 2337 Catenyms (欧拉回路)

    Catenyms Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8173   Accepted: 2149 Descript ...

  2. poj 2337(单向欧拉路的判断以及输出)

    Catenyms Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11648   Accepted: 3036 Descrip ...

  3. UVA12188-Inspector's Dilemma(欧拉回路+连通性判断)

    Problem UVA12188-Inspector's Dilemma Time Limit: 3000 mSec Problem Description In a country, there a ...

  4. poj 1300 Door Man 欧拉回路

    题目链接:http://poj.org/problem?id=1300 You are a butler in a large mansion. This mansion has so many ro ...

  5. HDU 1878 欧拉回路(判断欧拉回路)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1878 题目大意:欧拉回路是指不令笔离开纸面,可画过图中每条边仅一次,且可以回到起点的一条回路.现给定一 ...

  6. POJ 2337 Catenyms (欧拉图)

    本文链接http://i.cnblogs.com/EditPosts.aspx?postid=5402042 题意: 给你N个单词,让你把这些单词排成一个序列,使得每个单词的第一个字母和上一个字单词的 ...

  7. POJ 1562 && ZOJ 1709 Oil Deposits(简单DFS)

    题目链接 题意 : 问一个m×n的矩形中,有多少个pocket,如果两块油田相连(上下左右或者对角连着也算),就算一个pocket . 思路 : 写好8个方向搜就可以了,每次找的时候可以先把那个点直接 ...

  8. poj 1041(字典序输出欧拉回路)

    John's trip Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8641   Accepted: 2893   Spe ...

  9. POJ 3076 / ZOJ 3122 Sudoku(DLX)

    Description A Sudoku grid is a 16x16 grid of cells grouped in sixteen 4x4 squares, where some cells ...

随机推荐

  1. Chrome下font-size小于12px的解决办法

    自从Chorme取消了-webkit-text-size-adjust,这个问题又变得令人烦恼起来. 好在我们可以利用-webkit-transform这个私有属性. .box{ -webkit-tr ...

  2. K-th Number 线段树的区间第K大

    http://poj.org/problem?id=2104 由于这题的时间限制不紧,所以用线段树水一水. 每个节点保存的是一个数组. 就是对应区间排好序的数组. 建树的时间复杂度需要nlogn 然后 ...

  3. RAID基础

    磁盘类型 类型 IDE Integrated Drive Electronics SATA Srial ATA SCSI Small Computer System Interface FC Fibe ...

  4. Hadoop工作流--JobControl(五)

    不多说,直接上干货! 这只是部分,做个引子. 未完,待续!

  5. 微信环境支付宝服务窗环境app手机浏览器pc端混合判断

    //微信环境 if(userAgent.match(/micromessenger/) == 'micromessenger'){ }//支付宝服务窗环境else if(userAgent.match ...

  6. C#中this指针的用法示例

    这篇文章主要介绍了C#中this指针的用法,对初学者而言是非常重要的概念,必须加以熟练掌握,需要的朋友可以参考下. 本文实例展示了C#中this指针的用法,对于初学者进一步牢固掌握C#有很大帮助,具体 ...

  7. canvas基础绘制-绚丽时钟

    效果图: 与canvas基础绘制-绚丽倒计时的代码差异: // var endTime = new Date();//const声明变量,不可修改,必须声明时赋值: // endTime.setTim ...

  8. 【js数据结构】图的深度优先搜索与广度优先搜索

    图类的构建 function Graph(v) {this.vertices = v;this.edges = 0;this.adj = []; for (var i = 0; i < this ...

  9. python-seaborn绘图

    https://zhuanlan.zhihu.com/p/27435863 Seaborn(sns)官方文档学习笔记系列

  10. 配置nginx+tomcat支持websocket

    问题情景:    最近开发新增加一个项目,需要支持https wss协议 访问https://test.aa.com  使用nginx反向代理到后端tomcat web应用 访问https://tes ...