题目大意:给一个变量列表和变量的大小关系,输出所有的满足约束的序列。

  构建为有向图,然后就是拓扑排序,使用回溯输出所有的结果。

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <cctype>
  4. #include <map>
  5. #include <algorithm>
  6. #include <vector>
  7. using namespace std;
  8. #define N 26
  9.  
  10. map<char, int> id;
  11. map<int, char> var;
  12. vector<int> AdjList[N], ans;
  13. int n, indegree[];
  14.  
  15. void newNode(char c)
  16. {
  17. if (!id.count(c))
  18. {
  19. int t = id.size();
  20. id[c] = t;
  21. var[t] = c;
  22. }
  23. }
  24.  
  25. void dfs(int cur)
  26. {
  27. if (cur == n)
  28. {
  29. for (int i = ; i < n; i++) printf("%c", var[ans[i]]);
  30. printf("\n");
  31. return;
  32. }
  33. for (int i = ; i < n; i++)
  34. if (indegree[i] == )
  35. {
  36. indegree[i] = -;
  37. ans.push_back(i);
  38. vector<int> vt;
  39. for (int j = ; j < AdjList[i].size(); j++)
  40. {
  41. int v = AdjList[i][j];
  42. vt.push_back(v);
  43. indegree[v]--;
  44. }
  45. dfs(cur+);
  46. for (int j = ; j < vt.size(); j++)
  47. indegree[vt[j]]++;
  48. ans.pop_back();
  49. indegree[i] = ;
  50. }
  51. }
  52.  
  53. int main()
  54. {
  55. #ifdef LOCAL
  56. freopen("in", "r", stdin);
  57. freopen("out", "w", stdout);
  58. #endif
  59. char str[];
  60. bool first = true;
  61. while (gets(str))
  62. {
  63. int len = strlen(str);
  64. id.clear();
  65. var.clear();
  66. vector<char> varList;
  67. for (int i = ; i < len; i++)
  68. if (islower(str[i]))
  69. varList.push_back(str[i]);
  70. sort(varList.begin(), varList.end());
  71. for (int i = ; i < varList.size(); i++) newNode(varList[i]);
  72. n = id.size();
  73. gets(str);
  74. len = strlen(str);
  75. vector<int> rel;
  76. for (int i = ; i < len; i++)
  77. if (islower(str[i]))
  78. rel.push_back(id[str[i]]);
  79. for (int i = ; i < N; i++) indegree[i] = ;
  80. for (int i = ; i < N; i++) AdjList[i].clear();
  81. for (int i = ; i+ < rel.size(); i += )
  82. {
  83. AdjList[rel[i]].push_back(rel[i+]);
  84. indegree[rel[i+]]++;
  85. }
  86. if (first) first = false;
  87. else printf("\n");
  88. dfs();
  89. }
  90. return ;
  91. }

  第一次忘了给变量列表排序,结果WA了一次...

UVa 124 - Following Orders的更多相关文章

  1. POJ1270 Following Orders[拓扑排序所有方案 Kahn]

    Following Orders Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4885   Accepted: 1973 ...

  2. POJ 1270 Following Orders

    Following Orders Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4902   Accepted: 1982 ...

  3. poj1270Following Orders(拓扑排序+dfs回溯)

    题目链接: 啊哈哈.点我点我 题意是: 第一列给出全部的字母数,第二列给出一些先后顺序. 然后按字典序最小的方式输出全部的可能性.. . 思路: 整体来说是拓扑排序.可是又非常多细节要考虑.首先要按字 ...

  4. UVA - 1153 Keep the Customer Satisfied(贪心)

    UVA - 1153 Keep the Customer Satisfied Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: ...

  5. UVa 11729 - Commando War(贪心)

    "Waiting for orders we held in the wood, word from the front never came By evening the sound of ...

  6. uva 10192 Vacation(最长公共子)

    uva 10192 Vacation The Problem You are planning to take some rest and to go out on vacation, but you ...

  7. UVa 10012 - How Big Is It? 堆球问题 全排列+坐标模拟 数据

    题意:给出几个圆的半径,贴着底下排放在一个长方形里面,求出如何摆放能使长方形底下长度最短. 由于球的个数不会超过8, 所以用全排列一个一个计算底下的长度,然后记录最短就行了. 全排列用next_per ...

  8. [uva] 10067 - Playing with Wheels

    10067 - Playing with Wheels 题目页:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Ite ...

  9. UVA 1412 Fund Management (预处理+状压dp)

    状压dp,每个状态可以表示为一个n元组,且上限为8,可以用一个九进制来表示状态.但是这样做用数组开不下,用map离散会T. 而实际上很多九进制数很多都是用不上的.因此类似uva 1601 Mornin ...

随机推荐

  1. 天棋哥哥大战AlphaGo

    天棋哥哥大战AlphaGo Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 20  Solved: 9 [Submit][Status][Web Boa ...

  2. PowerDesigner中逆向工程将数据库中comment赋值到name

    '------------------------------------------------------------ ' '脚本功能: ' PowerDesigner中逆向工程完成后,将数据库中 ...

  3. 第19章 网络通信----UDP程序设计基础

    用户数据报协议(UDP)是网络信息传输的另一种形式. 基于UDP通信的基本模式如下: (1)将数据打包(称为数据包),然后将数据包发往目的地. 发送数据包: 使用DatagramSocket()创建一 ...

  4. win7 64位andriod开发环境搭建

    本文转自:http://www.cfanz.cn/index.php?c=article&a=read&id=65289 最近换了新电脑,装了win7 64位系统,安装了各种开发环境, ...

  5. 各种命令,以及FAQ..持续更新.....

    Linux 篇: CentOs 7 修改主机名 hostnamectl --static set-hostname <host-name> 统计最多的10条记录 awk '{print $ ...

  6. Apache下的FileUtils.listFiles方法简单使用技巧

    一.引言 Apache提供的很多工具方法非常好用,推荐. 今天在使用的过程中使用到了org.apache.commons.io.FileUtils.listFiles方法,本文主要谈谈这个工具方法的用 ...

  7. 提示:ArcGIS version not specified. You must call RuntimeManager.Bind before creating any ArcGIS components.错误

    ArcGIS10,然后就使用VS创建一个简单的AE应用程序,然后拖放一个toolbar.LicenseControl以及MapControl控件. 接着编译应用程序,编译成功. 然后单击F5运行程序, ...

  8. jQuery HighchartsTableHTML表格转Highcharts图表插件

    版权申明jQuery HighchartsTable 由 PMSIpilot 创建,中文使用文档由Highcharts中文网发布本文由Theo.红烧鸡翅膀.Mr.Zhang 翻译整理,版权归Highc ...

  9. Python+Selenium使用Page Object实现页面自动化测试

    Page Object模式是Selenium中的一种测试设计模式,主要是将每一个页面设计为一个Class,其中包含页面中需要测试的元素(按钮,输入框,标题 等),这样在Selenium测试页面中可以通 ...

  10. ssh密钥登录及远程执行命令

    以192.168.1.104作为客户机 以192.168.1.103作为服务器 使用密钥登录 创建密钥对 在SSH客户机创建用户秘钥对 ssh-keygen -t rsa 之后全回车即可 将会在~/. ...