UVA 185(暴力DFS)
| Roman Numerals |
The original system of writing numbers used by the early Romans was simple but cumbersome. Various letters were used to represent important numbers, and these were then strung together to represent other numbers with the values decreasing monotonically from left to right. The letters they used and the numbers that were represented are given in the following table.
| I | 1 | V | 5 | |
| X | 10 | L | 50 | |
| C | 100 | D | 500 | |
| M | 1000 |
Thus 1993 was written as MDCCCCLXXXXIII. This system was then superseded by a partially place-oriented system, whereby if the above rule of decreasing values was broken, it meant that the immediately preceding (lower) value was deemed to be `negative' and was subtracted from the higher (out of place) value. In this system 1993 was usually written as MCMXCIII. There is still some controversy as to which letters could precede which other letters, but for the purposes of this problem we will assume the following restrictions:
- 1.
- A letter from the left column can never appear more than three times in a row, and there can never be more than one other occurrence of that letter.
- 2.
- A letter from the right column can never appear more than once.
- 3.
- Once a letter has been used in a `negative' position, all subsequent characters (apart from the one immediately following) may not be greater than that character.
Thus we could write MXMIII for 1993 or CCXCIV for 294, however we could not write ILV for 54, nor could we write LIL for 99. Note that 299 could be written as CCXCIX or CCIC
Given a Roman sum, we can either interpret it as such or as an encoding of an Arabic sum. Thus V+V=X could be interpreted as an ambiguous encoding of an Arabic sum with V
{1, 2, 3, 4} and X = 2 * V. Similarly, X+X=XX could be interpreted as a correct Roman sum but an impossible Arabic encoding (apart from the trivial encoding X = 0) and XX+XX=MXC as an incorrect Roman sum, but a valid encoding with M = 1, X = 9, and C = 8.
Write a program that will read in sums in Roman numerals and determine whether or not they are correct as Roman sums and also whether they are impossible, ambiguous or valid as Arabic encodings. Assume that zero will never appear on its own or as a leading digit, and that no two Roman numerals map onto the same Arabic digit.
Input
Input will consist of a series of lines, each line consisting of an apparent Roman sum, i.e. a valid Roman number, a plus sign (
+
), another valid Roman number, an equal sign (
=
) and another valid Roman number. No Roman number will contain more than 9 letters. The file will be terminated by a line consisting of a single
#
.
Output
Output will consist of a series of lines, one for each line of the input, and each containing two words. The first word will be one of (
Correct, Incorrect
) depending on whether the Roman sum is or is not correct. The second word will be separated from the first by exactly one space and will be one of the set (impossible, ambiguous, valid) depending on the Arabic sum.
Sample input
V+V=X
X+X=XX
XX+XX=MXC
#
Sample output
Correct ambiguous
Correct impossible
Incorrect valid
题意:分两步,第一步判断输入的罗马数字运算结果对不对。第二步,如果每个字母可以用0-9代替,且不同字母不能重复,且有前导零不考虑。判断有一种还是多种还是没有能使得运算结果正确的答案。
思路:
#include <stdio.h>
#include <string.h> char c[105];
int v[777];
int vv[777];
int vis[777];
int visn[10];
int vvv[10];
int vvvn;
int judge2;
char num[3][11];
void tra() {
judge2 = 0;
vvvn = 0;
memset(vv, 0, sizeof(vv));
memset(vvv, 0, sizeof(vvv));
memset(vis, 0, sizeof(vis));
memset(visn, 0, sizeof(visn));
int nu = 0;
int nul = 0;
memset(num, 0, sizeof(num));
for (int i = 0 ; i <= strlen(c); i ++) {
if (c[i] == '+' || c[i] == '=' || c[i] == '\0') {
num[nul ++][nu] = '\0';
nu = 0;
continue;
}
if (vis[c[i]] == 0)
{
vis[c[i]] = 1;
vvv[vvvn ++] = c[i];
}
num[nul][nu ++] = c[i];
}
}
int judge1() {
int numm[3];
numm[0] = numm[1] = numm[2] = 0;
for (int k = 0; k < 3; k ++) {
for (int i = 0; i < strlen(num[k]); i ++) {
if (v[num[k][i + 1]] <= v[num[k][i]] || i == strlen(num[k]) - 1)
numm[k] += v[num[k][i]];
else
numm[k] -= v[num[k][i]];
}
}
if (numm[0] + numm[1] == numm[2])
return 1;
else
return 0;
} void dfs(int nn)
{
if (judge2 == 2)
return;
if (nn == vvvn) {
int numm[3];
numm[0] = numm[1] = numm[2] = 0;
for (int k = 0; k < 3; k ++) {
for (int j = 0; j < strlen(num[k]); j ++) {
numm[k] = numm[k] * 10 + vv[num[k][j]];
}
}
if (numm[0] + numm[1] == numm[2]) {
judge2 ++;
}
return;
}
for (int i = 0; i <= 9; i ++) {
if (!visn[i]) {
vv[vvv[nn]] = i;
int bo = 0;
if (!vv[vvv[nn]]) {
for (int j = 0; j < 3; j ++) {
if (num[j][0] == vvv[nn] && strlen(num[j]) > 1) {
bo = 1;
break;
}
}
}
if (bo)
continue;
visn[i] = 1;
dfs(nn + 1);
visn[i] = 0;
}
}
}
int main()
{
v['I'] = 1; v['V'] = 5; v['X'] = 10; v['L'] = 50;
v['C'] = 100; v['D'] = 500; v['M'] = 1000;
while (gets(c) != NULL && c[0] != '#') {
tra();
dfs(0);
if (judge1())
printf("Correct ");
else
printf("Incorrect ");
if (judge2 == 0)
printf("impossible\n");
if (judge2 == 1)
printf("valid\n");
if (judge2 == 2)
printf("ambiguous\n");
}
return 0;
}
第一步好处理。。把每个罗马数字转换为数字比较即可
第二步就是暴力。每个字母0-9暴力,如果遇到两种情况就可以直接结束。
UVA 185(暴力DFS)的更多相关文章
- hihoCoder 1185 连通性·三(Tarjan缩点+暴力DFS)
#1185 : 连通性·三 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 暑假到了!!小Hi和小Ho为了体验生活,来到了住在大草原的约翰家.今天一大早,约翰因为有事要出 ...
- Strange Country II 暴力dfs
这题点的个数(<=50)有限, 所以可以纯暴力DFS去搜索 //#pragma comment(linker, "/STACK:16777216") //for c++ Co ...
- UVA129 暴力dfs,有许多值得学习的代码
紫书195 题目大意:给一个困难的串,困难的串的定义就是里面没有重复的串. 思路:不需要重新对之前的串进行判重,只需要对当前的加入的字符进行改变即可. 因为是判断字典序第k个的字符串,所以要多一个全局 ...
- 2018杭电多校第五场1002(暴力DFS【数位】,剪枝)
//never use translation#include<bits/stdc++.h>using namespace std;int k;char a[20];//储存每个数的数值i ...
- A. The Fault in Our Cubes 暴力dfs
http://codeforces.com/gym/101257/problem/A 把它固定在(0,0, 0)到(2, 2, 2)上,每次都暴力dfs检查,不会超时的,因为规定在这个空间上,一不行, ...
- UVa 818Cutting Chains (暴力dfs+位运算+二进制法)
题意:有 n 个圆环,其中有一些已经扣在一起了,现在要打开尽量少的环,使所有的环可以组成一条链. 析:刚开始看的时候,确实是不会啊....现在有点思路,但是还是差一点,方法也不够好,最后还是参考了网上 ...
- UVA 818 Cutting Chains 切断圆环链 (暴力dfs)
题意就是给一张无向图,去掉某些结点,然后连成一条链,问最少去掉几个结点. n很小n<=15,所以直接枚举2^15个状态就行啦. 链的条件是1.无环,2.没有度大于2的点,3.把n个散链连起来需要 ...
- UVA 11754 (暴力+中国剩余定理)
题目链接: http://www.bnuoj.com/v3/problem_show.php?pid=20172 题目大意:有C个模方程,每个方程可能有k余数,求最小的S个解. 解题思路: 看见模方程 ...
- uva 211(dfs)
211 - The Domino Effect Time limit: 3.000 seconds A standard set of Double Six dominoes contains 28 ...
随机推荐
- [codility]Grocery-store
http://codility.com/demo/take-sample-test/hydrogenium2013 用Dijkstra求最短路径,同时和D[i]比较判断是不是能到.用了优先队列优化,复 ...
- Date简介
Date类 在JDK1.0中,Date类是唯一的一个代表时间的类,但是由于Date类不便于实现国际化,所以从JDK1.1版本开始,推荐使用Calendar类进行时间和日期处理.这里简单介绍一下Date ...
- 学习笔记-[Maven实战]-第三章:Maven使用入门(1)
说明:[Maven实战]一书还介绍了怎么样手工创建Maven工程,学习这本书是为了能尽快在工作中使用,就忽略了手工建工程的部分 如果想了解这部分的内容,可以自己看看书 开始: 1.新建一个maven工 ...
- IIS UrlWriter配置(asp.net)
前提在建虚拟目录或网站时注意以下设置第一步:下载URLRewriter 添加URLRewriter和ActionlessForm(不添加只能在VS实现,IIS下会找不到页面). 第二步:配置web.c ...
- c#基础这些你都看过吗?(一)-----仅供初学者使用
1.注释(不写注释是流氓,名字瞎起是扯淡)‘///’一般用于注释函数,注释类.2.快捷键ctrl+k+d(有语法错误无法进行对齐)ctrl+j(快速弹出只能提示)shift+end,shift+hom ...
- isIOS9
function isIOS9() { //获取固件版本 var getOsv = function () { var reg = /OS ((\d+_?){2,3})\s/; if (navigat ...
- INTERRUPT CONTROLLER
1,中断的基本概念 CPU与外设之间传输数据的控制方式通常有3种:查询方式,中断方式和DMA方式.查询方式的优点是硬件开销小不需要额外的硬件支持只是通过软件不断的轮询,使用起来也就比较简单,但在此方式 ...
- maven安装步骤
第一步:配置maven环境 maven3 安装: 安装 Maven 之前要求先确定你的 JDK 已经安装配置完成.Maven是 Apache 下的一个项目,目前最新版本是 3.0.4,我用的也是这个. ...
- uniq linux下去除重复行命令
一,uniq干什么用的 文本中的重复行,基本上不是我们所要的,所以就要去除掉.linux下有其他命令可以去除重复行,但是我觉得uniq还是比较方便的一个.使用uniq的时候要注意以下二点 1,对文本操 ...
- 对apache和memcache进行压力测试
工作中经常使用的软件之二:apache和memcache 以前经常听说memcache的TPS能达到几万,但一直也不知道apache的性能到底如何,所以在闲暇之余,就自己做了一下压力测试. 环境:两台 ...