fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main() {
  5. char s[52];
  6. cin >> s;
  7.  
  8. int pos[26][2];
  9. int index[26] = {0};
  10.  
  11. // Lưu vị trí xuất hiện của từng ký tự
  12. for (int i = 0; i < 52; i++) {
  13. int c = s[i] - 'A';
  14. pos[c][index[c]] = i;
  15. index[c]++;
  16. }
  17.  
  18. int dem = 0;
  19.  
  20. for (int i = 0; i < 26; i++) {
  21. for (int j = i + 1; j < 26; j++) {
  22. int A1 = pos[i][0], A2 = pos[i][1];
  23. int B1 = pos[j][0], B2 = pos[j][1];
  24.  
  25. if ((A1 < B1 && B1 < A2 && A2 < B2) || (B1 < A1 && A1 < B2 && B2 < A2)) {
  26. dem++;
  27. }
  28. }
  29. }
  30.  
  31. cout << dem << endl;
  32. return 0;
  33. }
Success #stdin #stdout 0.01s 5280KB
stdin
ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ
stdout
325