《1.String reorder》
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 <fstream>
#include <iostream>
#include <string>
using namespace std;
#define USE_IFSTREAM
class CHAR_COUNTER{
public:
char charValue;
long charCount;
};
//计数:这里我们需要降序
int compare_CHAR_COUNTER(const void * ptr1,const void * ptr2){
long value1=(*(CHAR_COUNTER*)ptr1).charCount;
long value2=(*(CHAR_COUNTER*)ptr2).charCount;
return (value1>value2)?-1:(value1==value2?0:1);
}
//字符:这里排成降序,后面颠倒顺序后(按升序)输出
int compare_char(const void * ptr1,const void * ptr2){
char value1 = *(char*)ptr1;
char value2= *(char*)ptr2;
return (value1>value2)?-1:(value1==value2?0:1);
}
void main(){
CHAR_COUNTER charArray[36];
char charSet[36+1];
int valueCharacters=0;
string str ;
long length = 0;
long i = 0;
long j = 0;
int k = 0;
int w;
long curOutPutTime = 0;
long maxOutPutTime =0;
long pausePoint =0;
#ifdef USE_IFSTREAM
ifstream ifs=ifstream("testCase.txt");
#endif
while(1){
i=0;
valueCharacters = 0;
memset(charArray,0,sizeof(charArray));
for( i=0;i<36;i++){
if(i<10)
charArray[i].charValue = char('0'+ i);
else
charArray[i].charValue = char('a'+(i-10));
charArray[i].charCount = 0;
}
#ifndef USE_IFSTREAM
getline(cin,str);
#else
if(ifs.eof())
break;
getline(ifs,str);
#endif
length = str.length();
i=0;
while(i<length){
char charTmp = str[i];
if(charTmp>='0' && charTmp<='9')
charArray[charTmp-'0'].charCount++;
else if(charTmp>='a' && charTmp<='z')
charArray[charTmp-'a'+10].charCount++;
else{
cout<<"<invalid input string>"<<endl;
goto again;
}
i++;
}
qsort(charArray,sizeof(charArray)/sizeof(CHAR_COUNTER),sizeof(CHAR_COUNTER),compare_CHAR_COUNTER);
memset(charSet,0,sizeof(charSet));
i=0;
while(charArray[i].charCount){
charSet[i]=charArray[i].charValue;
i++;
}
valueCharacters = i;//最少 1
qsort(charSet,sizeof(charSet)/sizeof(char)-1,sizeof(char),compare_char);//最后一个不参与排序
for(i=0;i<(valueCharacters/2);i++){ //逆序方便输出
char tmp=charSet[i];
charSet[i] = charSet[(valueCharacters-1)-i];
charSet[(valueCharacters-1)-i] = tmp;
}
curOutPutTime= 0;
maxOutPutTime = charArray[0].charCount;
pausePoint = charArray[(valueCharacters-1)].charCount;
while(curOutPutTime < maxOutPutTime){
if(curOutPutTime == pausePoint){
//w = valueCharacters;
if(valueCharacters > 1){
while(charArray[(valueCharacters-1)].charCount==pausePoint){
int left=0,right = (valueCharacters-1);
int index = ((left+right)/2);
char expectValue = charArray[valueCharacters-1].charValue;
while(left<=right){//二分查找
if(charSet[index] == expectValue){
for( k=index;k<(valueCharacters);k++)
charSet[k]=charSet[k+1];
break;
}
else if(charSet[index] < expectValue){
left=index+1;
index = (left+right)/2;
}
else if(charSet[index] > expectValue){
right =index-1;
index = (left+right)/2;
}
}
//charSet[j]= 0 ;
valueCharacters--;
}
if(valueCharacters > 0)
pausePoint = charArray[valueCharacters-1].charCount;
charSet[valueCharacters] = '\0';
}
}
cout<<(char*)(charSet);
curOutPutTime++;
}
cout<<endl;
flush(cout);
/*
char chartest[]="again";
cout<<chartest<<endl;
*/
again:
continue;
}
};