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
思路:使用一个长度为36(数字10个+26个字母)存储数字和字母出现次数,然后依次遍历数组,当出现次数不为0时,输出,出现次数减1,循环,循环停止条件为输出字符串长度与输入字符串长度相同
代码
#include<iostream>
#include<string>
using namespace std;
int main(){
string s;
while(cin>>s){
int len=s.size();
int flag=0;
for(int i=0;i<len;i++){
if((s[i]>='0' && s[i]<='9') || (s[i]>='a' && s[i]<='z')){
}
else{
cout<<"<invalid input string>"<<endl;
flag=1;
break;
}
}
if(flag==0){
int num[36];
int len2=0;
int temp;
for(int i=0;i<36;i++){
num[i]=0;
}
for(int i=0;i<len;i++){
if(s[i]>='0' && s[i]<='9'){
num[s[i]-'0']++;
len2++;
}
if(s[i]>='a' && s[i]<='z'){
num[s[i]-'a'+10]++;
len2++;
}
}
while(len2>0){
for(int i=0;i<36;i++){
if(num[i]>0){
if(i>=10){
cout<<(char)('a'+i-10);
}
else{
cout<<i;
}
num[i]--;
len2--;
}
}
}
cout<<endl;
}
}
return 0;
}
2.K-th string
Time Limit: 10000ms
Case Time Limit: 1000ms
Memory Limit: 256MB
Description
Consider a string set that each of them consists of {0, 1} only. All strings in the set have the same number of 0s and 1s. Write a program to find and output the K-th string according to the dictionary order. If such a string doesn’t exist, or the input is not valid, please output “Impossible”. For example, if we have two ‘0’s and two ‘1’s, we will have a set with 6 different strings, {0011, 0101, 0110, 1001, 1010, 1100}, and the 4th string is 1001.
Input
The first line of the input file contains a single integer t (1 ≤ t ≤ 10000), the number of test cases, followed by the input data for each test case.
Each test case is 3 integers separated by blank space: N, M(2 <= N + M <= 33 and N , M >= 0), K(1 <= K <= 1000000000). N stands for the number of ‘0’s, M stands for the number of ‘1’s, and K stands for the K-th of string in the set that needs to be printed as output.
Output
For each case, print exactly one line. If the string exists, please print it, otherwise print “Impossible”.
Sample In
3
2 2 2
2 2 7
4 7 47
Sample Out
0101
Impossible
01010111011
思路:带有重复的全排列
代码
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
int main(){
int N;
cin>>N;
for(int iCase=0;iCase<N;iCase++){
int m,n,t;
cin>>m>>n>>t;
int a=1,b=1,c=1;
for(int i=1;i<=m;i++){
a=a*i;
}
for(int i=1;i<=n;i++){
b=b*i;
}
for(int i=1;i<=m+n;i++){
c=c*i;
}
int d;
int flag=0;
d=c/(a+b);
if(t>d){
cout<<"Impossible"<<endl;
flag=1;
}
if(flag==0){
string str;
for(int i=0;i<m;i++){
str+='0';
}
for(int i=0;i<n;i++){
str+='1';
}
while(t>1){
next_permutation(str.begin(),str.end());
t--;
}
cout<<str<<endl;
}
}
return 0;
}