Codeforces 34 C. Page Numbers

题目链接:http://codeforces.com/contest/34/problem/C

«Bersoft» company is working on a new version of its most popular text editor — Bord 2010. Bord, like many other text editors, should be able to print out multipage documents. A user keys a sequence of the document page numbers that he wants to print out (separates them with a comma, without spaces).

Your task is to write a part of the program, responsible for «standardization» of this sequence. Your program gets the sequence, keyed by the user, as input. The program should output this sequence in format l1-r1,l2-r2,...,lk-rk, where ri + 1 < li + 1 for all i from 1 to k - 1, and li ≤ ri. The new sequence should contain all the page numbers, keyed by the user, and nothing else. If some page number appears in the input sequence several times, its appearances, starting from the second one, should be ignored. If for some element i from the new sequence li = ri, this element should be output as li, and not as «li - li».

For example, sequence 1,2,3,1,1,2,6,6,2 should be output as 1-3,6.

Input

The only line contains the sequence, keyed by the user. The sequence contains at least one and at most 100 positive integer numbers. It's guaranteed, that this sequence consists of positive integer numbers, not exceeding 1000, separated with a comma, doesn't contain any other characters, apart from digits and commas, can't end with a comma, and the numbers don't contain leading zeroes. Also it doesn't start with a comma or contain more than one comma in a row.

Output

Output the sequence in the required format.

Examples
input
Copy
1,2,3,1,1,2,6,6,2
output
Copy
1-3,6
input
Copy
3,2,1
output
Copy
1-3
input
Copy
30,20,10
output
Copy
10,20,30

题目大意:以逗号分隔给定所有的页码,出现多次的按一次计算,连续的l到r可以合并,写成l-r,从小到大输出,输出改变之后的格式。

题目思路:

把数字从字符串中转化出来,模拟判断一下两个连续的值

代码:

#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<queue>
#include<stack>
#include<map>

using namespace std;

#define FOU(i,x,y) for(int i=x;i<=y;i++)
#define FOD(i,x,y) for(int i=x;i>=y;i--)
#define MEM(a,val) memset(a,val,sizeof(a))
#define PI acos(-1.0)

const double EXP = 1e-9;
typedef long long ll;
typedef unsigned long long ull;
const int INF = 0x3f3f3f3f;
const ll MINF = 0x3f3f3f3f3f3f3f3f;
const double DINF = 0xffffffffffff;
const int mod = 1e9+7;
const int N = 1e6+5;

int vis[1005];
char str[N];
int a[1005];

int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    std::ios::sync_with_stdio(false);
    scanf(" %s",str);
    int len = strlen(str);
    MEM(vis,0);
    int cnt=0;
    int j=0,i=0;
    while(i<len)
    {
        if(str[i]==',')
        {
            int ans=0;
            for(int k=j;k<i;k++)
            {
                ans=ans*10+(str[k]-'0');
            }
            if(vis[ans]==0)
            {
                vis[ans]=1;
                a[cnt++]=ans;
            }
            j=i+1;
        }
        i++;
    }
    int ans=0;
    for(int k=j;k<i;k++)
    {
        ans=ans*10+(str[k]-'0');
    }
    if(vis[ans]==0)
    {
        vis[ans]=1;
        a[cnt++]=ans;
    }
    //cout<<"cnt="<<cnt<<endl;
    sort(a,a+cnt);
    //for(int i=0;i<cnt;i++)
    //    printf("%d%c",a[i],(i==cnt-1)?'\n':' ');
    int tmp=a[0];
    int loc=0;
    for(int i=1;i<cnt;i++)
    {
        if(a[i]>a[i-1]+1)
        {
            if(loc==i-1)
                printf("%d,",tmp);
            else
                printf("%d-%d,",tmp,a[i-1]);
            loc=i;
            tmp=a[i];
        }
    }
    if(loc==cnt-1)
        printf("%d\n",a[cnt-1]);
    else
        printf("%d-%d\n",tmp,a[cnt-1]);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/baodream/article/details/80376428