CodeForcesGym 100641D Generalized Roman Numerals

Generalized Roman Numerals

Time Limit: 5000ms
Memory Limit: 262144KB
This problem will be judged on  CodeForcesGym. Original ID: 100641D
64-bit integer IO format: %I64d      Java class name: (Any)
 

The ancient Romans developed a terrible numbering system in which I, V, X, L and C stood for 1, 5, 10, 50 and 100, respectively. So XXXVII represents 37 (10+10+10+5+1+1). They typically wrote the numerals in non-increasing order. However, when a single Roman numeral is written before one that is larger, we subtract the smaller from the larger. So we can write IV and IX to represent 4 and 9 (subtracting 1), or XL and XC to represent 40 and 90 (subtracting 10). To represent 94, we would write XCIV. VIC is generally not considered a traditional Roman numeral, but we can interpret this as another representation of 94: VI is 6, so VIC is 100-6. In general, if we have two expressions a and b representing values v(a) and v(b), then we say that v(ab) is v(a) + v(b) if v(a) v(b), and v(b) ? v(a) otherwise. Unfortunately, this generalization introduces some ambiguity, since dierent orders of evaluation may result in dierent values. For example, consider IVX: IV is 4 and X is 10, so by that reasoning IVX is 6. However, I is 1 and VX is 5, so this suggests that IVX is actually 4. To remedy this ambiguity, we allow the addition of parentheses. The question arises: for a given string of Roman numeral characters, how many dierent values can be obtained using dierent placements of parentheses?

Input

Each test case consists of a single string containing only the characters I, V, X, L and C. The length of
this string will be 50. A line containing of a single 0 will terminate input.

Output
For each test case, output all possible distinct values that can be represented by the string via the
addition of parentheses. Display these values in increasing order.

Sample Input
IVX
XIXIX
0


Sample Output
Case 1: 4 6
Case 2: 8 10 28 30 32

解题:区间dp...吓呆本宝宝 了5次幂的时间复杂度

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int maxn = 55;
 4 unordered_set<int>dp[maxn][maxn];
 5 unordered_map<char,int>ump;
 6 char str[maxn];
 7 void solve(int a,int b,int c) {
 8     for(auto &A:dp[a][c])
 9         for(auto &B:dp[c+1][b]) {
10             if(A >= B) dp[a][b].insert(A + B);
11             else dp[a][b].insert(B - A);
12         }
13 }
14 int ret[10001000],tot;
15 int main() {
16     int cs = 1;
17     ump['I'] = 1;
18     ump['V'] = 5;
19     ump['X'] = 10;
20     ump['L'] = 50;
21     ump['C'] = 100;
22     while(~scanf("%s",str)) {
23         if(str[0] == '0') break;
24         for(int i = 0; i < maxn; ++i)
25             for(int j = 0; j < maxn; ++j)
26                 dp[i][j].clear();
27         int len = strlen(str);
28         for(int i = 0; i < len; ++i)
29             dp[i][i].insert(ump[str[i]]);
30         for(int i = 2; i <= len; ++i)
31             for(int j = 0; j + i <= len; ++j)
32                 for(int k = j,t = j + i - 1; k < t; ++k)
33                     solve(j,t,k);
34         tot = 0;
35         for(auto &it:dp[0][len-1]) ret[tot++] = it;
36         sort(ret,ret+tot);
37         printf("Case %d:",cs++);
38         for(int i = 0; i < tot; ++i)
39             printf(" %d",ret[i]);
40         putchar('\n');
41     }
42     return 0;
43 }
View Code

 

 

转载于:https://www.cnblogs.com/crackpotisback/p/4758495.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值