uva 11166 - Power Signs (贪心)
来源:互联网 发布:生鲜类app源码 编辑:程序博客网 时间:2024/06/04 17:42
Problem B
Power Signs
Input: Standard Input
Output: Standard Output
"Increase my killing power, eh?"
– Homer Simpson
–
You are probably familiar with the binary representation of integers, i.e. writing a nonnegative integer n as ∑ai2i, where each ai is either 0 or 1. In this problem, we consider a so called signed binary representation, in which we still write n as ∑ai2i, but allow ai to take on the values -1, 0 and 1. We write a signed binary representation of a number as a vector (ak, ak-1, ..., a1, a0). For instance, n = 13 can be represented as (1, 0, 0, -1, -1) = 24 - 21 - 20.
The binary representation of a number is unique, but obviously, the signed binary representation is not. In certain applications (e.g. cryptography), one seeks to write a number n in signed binary representation with as few non-zero digits as possible. For example, we consider the representation (1, 0, 0, -1) to be a better representation of n = 7 than (1, 1, 1). Your task is to write a program which will find such a minimal representation.
Input
The input consists of several test cases (at most 25), one per line. Each test case consists of a positive integer n ≤ 250000 written in binary without leading zeros. The input is terminated by a case where n = 0, which should not be processed.
Output
For each line of input, output one line containing the signed binary representation of n that has the minimum number of non-zero digits, using the characters '-' for -1, '0' for 0 and '+' for +1. The number should be written without leading zeros. If there are several possible answers, output the one that is lexicographically smallest (by the ASCII ordering).
Sample Input Output for Sample Input
10000
1111
10111
0
+0000
+000-
++00-
Problem setter: Per Aurtrin
Special Thanks: Mikael Goldmann
让0减少的唯一方式,就是类似用+00-这样的形式替换111,还要注意连续的2个1,是否替换?如果2个1在开头,就不换,否则换,字典序会变小。
#include<cstdio>#include<map>#include<queue>#include<cstring>#include<iostream>using namespace std;const int maxn = 50000 + 5;typedef long long LL;typedef pair<int,int> P;char s[maxn];void change(int last, int pos){ s[last] = '-'; for(int j = last-1;j > pos;j--){ s[j] = '0'; } s[pos] = '1';}int main(){ while(scanf("%s", s+1)){ if(s[1] == '0') break; int n = strlen(s+1); s[0] = '0'; int pos = n; int sum = 0; int last = -1; while(pos >= 1){ if(s[pos]=='1'){ if(last == -1){ last = pos; } sum++; } else{ if(sum >= 2){ change(last, pos); sum = 1; last = pos; } else{ sum = 0; last = -1; } } pos--; } if(sum > 2){ change(last, pos); } for(int i = 0;i <= n;i++){ if(s[i]=='1') s[i] = '+'; } if(s[0]=='0') printf("%s\n",s+1); else printf("%s\n",s); } return 0;}
- uva 11166 - Power Signs (贪心)
- HDU6034 Balala Power! [贪心]
- HDU6034-Balala Power! 贪心
- hdu6034 Balala Power! 贪心
- Balala Power(贪心)
- HDU6034 Balala Power!【贪心】
- uva 10330 - Power Transmission
- UVa Power of Cryptography
- uva Power of Cryptography
- Uva 151 - Power Crisis
- uva 151 - Power Crisis
- UVA 151 - Power Crisis
- UVa:10330 Power Transmission
- UVA Power of Cryptography
- UVa 151 - Power Crisis
- UVa 151 - Power Crisis
- UVa 10298 - Power Strings
- UVa 1374 Power Calculus
- javaScript在ie中运行不起
- 关于Log.isLoggable(TAG, Log.VERBOSE)
- MySQL SELECT 语句
- 多线程总结
- 【纯记录】谨以此文悼念我为此失去的时间|vim+pyclewn|
- uva 11166 - Power Signs (贪心)
- 多参数[构建器]
- JSON对象和JSON字符串
- 【Android快速入门】目录结构及adb命令【附Android拨号器的实现,自作】
- Java编程基础
- Animation and Animate.
- POJ-1159(添加一些字符使得字符串对称)(DP+滚动数组)
- C#中值类型和引用类型的区别浅记
- SVN 在线代码托管工具