-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from AlgoLeadMe/7-g0rnn
7-g0rnn
- Loading branch information
Showing
2 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
#include <iostream> | ||
using namespace std; | ||
|
||
int alpha[26] = { | ||
0, | ||
}; | ||
string name; | ||
int n; | ||
|
||
// When palindrome is 'ABABA', half is 'ABA' and n.length = 5 | ||
void print_palindrome(string half) | ||
{ | ||
cout << half; | ||
// subtract 1 when it's odd. | ||
int len = half.length() - n % 2; | ||
for (int i = len - 1; i >= 0; i--) | ||
{ | ||
cout << half[i]; | ||
} | ||
} | ||
|
||
int main() | ||
{ | ||
ios_base::sync_with_stdio(false); | ||
cin.tie(NULL); | ||
|
||
cin >> name; | ||
n = name.length(); | ||
|
||
// init alpha[] | ||
for (int i = 0; i < n; i++) | ||
{ | ||
int index = name[i] - 'A'; | ||
alpha[index]++; | ||
} | ||
|
||
int i; | ||
string ret = ""; | ||
for (i = 0; i < 26; i++) | ||
{ | ||
// push each alphabet until possible | ||
while (alpha[i] > 1) | ||
{ | ||
ret = ret + (char)(i + 'A'); | ||
alpha[i] -= 2; | ||
} | ||
} | ||
|
||
// when length is odd | ||
if (n % 2 == 1) | ||
{ | ||
i = 0; | ||
while (alpha[i] == 0) | ||
i++; | ||
ret = ret + (char)(i + 'A'); | ||
alpha[i] -= 1; | ||
} | ||
|
||
for (i = 0; i < 26; i++) | ||
{ | ||
if (alpha[i] > 0) | ||
{ | ||
cout << "I'm Sorry Hansoo\n"; | ||
return 0; | ||
} | ||
} | ||
|
||
print_palindrome(ret); | ||
return 0; | ||
} |