UVA - 185 Roman Numerals
题目链接:
https://vjudge.net/problem/UVA-185
思路:
剪枝、回溯
注意回溯的时候,是从当前点的下一个开始,而不是从已经遍历的个数点开始!!不然回溯有问题!
思路参考自https://blog.csdn.net/HelloWorld10086/article/details/38778361
1 #include <iostream>
2 #include<cstdio>
3 #include<map>
4 #include<set>
5 #include<cstring>
6 #include<string>
7 using namespace std;
8 map<char,int> mp,mp2;
9 string s1,s2,sum;
10 int vis[2000];
11 int size,count=0;
12 const char letter[]="IXCMVLD";
13 int head[2000];
14
15 int change(string number){
16 int ans=mp[number[number.size()-1]];
17 for(int i=number.size()-2;i>=0;i--){
18 if(mp[number[i]]<mp[number[i+1]])
19 ans-=mp[number[i]];
20 else
21 ans+=mp[number[i]];
22 }
23 return ans;
24 }
25 int judge2(string a,string b,string c){
26 int num1,num2,ans;
27 int tmp=1;
28 for(int i=a.size()-1;i>=0;i--){
29 num1+=mp2[a[i]]*tmp;
30 tmp*=10;
31 }
32 }
33 int trans(string s){
34 int tmp=1,ans=0;
35 for(int i=s.size()-1;i>=0;i--){
36 ans+=tmp*mp2[s[i]];
37 tmp*=10;
38 }
39 return ans;
40 }
41 void dfs(int cur,int cnt){
42 if(cnt==size){
43 if(trans(s1)+trans(s2)==trans(sum)) {
44 count++;
45 }
46 return;
47 }
48 if(count>1) return;
49 for(int i=cur;i<7;i++){
50 if(vis[letter[i]]){
51 for(int j=0;j<=9;j++){
52 if(!vis[j]){//letter[i]<-j
53
54 if(j==0&&head[letter[i]])
55 continue;
56
57 vis[j]=1;
58
59 mp2[letter[i]]=j;//想象成一个数组,不用再回溯
60
61 // dfs(cur+1,cnt+1);
62 dfs(i+1,cnt+1);//不是下一位,不然回溯有问题!!
63
64 vis[j]=0;
65
66 if(count>1)
67 return;
68 }
69 }
70 }
71 }
72 }
73 void init(){
74 mp2.clear();
75 memset(vis,0,sizeof(vis));
76 memset(head,0,sizeof(head));
77 size=0;count=0;
78
79 mp['I']=1;mp['X']=10;mp['C']=100;mp['M']=1000;
80 mp['V']=5;mp['L']=50;mp['D']=500;
81 }
82 int main(int argc, char** argv) {
83 string line;
84 while(cin>>line){
85 if(line[0]=='#') break;
86 init();
87
88 int ind1,ind2;
89 for(int i=0;i<line.size();i++){
90 if(line[i]=='+') {
91 ind1=i;
92 s1=line.substr(0,ind1);
93 }
94 else if(line[i]=='='){
95 ind2=i;
96 s2=line.substr(ind1+1,ind2-ind1-1);
97 }
98 else if(!vis[line[i]]){
99 vis[line[i]]=1;
100 size++;
101 }
102 }
103 sum=line.substr(ind2+1);
104
105 head[s1[0]]=1;head[s2[0]]=1;head[sum[0]]=1;
106
107 if(change(s1)+change(s2)==change(sum)){
108 printf("Correct ");
109 }else{
110 printf("Incorrect ");
111 }
112 dfs(0,0);
113 if(count>1) printf("ambiguous\n");
114 else if(count==1) printf("valid\n");
115 else printf("impossible\n");
116
117 }
118 return 0;
119 }
UVA - 185 Roman Numerals的更多相关文章
- Project Euler 89:Roman numerals 罗马数字
Roman numerals For a number written in Roman numerals to be considered valid there are basic rules w ...
- UVA 185(暴力DFS)
Roman Numerals The original system of writing numbers used by the early Romans was simple but cum ...
- Roman numerals
Roman numerals 罗马数字的题目, 注意几个关键的数字即可: (100, 400, 500, 900) -> ('C', 'CD', 'D', 'CM'); (10, 40, 50, ...
- 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 ...
- CodeForcesGym 100641D Generalized Roman Numerals
Generalized Roman Numerals Time Limit: 5000ms Memory Limit: 262144KB This problem will be judged on ...
- Roman Numerals All In One
Roman Numerals All In One 罗马数字 refs https://www.mathsisfun.com/roman-numerals.html https://www.maths ...
- UVa 130 - Roman Roulette
模拟约瑟夫环 Roman Roulette The historian Flavius Josephus relates how, in the Romano-Jewish conflict o ...
- UVA题目分类
题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics ...
- [LeetCode] Roman to Integer 罗马数字转化成整数
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...
随机推荐
- Greenplum 性能优化之路 --(三)ANALYZE
一.为什么需要 ANALYZE 首先介绍下 RBO 和 CBO,这是数据库引擎在执行 SQL 语句时的2种不同的优化策略. RBO(Rule-Based Optimizer) 基于规则的优化器,就是优 ...
- 2020-2021-1 20209307《Linux内核原理与分析》第一周作业
一.Linux基础命令操作 1.查看目录.新建文件.复制移除文件等 ls[选项] [文件或目录] -a 显示所有文件 包含隐藏文件 -l显示详细信息 -d查看目录属性 pwd显示当前目录 mkdir ...
- css 03-CSS样式表和选择器
03-CSS样式表和选择器 #本文主要内容 CSS概述 CSS和HTML结合的三种方式:行内样式表.内嵌样式表.外部样式表 CSS四种基本选择器:标签选择器.类选择器.ID选择器.通用选择器 CSS几 ...
- Python进阶——什么是上下文管理器?
在 Python 开发中,我们经常会使用到 with 语法块,例如在读写文件时,保证文件描述符的正确关闭,避免资源泄露问题. 你有没有思考过, with 背后是如何实现的?我们常常听到的上下文管理器究 ...
- .NET5 API 网关Ocelot+Consul服务注册
1|0网关介绍 网关其实就是将我们写好的API全部放在一个统一的地址暴露在公网,提供访问的一个入口.在 .NET Core下可以使用Ocelot来帮助我们很方便的接入API 网关.与之类似的库还有Pr ...
- wildfly 21中应用程序的部署
目录 简介 Managed Domain中的部署 管理展开的部署文件 standalone模式下的部署 standalone模式下的自动部署 Marker Files 受管理的和不受管理的部署 部署覆 ...
- Centos7 根目录存储空间扩展方法
Centos7 根目录存储空间扩展方法 一.首先通过 df -hl 命令查看磁盘占用情况,其中根目录已经被占满,此时需要对其进行扩容 二.针对虚拟机环境的centos7系统根存储空间扩容,可利 ...
- springboot 中yml配置
springboot 中各种配置项纪录 1. @Value 最早获取配置文件中的配置的时候,使用的就是这个注解,SpEL表达式语言. // 使用起来很简单 @Value("${config. ...
- java如何实现入职时间到现在 java如何计算知道入职时间, 求工作时长格式为年--月--日。
Date ruZhi = new Date("入职年月bai"); Date now = new Date(); //算出du来时间夸格zhi多长 long shiChang = ...
- web项目报错 无法解析,丢失包 是缺少本地运行jre
1.通过build path 添加add Library 2.添加jre