题目描述:
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 <iostream>
#include <string>
using namespace std;
const char usage[]={
"########welcome to use######"
"\r\nInput a line:\r\n"
"Input \'!\'to exit!\n "
};
int inline partions(string &l,int low,int high);
void inline qsort(string &l,int low,int high);
void inline quicksort(string &l,int n);
//按字典排序
void sort(string &str,int n)
{
quicksort(str,n);
}
//输出结果
string delcomplex(string& str)
{
unsigned int i=0;
string str2;
while(i<(str.length())){
if( str[i] == str[i+1] )
{
str2.append(1,(str[i+1]));//1个str[i+1],append(个数,字符),如果append(字符地址)会
str.erase(i+1,1); //从i+1擦除1个,如果erase(i+1)则会一直擦到结尾
}
else i++;
}
cout<<"One Subset:"<<str<<endl;
return str2;
}
int main() {
string str;
string::iterator it;
cout<<usage;
while(cin>>str)
{
it = str.begin();
if(*it == '!'){
cout<<"You input \'!\'.Bye!"<<endl;
break;
}
sort(str,str.size());
//产生结果
while(str.length()>0)
str = delcomplex(str);
cout<<usage;
}
return 0;
}
int inline partions(string &l,int low,int high)
{
int prvotkey=l[low];
char tmp;
tmp=l[low];
while (low<high)
{
while (low<high&&l[high]>=prvotkey)
--high;
l[low]=l[high];
while (low<high&&l[low]<=prvotkey)
++low;
l[high]=l[low];
}
l[low]=tmp;
return low;
}
void inline qsort(string &l,int low,int high)
{
int prvotloc;
if(low<high)
{
prvotloc=partions(l,low,high); //将第一次排序的结果作为枢轴
qsort(l,low,prvotloc-1); //递归调用排序 由low 到prvotloc-1
qsort(l,prvotloc+1,high); //递归调用排序 由 prvotloc+1到 high
}
}
void inline quicksort(string &l,int n)
{
qsort(l,0,n-1); //第一个作为枢轴 ,从第一个排到第n个
}
http://blog.csdn.net/guo8113/article/details/23557945