【微软2014实习生】 String reorder
2014-04-27 22:00
363 查看
问题描述:
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
基本思路:利用map容器实现,统计每个字符的个数,然后逐个输出,直到该字符的个数为0。
#include<iostream>
#include<string>
#include<map>
using namespace std;
string errormsg ="<invalid input string>";
string getresult(string str)
{
map<char,int> mp;
string reslut ="";
if(str.empty())
return errormsg;
if(str.size()==0)
return errormsg;
for(string::size_type i=0;i<str.size();i++)
{
char temp= str[i];
if((temp<='z'&& temp>='a')||(temp<='9'&& temp>='0'))
{
mp[temp]++;
}
else
{
return errormsg;
}
}
bool isover = false;
while(!isover)
{
isover = true;
map<char,int>::iterator iter = mp.begin();
for(;iter!=mp.end();iter++)
{
if(iter->second>0)
{
isover = false;
reslut +=iter->first;
iter->second--;
}
}
}
return reslut;
}
int main(void) {
string str;
while(getline(cin,str))
{
string res= getresult(str);
cout<<res<<endl;;
cin.ignore();
}
return 0;
}
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
基本思路:利用map容器实现,统计每个字符的个数,然后逐个输出,直到该字符的个数为0。
#include<iostream>
#include<string>
#include<map>
using namespace std;
string errormsg ="<invalid input string>";
string getresult(string str)
{
map<char,int> mp;
string reslut ="";
if(str.empty())
return errormsg;
if(str.size()==0)
return errormsg;
for(string::size_type i=0;i<str.size();i++)
{
char temp= str[i];
if((temp<='z'&& temp>='a')||(temp<='9'&& temp>='0'))
{
mp[temp]++;
}
else
{
return errormsg;
}
}
bool isover = false;
while(!isover)
{
isover = true;
map<char,int>::iterator iter = mp.begin();
for(;iter!=mp.end();iter++)
{
if(iter->second>0)
{
isover = false;
reslut +=iter->first;
iter->second--;
}
}
}
return reslut;
}
int main(void) {
string str;
while(getline(cin,str))
{
string res= getresult(str);
cout<<res<<endl;;
cin.ignore();
}
return 0;
}
相关文章推荐
- 微软2014实习生及秋令营技术类职位在线测试--String reorder
- 微软2014实习生及秋令营技术类职位在线测试-1_String reorder
- 微软2014实习生及秋令营技术类职位在线测试 原创解答 题目1 : String reorder
- 【微软2014实习生及秋令营技术类职位在线測试】题目1 : String reorder
- 微软2014实习生在线测试之String reorder
- 微软2014实习生在线测试之String reorder .
- (2014微软实习生笔试题)1.String reorder
- 微软2014实习生及秋令营技术类职位在线测试 String reorder java
- 微软2014实习生及秋令营技术类职位在线测试(题目1 : String reorder)
- 微软2014实习生招聘在线测试第1题——string reorder
- 微软2014实习生及秋令营技术类职位在线测试(题目1 : String reorder)
- 微软2014实习生及校招秋令营技术类职位在线测试:1.String reorder
- 微软2014实习生及秋令营技术类职位在线测试:String reorder
- 微软2014实习生招聘编程测试string reorder
- 【微软2014实习生及秋令营技术类职位在线测试】题目1 : String reorder
- 微软2014实习生及秋令营技术类职位在线测试——String reorder
- 微软2014实习生及秋令营技术类职位在线测试第一题:String reorder
- 【微软2014实习生及秋令营技术类职位在线测试】题目1 : String reorder
- 微软2014实习生及秋令营技术类职位在线测试-题目1 : String reorder
- 微软2014实习生及秋令营技术类职位在线测试 题目1 : String reorder