题目链接:

https://vjudge.net/problem/UVA-185

思路:

剪枝、回溯

注意回溯的时候,是从当前点的下一个开始,而不是从已经遍历的个数点开始!!不然回溯有问题!

思路参考自https://blog.csdn.net/HelloWorld10086/article/details/38778361

  1. 1 #include <iostream>
  2. 2 #include<cstdio>
  3. 3 #include<map>
  4. 4 #include<set>
  5. 5 #include<cstring>
  6. 6 #include<string>
  7. 7 using namespace std;
  8. 8 map<char,int> mp,mp2;
  9. 9 string s1,s2,sum;
  10. 10 int vis[2000];
  11. 11 int size,count=0;
  12. 12 const char letter[]="IXCMVLD";
  13. 13 int head[2000];
  14. 14
  15. 15 int change(string number){
  16. 16 int ans=mp[number[number.size()-1]];
  17. 17 for(int i=number.size()-2;i>=0;i--){
  18. 18 if(mp[number[i]]<mp[number[i+1]])
  19. 19 ans-=mp[number[i]];
  20. 20 else
  21. 21 ans+=mp[number[i]];
  22. 22 }
  23. 23 return ans;
  24. 24 }
  25. 25 int judge2(string a,string b,string c){
  26. 26 int num1,num2,ans;
  27. 27 int tmp=1;
  28. 28 for(int i=a.size()-1;i>=0;i--){
  29. 29 num1+=mp2[a[i]]*tmp;
  30. 30 tmp*=10;
  31. 31 }
  32. 32 }
  33. 33 int trans(string s){
  34. 34 int tmp=1,ans=0;
  35. 35 for(int i=s.size()-1;i>=0;i--){
  36. 36 ans+=tmp*mp2[s[i]];
  37. 37 tmp*=10;
  38. 38 }
  39. 39 return ans;
  40. 40 }
  41. 41 void dfs(int cur,int cnt){
  42. 42 if(cnt==size){
  43. 43 if(trans(s1)+trans(s2)==trans(sum)) {
  44. 44 count++;
  45. 45 }
  46. 46 return;
  47. 47 }
  48. 48 if(count>1) return;
  49. 49 for(int i=cur;i<7;i++){
  50. 50 if(vis[letter[i]]){
  51. 51 for(int j=0;j<=9;j++){
  52. 52 if(!vis[j]){//letter[i]<-j
  53. 53
  54. 54 if(j==0&&head[letter[i]])
  55. 55 continue;
  56. 56
  57. 57 vis[j]=1;
  58. 58
  59. 59 mp2[letter[i]]=j;//想象成一个数组,不用再回溯
  60. 60
  61. 61 // dfs(cur+1,cnt+1);
  62. 62 dfs(i+1,cnt+1);//不是下一位,不然回溯有问题!!
  63. 63
  64. 64 vis[j]=0;
  65. 65
  66. 66 if(count>1)
  67. 67 return;
  68. 68 }
  69. 69 }
  70. 70 }
  71. 71 }
  72. 72 }
  73. 73 void init(){
  74. 74 mp2.clear();
  75. 75 memset(vis,0,sizeof(vis));
  76. 76 memset(head,0,sizeof(head));
  77. 77 size=0;count=0;
  78. 78
  79. 79 mp['I']=1;mp['X']=10;mp['C']=100;mp['M']=1000;
  80. 80 mp['V']=5;mp['L']=50;mp['D']=500;
  81. 81 }
  82. 82 int main(int argc, char** argv) {
  83. 83 string line;
  84. 84 while(cin>>line){
  85. 85 if(line[0]=='#') break;
  86. 86 init();
  87. 87
  88. 88 int ind1,ind2;
  89. 89 for(int i=0;i<line.size();i++){
  90. 90 if(line[i]=='+') {
  91. 91 ind1=i;
  92. 92 s1=line.substr(0,ind1);
  93. 93 }
  94. 94 else if(line[i]=='='){
  95. 95 ind2=i;
  96. 96 s2=line.substr(ind1+1,ind2-ind1-1);
  97. 97 }
  98. 98 else if(!vis[line[i]]){
  99. 99 vis[line[i]]=1;
  100. 100 size++;
  101. 101 }
  102. 102 }
  103. 103 sum=line.substr(ind2+1);
  104. 104
  105. 105 head[s1[0]]=1;head[s2[0]]=1;head[sum[0]]=1;
  106. 106
  107. 107 if(change(s1)+change(s2)==change(sum)){
  108. 108 printf("Correct ");
  109. 109 }else{
  110. 110 printf("Incorrect ");
  111. 111 }
  112. 112 dfs(0,0);
  113. 113 if(count>1) printf("ambiguous\n");
  114. 114 else if(count==1) printf("valid\n");
  115. 115 else printf("impossible\n");
  116. 116
  117. 117 }
  118. 118 return 0;
  119. 119 }

UVA - 185 Roman Numerals的更多相关文章

  1. Project Euler 89:Roman numerals 罗马数字

    Roman numerals For a number written in Roman numerals to be considered valid there are basic rules w ...

  2. UVA 185(暴力DFS)

      Roman Numerals  The original system of writing numbers used by the early Romans was simple but cum ...

  3. Roman numerals

    Roman numerals 罗马数字的题目, 注意几个关键的数字即可: (100, 400, 500, 900) -> ('C', 'CD', 'D', 'CM'); (10, 40, 50, ...

  4. Project Euler:Problem 89 Roman numerals

    For a number written in Roman numerals to be considered valid there are basic rules which must be fo ...

  5. CodeForcesGym 100641D Generalized Roman Numerals

    Generalized Roman Numerals Time Limit: 5000ms Memory Limit: 262144KB This problem will be judged on  ...

  6. Roman Numerals All In One

    Roman Numerals All In One 罗马数字 refs https://www.mathsisfun.com/roman-numerals.html https://www.maths ...

  7. UVa 130 - Roman Roulette

    模拟约瑟夫环  Roman Roulette  The historian Flavius Josephus relates how, in the Romano-Jewish conflict  o ...

  8. UVA题目分类

    题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics ...

  9. [LeetCode] Roman to Integer 罗马数字转化成整数

    Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...

随机推荐

  1. JS怎么把for循环出来的东西放到一个数组里

    var students=[ {name: "vehicleTravelLicenseCopyBack", id: "a1"}, {name: "ve ...

  2. 1.微博回调接口 和绑定user接口

    1.1 oauth/views.py 中添加试图函数 http://192.168.56.100:8888/oauth/weibo/callback/ # 通过vue前端传入的code,微博身份验证c ...

  3. 二、JMeter的图形界面认识

    JMeter的图形界面认识 JMeter是一个工具,应该去认识它,熟悉它,现在的能力还没达到去优化.改造它能力,所以先花时间熟悉它. JMeter的界面主要分为:菜单栏.工具栏.计划树标签栏.内容栏 ...

  4. Swing01-概述

    1.Swing概述 Swing百分之百由Java本身实现,是一套轻量级组件(完全由Java实现的组件叫做轻量级套件,依赖于本地平台的套件称之为重量级套件).Swing不再依赖于平台的GUI,因此真正做 ...

  5. Spark内核-任务调度机制

    作者:十一喵先森 链接:https://juejin.im/post/5e1c414fe51d451cad4111d1 来源:掘金 著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. ...

  6. matplotlib的学习12-Subplot 多合一显示

    import matplotlib.pyplot as plt # matplotlib 是可以组合许多的小图, 放在一张大图里面显示的. 使用到的方法叫作 subplot. plt.figure() ...

  7. C#中更改DataTable列名的三种方法

    解决办法 直接修改列名 dt.Columns["Name"].ColumnName = "ShortName"; sql查询时设置别名 select ID as ...

  8. JVM笔记【1】-- 运行时数据区

    目录 (一)java内存区域管理 1.1 程序计数器 1.2 虚拟机栈 1.3 本地方法栈 1.4 java堆 1.5 方法区 1.5.1 运行时常量池 (二)直接内存 (一)java内存区域管理 C ...

  9. springMVC框架配置定时器

    在springmvc.xml添加如下内容在xmlns中添加 xmlns:task="http://www.springframework.org/schema/task"1在xsi ...

  10. Oracle dd-m月-yy转yyyy-mm-dd

    表名称:TEST_LP 字段:PROD_DATE 1 SELECT '20' || SUBSTR(T.PROD_DATE, INSTR(T.PROD_DATE, '-', 1, 2) + 1, 2) ...