fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define task "thd"
  5.  
  6. string S;
  7. int n;
  8.  
  9. // Kiểm tra xem tất cả các xâu con độ dài k có đôi một phân biệt không
  10. bool check(int k) {
  11. unordered_set<string> st;
  12. for (int i = 0; i <= n - k; i++) {
  13. string sub = S.substr(i, k);
  14. // Nếu đã xuất hiện xâu trùng -> k này không hợp lệ
  15. if (st.count(sub)) return false;
  16. st.insert(sub);
  17. }
  18. return true;
  19. }
  20.  
  21. int main() {
  22. ios_base::sync_with_stdio(0);
  23. cin.tie(0);
  24.  
  25. if (fopen(task".inp", "r")) {
  26. freopen(task".inp", "r", stdin);
  27. freopen(task".out", "w", stdout);
  28. }
  29.  
  30. cin >> S;
  31. n = S.size();
  32.  
  33. // Thử độ dài k từ 1 đến n, gặp k thỏa mãn đầu tiên là dừng
  34. for (int k = 1; k <= n; k++) {
  35. if (check(k)) {
  36. cout << k;
  37. return 0;
  38. }
  39. }
  40.  
  41. return 0;
  42. }
Success #stdin #stdout 0.01s 5320KB
stdin
Standard input is empty
stdout
Standard output is empty