Openjudge 1.13-40 提取数字串按数值排序

40:提取数字串按数值排序

总时间限制: 
1000ms
 
内存限制: 
65536kB
描述

给定一个字符串,请将其中的所有数字串提取,并将每个数字串作为整数看待(假设可以用int 表示),按从小到大顺序输出结果,输出的整数之间以逗号间隔。如果没有数字,则输出0;例如:*1234.345#6781ad9jk81-11101?aght88ir09kp,其中的整数包括:1234,345,6781,9,81,11101,88,9,从小到大排序后,应该输出: 
9,9,81,88,345,1234,6781,11101 

输入
在一行内输入一串符号,长度不大于300。输入数据保证提取的整数不超过109
输出
从小到大排序的整数序列,如果没有数字,则输出0;
样例输入
*1234.345#6781ad9jk81-11101?aght88ir09kp
样例输出
9,9,81,88,345,1234,6781,11101
来源
元培-From Whf
注意姿势的模拟。可以练练点能力。。
我做的也不容易。。
一共WA了4遍 CE了1遍才A
下面是代码 
#include <algorithm>
#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>

using namespace std;

struct node{
    string a;
}e[30001];
int tot,i,s;
string str;
bool f;
bool cmp(node x,node y)
{
    if(x.a.length()==y.a.length()){
        if(x.a.length()!=1)
        {
            int d=0;
            while(x.a[d]==y.a[d]){
                ++d;
            }
            return x.a[d]<y.a[d];
        }
        else return x.a[0]<y.a[0];
    }
    else return x.a.length()<y.a.length();
}
int main()
{
    cin >> str;
    int st=0;
    for (i = max (s,0) ; i < str.length();++i)
    {
        if((str[i]>='0'&&str[i]<='9')&&!f)
        {
            st=i;
            f=1;
        }
        if(f&&(!(str[i]>='0'&&str[i]<='9'))) {
            ++tot;
            while(str[st]=='0'&&i-st>1){
                st++;
            }
            if(st!=i)
            {
                for(int j=st;j<i;++j)
                e[tot].a+=str[j];
            }
            else e[tot].a="0";
            f=0;
            st=0;
        }
    }
    if(f)
    {
        ++tot;
        while(str[st]=='0'&&i-st>1){
                st++;
        }
        if(st!=str.length())
        for(int j=st;j<str.length();++j)
        e[tot].a+=str[j];
        else e[tot].a="0";
    }
/*    if(st&&ov)
    {
        ++tot;
        for(int j=st;j<=ov;++j)
        e[tot].a+=str[j];
    }
*/    if(!tot)
    {
        cout<<"0";
    }
    else{
        sort(e+1,e+tot+1,cmp);
        for(i=1;i<tot;++i)
        cout<<e[i].a<<",";
        cout<<e[tot].a;
    }
    return 0;
}
点击展开