Advertisement
kuludrew

strings

Nov 28th, 2014
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.69 KB | None | 0 0
  1. #include <iostream>
  2. #include <set>
  3. #include <map>
  4. #include <vector>
  5. #include <cstring>
  6. #include <algorithm>
  7. #include <cmath>
  8. #include <stdio.h>
  9. #include <queue>
  10. #include <cstdio>
  11. #include <stack>
  12. #include <iomanip>
  13. #include <numeric>
  14. #include <fstream>
  15. #include <functional>
  16. #include <sstream>
  17.  
  18. #define SSTR( x ) dynamic_cast< std::ostringstream & >( \
  19.        ( std::ostringstream() << std::dec << x ) ).str()
  20. #define LL long long
  21. #define fr(i,j) for(int i=0; i<j; i++)
  22. #define each(i,in,j) for(int i=in; i<j; i++)
  23. #define F first
  24. #define S second
  25. using namespace std;
  26.  
  27. ifstream fin("strings.in");
  28. #define cin fin
  29.  
  30. ofstream fout("strings.out");
  31. #define cout fout
  32.  
  33. const int con = 1e9;
  34. typedef vector<int> vi;
  35. typedef vector<vector<int > > vvi;
  36. typedef vector<pair<int,int > > vpi;
  37. typedef pair<int, int> pi;
  38. typedef vector<int>::iterator viit;
  39. typedef vector<LL> vll;
  40. typedef vector<bool> vb;
  41. template <typename T>
  42. void gv (vector<T>& a){
  43.        fr(h,a.size()) scanf("%d",&a[h]);
  44. }
  45. void gv (vpi& a){
  46.    int b,c;
  47.    fr(h,a.size()) {
  48.        scanf("%d %d",&b,&c);
  49.        a[h].first = b;
  50.        a[h].second = c;
  51.    }
  52. }
  53. int max(int a, int b) {
  54.     return a>b?a:b;
  55. }
  56. void pnt (vi a, int n) {
  57.        fr(h,n) cout<<a[h]<<" ";
  58.        cout<<"\n";
  59. }
  60. template <typename T>
  61. void pnt (vector<T> a) {
  62.        if (a.size()==0) cout<<"\n";
  63.        fr(h,a.size()-1) cout<<a[h]<<" ";
  64.        cout<<a.back()<<"\n";
  65. }
  66. void pnt (vvi a) {
  67.        fr(h,a.size()) pnt(a[h]);
  68. }
  69. int gcd (int a, int b) {
  70.         return b ? gcd (b, a % b) : a;
  71. }
  72. vpi v;
  73.  
  74. bool cond (int i, int j){
  75.     pi p1 = v[i], p2 = v[j];
  76.     return (p1.F<p2.F)||(p1.F==p2.F&&p1.S<=p2.S);
  77. }
  78. map<string, int> m;
  79. vi oc;
  80. int mx;
  81. string ans;
  82. string s;
  83. int n;
  84. void proc(int l){
  85.     bool b;
  86.     for(int i = 0; i <= n-l; i++){
  87.         b = true;
  88.         int lim = i+l;
  89.         for(int j = i; j<i+l; j++){
  90.             if (oc[j]<lim) b = false;
  91.         }
  92.         if (b) {
  93.             string t = s.substr(i,l);
  94.             m[t]++;
  95.             if (m[t]>=mx) mx = m[t], ans = t;
  96.         }
  97.     }
  98. }
  99. int main()
  100. {
  101.    
  102.     cin>>s;
  103.     n = s.size();
  104.     oc.resize(n, n+1);
  105.     map<char, int> oc2;
  106.     fr(i,n){
  107.         if (oc2.find(s[i])!=oc2.end()) oc[oc2[s[i]]] = i;
  108.         oc2[s[i]] = i;
  109.     }
  110.     oc2.clear();
  111.     fr(i,n){
  112.         oc2[s[i]]++;
  113.         if (oc2[s[i]]>mx) {
  114.             mx = oc2[s[i]];
  115.             ans = s.substr(i,1);
  116.         }
  117.     }
  118.     int l = 1;
  119.     int r = 27;
  120.     while(l<r-1){
  121.         m.clear();
  122.         int mid = l + (r-l)/2;
  123.         string t = ans;
  124.         proc(mid);
  125.         if (t!=ans) l = mid;
  126.         else r = mid;
  127.     }
  128.     cout<<ans.size()<<"\n";
  129.    
  130.     return 0;
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement