Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <iostream>
  3. #include <vector>
  4. #include <string>
  5. #include <cstring>
  6. #include <cmath>
  7. using namespace std;
  8.  
  9. bool is_possible(vector<char> str){
  10. int i;
  11. int num;
  12. int check = 0;
  13. int count_odd = 0;
  14. bool has_check[str.size()] = {false};
  15. for(num = 0; num < str.size(); num++){
  16. check = 0;
  17. if(has_check[num] == true){
  18. continue;
  19. }
  20. for(i = num; i < str.size(); i++){
  21. if(str[num] == str[i]){
  22. has_check[i] = true;
  23. ++check;
  24. }
  25. if(check == 2){
  26. break;
  27. }
  28. }
  29. if(check == 1){
  30. count_odd++;
  31. }
  32. has_check[num] = true;
  33. }
  34. if(count_odd > 1){
  35. return false;
  36. }
  37. else{
  38. return true;
  39. }
  40. }
  41. int count_times(vector<char> str){
  42. int start = 0;
  43. int end = str.size()-1;
  44. int same = -1;
  45. int size_mid = str.size()/2;
  46. char temp;
  47. int count = 0;
  48. while(start < size_mid){
  49. //same從後面開始找
  50. for(int i = end; i > start; i--){
  51. if(str[i] == str[start]){
  52. same = i;
  53. break;
  54. }
  55. }
  56. //如果是只有一個,移到中間
  57. if(same == -1){
  58. //cout << "has one" << endl;
  59. for(int i = start; i < size_mid; i++){
  60. temp = str[i];
  61. str[i] = str[i+1];
  62. str[i+1] = temp;
  63. count++;
  64. }
  65. //cout << "after same : " << count << endl;
  66. continue;
  67. }
  68. //移動到最後
  69. for(int i = same; i < end; i++){
  70. temp = str[i];
  71. str[i] = str[i+1];
  72. str[i+1] = temp;
  73. count++;
  74. }
  75. //cout << "after move to end : " << count << endl;
  76. start++;
  77. end--;
  78. same = -1;
  79. }
  80. return count;
  81. }
  82. int main(){
  83. int amount;
  84. int count = 0;
  85. cin >> amount;
  86. cin.ignore();
  87. while(count < amount){
  88. string input;
  89. vector<char> first;
  90. getline(cin, input);
  91. char str[input.size()];
  92. strcpy(str, input.c_str());
  93. for(int i = 0; i < sizeof(str); i++){
  94. first.push_back(str[i]);
  95. }
  96. vector<char>::iterator it = first.begin();
  97. if(!is_possible(first)){
  98. cout << "Impossible" << endl;
  99. count++;
  100. continue;
  101. }
  102. else{
  103. cout << count_times(first) << endl;
  104. }
  105. count++;
  106. }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement