fork download
  1. #include <iostream>
  2. using namespace std;
  3. struct field{
  4. string f;
  5. string s;
  6. int num;
  7. };
  8.  
  9. void removeNumbersAndCommas(string arr[], int n) {
  10. for (int i = 0; i < n; i++) {
  11. int j = 0; // Index for the current word
  12.  
  13. // Iterate over each character in the word
  14. while (arr[i][j] != '\0') {
  15. if (arr[i][j] >= '0' && arr[i][j] <= '9') {
  16. // If the character is a number, remove it by shifting the rest of the characters
  17. int k = j;
  18. while (arr[i][k] != '\0') {
  19. arr[i][k] = arr[i][k + 1];
  20. k++;
  21. }
  22. } else if (arr[i][j] == ',') {
  23. // If the character is a comma, remove it by shifting the rest of the characters
  24. int k = j;
  25. while (arr[i][k] != '\0') {
  26. arr[i][k] = arr[i][k + 1];
  27. k++;
  28. }
  29. } else {
  30. j++; // Move to the next character in the word
  31. }
  32. }
  33. }
  34. }
  35. void prints(field arr[], int n, string p[], string q[], string l[], string m[], string b[], int ind){
  36.  
  37. for(int i =0;i<n;i++){
  38. if(arr[i].num == 1){
  39. p[i] = arr[i%n].f;
  40. b[ind++] = p[i];
  41. q[i] = arr[i%n].s;
  42. b[ind++] = q[i];
  43.  
  44. }
  45. else if(arr[i].num ==0){
  46. l[i] = arr[i%n].f;
  47. b[ind++] = l[i];
  48. m[i] = arr[i%n].s;
  49. b[ind++] = m[i];
  50. }
  51. else{
  52. p[i] = arr[i%n].f;
  53. b[ind++] = p[i];
  54. q[i] = arr[i%n].f;
  55. b[ind++] = q[i];
  56. l[i] = arr[i%n].f;
  57. b[ind++] = l[i];
  58. m[i] = arr[i%n].f;
  59. b[ind++] = m[i];
  60. }
  61. }
  62. }
  63.  
  64. void find(field arr[], int n, string b[]){
  65. string p[n], q[n], l[n], m[n];
  66. int ind = 0;
  67. prints(arr, n, p, q, l, m, b, ind);
  68. string r[4*n];
  69.  
  70. removeNumbersAndCommas(b, n);
  71. for(int i = 0;i<4*n;i++){
  72. for(int j = i+1;j<4*n;j++){
  73. if(b[i]==b[j]){
  74. b[i] = "remove";
  75. }
  76. }
  77. }
  78. for(int i = 0;i<4*n;i++){
  79. if(b[i]!= "remove")
  80. r[i]= b[i];
  81. }
  82. for(int i =0;i<4*n;i++)
  83. cout<<r[i]<<" ";
  84.  
  85. }
  86.  
  87. void f(field arr[], int n){
  88. string b[4*n];
  89. find(arr, n, b);
  90. }
  91. int main() {
  92. // your code goes here
  93. int n;
  94. cin>>n;
  95. field arr[n];
  96. for(int i =0;i<n;i++){
  97. cin>>arr[i].f>>arr[i].s>>arr[i].num;
  98. }
  99.  
  100. f(arr, n);
  101. return 0;
  102. }
Success #stdin #stdout 0.01s 5360KB
stdin
6
Jo4e Ma,Ry 1
ElizaB53et5h June 1
Joe John 0
Joe, JuNe 0
John JUne 1
Margaret
stdout
Joe MaRy ElizaBeth June Joe  Joe, JuNe John JUne    Margaret