fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <algorithm>
  5.  
  6. using namespace std;
  7.  
  8. bool EncloseWithBrackets(string& ReferenceStr, string& TargetStr, size_t pos) {
  9. if (!count(ReferenceStr.begin(), ReferenceStr.end(), TargetStr[pos])) {
  10. ReferenceStr += TargetStr[pos];
  11. TargetStr.insert(pos, "[");
  12. TargetStr.insert(pos + 2, "]");
  13. return true;
  14. }
  15. return false;
  16. }
  17.  
  18.  
  19. int main() {
  20. int N = 0;
  21. vector<string> Options;
  22. string ShortCut;
  23.  
  24. cin >> N;
  25.  
  26. cin.ignore();
  27. for (int i = 0; i < N; i++) {
  28. string input;
  29. getline(cin, input);
  30. Options.push_back(input);
  31. }
  32.  
  33. for (int i = 0; i < N; i++) {
  34. cout << Options[i] << endl;
  35. }
  36.  
  37. bool sw = false;
  38. size_t pos = 0;
  39. for (int i = 0; i < N; i++) {
  40. if (EncloseWithBrackets(ShortCut, Options[i], 0)) {
  41. cout << Options[i] << endl;
  42. continue;
  43. }
  44. while ((pos = Options[i].find(" ", pos)) != string::npos) {
  45. pos += 1;
  46. if (sw = EncloseWithBrackets(ShortCut, Options[i], pos)) {
  47. break;
  48. }
  49. }
  50. if (sw) {
  51. sw = false;
  52. cout << Options[i] << endl;
  53. continue;
  54. }
  55. pos = 0;
  56. while (Options[i][pos] != '\0') {
  57. pos += 1;
  58. if (EncloseWithBrackets(ShortCut, Options[i], pos)) {
  59. break;
  60. }
  61. }
  62. cout << Options[i] << endl;
  63. }
  64.  
  65. for (int i = 0; i < N; i++) {
  66. cout << Options[i] << endl;
  67. }
  68. return 0;
  69. }
Success #stdin #stdout 0.01s 5288KB
stdin
5
New
Open
Save
Save As
Save All
stdout
New
Open
Save
Save As
Save All
[N]ew
[O]pen
[S]ave
Save [A]s
S[a]ve All
[N]ew
[O]pen
[S]ave
Save [A]s
S[a]ve All