Advertisement
Dang_Quan_10_Tin

SUBSTR TS10 PTNK 2006-2007

Jan 15th, 2022
1,087
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.27 KB | None | 0 0
  1. #define task "SUBSTR"
  2.  
  3. #include <iostream>
  4. #include <cstdio>
  5. #include <vector>
  6.  
  7. using namespace std;
  8.  
  9. using ll = long long;
  10. using ld = long double;
  11.  
  12. constexpr int N = 1e5 + 5;
  13. string s;
  14.  
  15. void Read()
  16. {
  17.     cin >> s;
  18. }
  19.  
  20. bool Check(string s)
  21. {
  22.     vector<int> first(30, 0), last(30, 0);
  23.     s = " " + s;
  24.  
  25.     for (int i = 1; i < (int)s.size(); ++i)
  26.     {
  27.         if (!first[s[i] - 'A'])
  28.             first[s[i] - 'A'] = i;
  29.         last[s[i] - 'A'] = i;
  30.     }
  31.  
  32.     for (int i = 0; i < 26; ++i)
  33.         if (first[i] != last[i]) // Chữ cái thứ i xuất hiện nhiều hơn 1 lần
  34.             return false;
  35.  
  36.     return true;
  37. }
  38.  
  39. void Solve()
  40. {
  41.     int ans(0), pos(0);
  42.     for (int i = 0; i < (int)s.size(); ++i)
  43.         for (int j = i + ans; j < (int)s.size(); ++j) // Chúng ta chỉ cần duyệt các xâu độ dài > ans
  44.             if (Check(s.substr(i, j - i + 1)))
  45.             {
  46.                 ans = j - i + 1;
  47.                 pos = i + 1;
  48.             }
  49.  
  50.     cout << pos << " " << ans;
  51. }
  52.  
  53. int32_t main()
  54. {
  55.     ios::sync_with_stdio(0);
  56.     cin.tie(0);
  57.     cout.tie(0);
  58.     if (fopen(task ".INP", "r"))
  59.     {
  60.         freopen(task ".INP", "r", stdin);
  61.         freopen(task ".OUT", "w", stdout);
  62.     }
  63.  
  64.     Read();
  65.     Solve();
  66. }
  67.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement