fork download
  1. #include <iostream>
  2. #include <unordered_set>
  3. #include <string>
  4. #include <vector>
  5. using namespace std;
  6.  
  7. bool isValidAlienABC(const string& S, unordered_set<string>& seen) {
  8. int n = S.size();
  9. for (int i = 0; i < n; ++i) {
  10. for (int len = 1; len <= 2 && i + len <= n; ++len) {
  11. string letter = S.substr(i, len);
  12. if (seen.find(letter) != seen.end()) return false;
  13. seen.insert(letter);
  14. }
  15. }
  16. return true;
  17. }
  18.  
  19. void reconstructAlienABC(const string& S) {
  20. unordered_set<string> seen;
  21. string result;
  22. int n = S.size();
  23.  
  24. for (int i = 0; i < n; ) {
  25. if (i + 1 < n && S[i] == S[i + 1]) { // Double letter case
  26. string doubleLetter = S.substr(i, 2);
  27. if (seen.find(doubleLetter) != seen.end()) {
  28. cout << -1 << endl;
  29. return;
  30. }
  31. result += doubleLetter + " ";
  32. seen.insert(doubleLetter);
  33. i += 2;
  34. } else { // Single letter case
  35. string singleLetter = S.substr(i, 1);
  36. if (seen.find(singleLetter) != seen.end()) {
  37. cout << -1 << endl;
  38. return;
  39. }
  40. result += singleLetter + " ";
  41. seen.insert(singleLetter);
  42. i++;
  43. }
  44. }
  45. cout << result << endl;
  46. }
  47.  
  48. int main() {
  49. int T;
  50. cin >> T;
  51. while (T--) {
  52. string S;
  53. cin >> S;
  54. reconstructAlienABC(S);
  55. }
  56. return 0;
  57. }
Success #stdin #stdout 0.01s 5276KB
stdin
Standard input is empty
stdout
Standard output is empty