Binary Protocol (Codeforces

来源:互联网 发布:js里面怎么让div隐藏 编辑:程序博客网 时间:2024/06/05 16:11

题目链接  http://codeforces.com/problemset/problem/825/A

A. Binary Protocol
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Polycarp has just invented a new binary protocol for data transmission. He is encoding positive integer decimal number to binary string using following algorithm:

  • Each digit is represented with number of '1' characters equal to the value of that digit (for 0 it is zero ones).
  • Digits are written one by one in order corresponding to number and separated by single '0' character.

Though Polycarp learnt how to encode the numbers, he has no idea how to decode them back. Help him calculate the decoded number.

Input

The first line contains one integer number n (1 ≤ n ≤ 89) — length of the string s.

The second line contains string s — sequence of '0' and '1' characters, number in its encoded format. It is guaranteed that the number corresponding to the string is positive and doesn't exceed 109. The string always starts with '1'.

Output

Print the decoded number.

Examples
input
3111
output
3
input
9110011101
output
2031
题目大意:

第一行输入一个整数n,第二行输入一个长度为n的字符串。题目要求按照一定的规则转换成十进制。加密规则为:

如果遇到‘1’,那么输出‘1’的连续个数;例如:1111,就输出4;111就输出3;

如果遇到‘0’,那么就输出‘0’的里阿奴个数减1(因为数字是以0隔开的);例如:11011,就输出22;110011,就输出202;

解题思路:

这就是考察逻辑思维的,不需要多解释。希望读者跟着代码走一遍,就一定会明白了其中的道理。其实非常简单。

代码:

#include<iostream>using namespace std;int main(){    int n;    char c[95];    while(cin>>n)    {        int sum1=0;        for(int i=0;i<=n-1;i++)            cin>>c[i];        for(int i=0;i<=n-1;i++)        {            if(c[i]=='1')                sum1++;            else            {                cout<<sum1;                sum1=0;            }        }        cout<<sum1<<endl;   //最终那一组数据需要最后单独输出,因为上面没有输最后一组数据    }    return 0;}