Skip to content

Instantly share code, notes, and snippets.

@yanring
Created March 20, 2019 11:43
Show Gist options
  • Save yanring/12e8769f59b233d7959d8e284a06859d to your computer and use it in GitHub Desktop.
Save yanring/12e8769f59b233d7959d8e284a06859d to your computer and use it in GitHub Desktop.
#include <iostream>
#include <string>
using namespace std;
int daxie[26],xiaoxie[26];
string str;
void scan(string str){
int len = str.size();
for(char c : str){
if(c <= 'Z' and c>='A'){
daxie[c-'A']++;
}else if(c<='z' and c>='a'){
xiaoxie[c-'a']++;
}
}
}
int find_start_index(){
int count = 0, start_index = -1, max_count=0;
for(int i = 25; i >= 0 ; i--){
if(daxie[i]>0 && xiaoxie[i]>0){
count++;
if(count >= max_count){
max_count = count;
start_index = i;
}
}else{
count=0;
}
}
return start_index;
}
void print_res(){
int pair_count = 0, start_index=-1;
while(1){
start_index = find_start_index();
if(start_index == -1)
break;
else{
while(daxie[start_index]>0 && xiaoxie[start_index]>0){
cout<<(char)('A'+start_index)<<(char)('a'+start_index);
daxie[start_index] --;
xiaoxie[start_index] --;
start_index ++;
}
cout<<endl;
}
}
}
int main(int argc, char *argv[]) {
while(1){
memset(daxie, 0, sizeof(daxie));
memset(daxie, 0, sizeof(xiaoxie));
cin>>str;
scan(str);
if(find_start_index()>-1)
print_res();
else
cout<<"Not Found"<<endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment