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

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

这里有一点问题就是我一开始使用的是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. 为页面添加favicon

    <link rel="shortcut icon" href="favicon.ico" /> 还有另一种写法,但是IE对它的支持不够好: < ...

  2. markdown快捷键(转)

    markdown快捷键 GLYPH NAME COMMAND Command, Cmd, Clover, (formerly) Apple ALT Option, Opt, (Windows) Alt ...

  3. [BZOJ3527][ZJOI2014]力 FFT+数学

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3527 首先卷积的形式是$h(i)=\sum_{i=0}^jf(i)g(i-j)$,如果我们 ...

  4. mac osx上为qt应用生成debug symbol

    mac平台上,希望Qt编译的release程序也能包含debug symbol,这样出问题以后便于查找问题 开始按照http://doc.qt.io/qt-4.8/mac-differences.ht ...

  5. 将Chrome调试器里的JavaScript变量保存成本地JSON文件

    我写了一个系列的文章,主要用来搜集一些供程序员使用的小工具,小技巧,帮助大家提高工作效率. 推荐一个功能强大的文件搜索工具SearchMyFiles 介绍一个好用的免费流程图和UML绘制软件-Diag ...

  6. SQLite -插入查询

     SQLite -插入查询 SQLite插入语句是用来添加新行数据到数据库中的一个表. 语法: 有两种基本的插入语句的语法如下: INSERT INTO TABLE_NAME (column1, co ...

  7. (转)Spring简介

    http://blog.csdn.net/yerenyuan_pku/article/details/52830571 阿昀以一个初学者的身份学习Spring框架,并按照一个小白的思路详细总结学习过程 ...

  8. 2019_京东JAVA实习生招聘机试第一题

    题意抽象出来就是,求根节点的所有子节点中,以这些子节点为根的子树的最大节点数. 已有向图的方式来保存无向图,所以叶子结点i的eage[i].size()==1. import java.util.Ar ...

  9. 【整理】treeGrid 树形表格

    treeGrid 树形表格 https://fly.layui.com/extend/treeGrid/

  10. Hbase数据库简介

    Hbase是基于Hadoop下分布式存储 数据库,列式存储.(https://www.imooc.com/video/17202) 动态的增加列,不像关系数据库需要提前定义好列. 关系数据库     ...