codeforces_600A. Extract Numbers(字符串处理)

来源:互联网 发布:数据的收集与整理 编辑:程序博客网 时间:2024/06/10 03:52
A. Extract Numbers
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given string s. Let's call word any largest sequence of consecutive symbols without symbols ',' (comma) and ';' (semicolon). For example, there are four words in string "aba,123;1a;0": "aba", "123", "1a", "0". A word can be empty: for example, the strings=";;" contains three empty words separated by ';'.

You should find all words in the given string that are nonnegative INTEGER numbers without leading zeroes and build by them new string a. String a should contain all words that are numbers separating them by ',' (the order of numbers should remain the same as in the string s). By all other words you should build string b in the same way (the order of numbers should remain the same as in the string s).

Here strings "101", "0" are INTEGER numbers, but "01" and "1.0" are not.

For example, for the string aba,123;1a;0 the string a would be equal to "123,0" and string b would be equal to "aba,1a".

Input

The only line of input contains the string s (1 ≤ |s| ≤ 105). The string contains only symbols '.' (ASCII 46), ',' (ASCII 44), ';' (ASCII 59), digits, lowercase and uppercase latin letters.

Output

Print the string a to the first line and string b to the second line. Each string should be surrounded by quotes (ASCII 34).

If there are no words that are numbers print dash (ASCII 45) on the first line. If all words are numbers print dash on the second line.

Examples
input
aba,123;1a;0
output
"123,0""aba,1a"
input
1;;01,a0,
output
"1"",01,a0,"
input
1
output
"1"-
input
a
output
-"a"
Note

In the second example the string s contains five words: "1", "", "01", "a0", "".

对我来说有点恶心的模拟题,反正我是wa了两次,做了挺久的,而且代码时间复杂度很大,后来发现这道题用STL会很方便,

或者用上void *memcpy(void *dest, const void *src, size_t n);(从源src所指的内存地址的起始位置开始拷贝n个字节到目标dest所指的内存地址的起始位置中)也会减少代码量。。。。。

好吧,还是要多做。。。。

#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>#include <stack>#include <bitset>#include <map>#include <string>#include <algorithm>#define Si(a) scanf("%d",&a)#define Sl(a) scanf("%lld",&a)#define Sd(a) scanf("%lf",&a)#define Ss(a) scanf("%s",a)#define Pi(a) printf("%d\n",(a))#define Pl(a) printf("%lld\n",(a))#define Pd(a) printf("%lf\n",(a))#define Ps(a) printf("%s\n",(a))#define W(a) while(a--)#define mem(a,b) memset(a,(b),sizeof(a))#define inf 0x3f3f3f3f#define maxn 1000010#define mod 1000000007#define PI acos(-1.0)#define LL long longusing namespace std;char s[100010];char a[100010];char b[100010];int l_a=0,l_b=0;int is_int(char ch){    if(ch>='0'&&ch<='9')return 1;    else return 0;}int is_a(char *c,int l,int r){    int i,j;    if(c[l]=='0'&&(c[l+1]!=','&&c[l+1]!=';'&&c[l+1]!='\0'))return 0;    for(i=l; i<=r; i++)    {        if(!is_int(c[i]))return 0;    }    //printf("l:%d\n",l);    for(i=l; i<=r; i++)    {        a[l_a]=c[i];        l_a++;    }    a[l_a]=',';    l_a++;    return 1;}int load_b(char *c,int l,int r){    int i;    if(c[l]==';'||c[l]==',')    {        b[l_b]=',';        l_b++;        return 1;    }    for(i=l; i<=r; i++)    {        b[l_b]=c[i];        l_b++;    }    b[l_b]=',';    l_b++;}int main(){    gets(s);    int len=strlen(s);    s[len]=',';    int i,j,l=0;    for(i=0; s[i]!='\0'; i++)    {        if((s[i]==','||s[i]==';')&&i==0)        {            b[l_b]=',';            l_b++;            l++;            continue;        }        if(s[l]==','||s[l]==';')        {            load_b(s,l,l);            l++;            if(s[i+1]!=','&&s[i+1]!=';')            i++;            continue;        }        if((s[i]==','||s[i]==';'))        {            int first=l;            int last=i-1;            //printf("%c %c\n",s[first],s[last]);            if(!is_a(s,first,last))            {                load_b(s,first,last);            }            l=i+1;        }    }    if(!is_a(s,l,i))    {        load_b(s,l,i);    }    a[l_a-1]='\0';    b[l_b-1]='\0';    if(strlen(a)==0)    {        printf("-\n");    }    else    {        printf("%c%s%c\n",'"',a,'"');    }    if(strlen(b)==0)    {        printf("-\n");    }    else    {        printf("%c",'"');        for(i=0; i<strlen(b)-1; i++)        {            printf("%c",b[i]);        }        printf("%c\n",'"');    }    return 0;}

补充上islava大神的漂亮代码

#include <bits/stdc++.h>#define files(x) freopen(x ".in", "r", stdin); freopen(x ".out", "w", stdout);#define all(a) (a).begin(),(a).end()#define ld long double#define ll long long#define sqr(a) (a)*(a)#define mp make_pair#define pb push_back#define x first#define y second#define inf (int)1e9#define pi pair<int,int>#define y1 fdfsusing namespace std;string s,t;vector<string>a,b;bool f(string s){    if(s.empty()) return 0;    if(s.size()==1&&s=="0") return 1;    if(s[0]=='0') return 0;    if(!isdigit(s[0])) return 0;    for(int i=0;i<s.size();++i)        if(!isdigit(s[i])) return 0;    return 1;}int main(){    #ifndef LOCAL        //files("");    #endif    ios_base::sync_with_stdio(0);    cin>>s;    for(int i=0;i<s.size();++i)    {        if(s[i]!=','&&s[i]!=';')        {            t.pb(s[i]);        }else{            if(f(t)) a.pb(t);            else b.pb(t);            t.clear();        }    } if(f(t)) a.pb(t);            else b.pb(t);    if(a.size()) cout<<'"';    for(int i=0;i+1<a.size();++i)        cout<<a[i]<<',';    if(a.size()) cout<<a.back()<<'"';else cout<<"-";    cout<<"\n";    a=b;    if(a.size()) cout<<'"';    for(int i=0;i+1<a.size();++i)        cout<<a[i]<<',';    if(a.size()) cout<<a.back()<<'"';else cout<<"-";    cout<<"\n";}


0 0
原创粉丝点击