您的位置:首页 > 其它

String reorder

2014-04-15 18:37 274 查看
Time Limit: 10000ms

Case Time Limit: 1000ms

Memory Limit: 256MB

Description

For this question, your program is required to process an input string containing only ASCII characters between ‘0’ and ‘9’, or between ‘a’ and ‘z’ (including ‘0’, ‘9’, ‘a’, ‘z’). 

Your program should reorder and split all input string characters into multiple segments, and output all segments as one concatenated string. The following requirements should also be met,

1. Characters in each segment should be in strictly increasing order. For ordering, ‘9’ is larger than ‘0’, ‘a’ is larger than ‘9’, and ‘z’ is larger than ‘a’ (basically following ASCII character order).

2. Characters in the second segment must be the same as or a subset of the first segment; and every following segment must be the same as or a subset of its previous segment. 

Your program should output string “<invalid input string>” when the input contains any invalid characters (i.e., outside the '0'-'9' and 'a'-'z' range).

Input

Input consists of multiple cases, one case per line. Each case is one string consisting of ASCII characters.

Output

For each case, print exactly one line with the reordered string based on the criteria above.

Sample In

aabbccdd

007799aabbccddeeff113355zz

1234.89898

abcdefabcdefabcdefaaaaaaaaaaaaaabbbbbbbddddddee

Sample Out

abcdabcd

013579abcdefz013579abcdefz

<invalid input string>
abcdefabcdefabcdefabdeabdeabdabdabdabdabaaaaaaa

#include <stdio.h>
#include <iostream>
#include <string>
#include <assert.h>
using namespace std;
bool g_charge = true;

void swap(char &a,char &b)
{
char temp;
temp = a;
a = b;
b = temp;
}
void bsertsort(string &a,const int n)
{
if(a.length()<=0 || n<=0)
{
g_charge = false;
return;
}
int low,high,m=1;
int t=0;
if(n==1) cout<<"only one elem!"<<endl;
else
{
for(int i=1;i<n;i++)
{
low = 0;
high = i-1;
t = i;
while(low<=high)
{
m = (low+high)/2;
if(a[i]<a[m]) high = m-1;
else low = m+1;
}//此时high是小于low的,所以要用high+1
for(int j=i-1;j>=high+1;j--)
{
if(a[i]<a[j])
{
swap(a[i],a[j]);
i--;
}
else break;
}
i = t;
}//end for i
}//end else
}
void print(const string &a,const int n)
{
for(int i=0;i<=n;i++)
{
cout<<a[i]<<" ";
}
cout<<endl;
}
void SortRsult(string &a,const int n)
{
char temp;
string b;
int i,j;
for(i=1;i<n;i++)
{
if((a[i-1] == a[i]))
{
temp = a[i];
for(j=i+1;j<n;j++)
{
a[j-1] = a[j];
}
a[j-1] = temp;
if(a==b) break;
b=a;
i--;
}
else
continue;
print(a,n);
}
}
int _tmain(int argc, _TCHAR* argv[])
{
string str;
cin>>str;
int StrLength = str.length();
int count = 0;
int max = 0;
for(int i=0;i<StrLength;i++)
{
if((!(str[i]>='0' && str[i]<='9' )) && (!(str[i] >= 'a' && str[i] <='z')))
cout<<"<invalid input string>"<<endl;
}
bsertsort(str,StrLength);
print(str,StrLength);
SortRsult(str,StrLength);
print(str,StrLength);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: