栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

poj 2337 Catenyms

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

poj 2337 Catenyms

#include <cstdio>#include <cstring>#include <vector>#include <stack>#include <algorithm>using namespace std;struct Node {  int to;  bool vis;  char word[20 + 1];  Node () { to = -1; vis = false; }  bool operator < (const Node &rhs) const {    return strcmp(word, rhs.word) < 0;  }};const int maxn = 26;vector<Node> G[maxn];stack<char*> s;bool used[maxn];int in[maxn], out[maxn], n, p[maxn], start;char input[20 + 3];void init() {  for (int i = 0; i < maxn; i++) {    G[i].clear();    p[i] = i;  }  memset(in, 0, sizeof(in));  memset(out, 0, sizeof(out));  memset(used, 0, sizeof(used));  while (!s.empty()) s.pop();}int find(int x) {  return x == p[x] ? x : p[x] = find(p[x]);}void Union(int x, int y) {  int fx = find(x), fy = find(y);  if (fx != fy) p[fx] = fy;}void add_edge(char* str) {  int ll = strlen(str) - 1;  int from = str[0] - 'a', to = str[ll] - 'a';  in[to]++; out[from]++;  used[from] = used[to] = true;  Node e; e.to = to;  strcpy(e.word, str);  G[from].push_back(e);}bool connected() {  int cnt = 0;  for (int i = 0; i < 26; i++)    if (used[i] && p[i] == i) cnt++;  return cnt == 1;}bool pre_euler() {  int ru = 0, chu = 0;  for (int i = 0; i < 26; i++) {    if (used[i]) {      if (in[i] == out[i]) continue;      if (in[i] - out[i] == 1) ru++;      else if (out[i] - in[i] == 1) {        chu++; start = i;      }      else return false;    }  }  if (!ru && !chu) {    for (int i = 0; i < 26; i++)      if (used[i]) {        start = i; break;      }    return true;  }  else if (ru == 1 && chu == 1) return true;  else return false;}void euler(int u) {  int bound = G[u].size();  for (int i = 0; i < bound; i++) {    Node &e = G[u][i];    int v = e.to;    if (!e.vis) {      e.vis = true;      euler(v);      s.push(e.word);    }  }}int main() {  int kase;  scanf("%d", &kase);  while (kase--) {    scanf("%d", &n);    init();    for (int i = 1; i <= n; i++) {      memset(input, 0, sizeof(input));      scanf("%s", input);      add_edge(input);      int a = input[0] - 'a';      int b = input[strlen(input) - 1] - 'a';      Union(a, b);    }    bool liantong = connected();    if (!liantong) {      printf("***n");      continue;    }    bool cunzai = pre_euler();    if (!cunzai) {      printf("***n");      continue;    }    for (int i = 0; i < 26; i++)      if (used[i]) sort(G[i].begin(), G[i].end());    euler(start);    bool dot = false;    while (!s.empty()) {      if (dot) printf(".");      printf("%s", s.top());      s.pop();      dot = true;    }    putchar('n');  }  return 0;}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/371868.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号