Cod sursa(job #2942471)

Utilizator AlexNicuNicu Alexandru AlexNicu Data 19 noiembrie 2022 18:35:06
Problema A+B Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.84 kb
#include <fstream>
#include <string>
#include <vector>
#include <unordered_map>
#include <cmath>

using namespace std;

ifstream cin ("subsecvente2.in");
ofstream cout ("subsecvente2.out");

unordered_map<long long, int> frecv;

int main()
{
    int n, maxx;
    cin >> n;
    vector<string> s(n);
    for ( int i = 0; i < n; i++ ) {
      cin >> s[i];
    }
    for ( int len = 1; len <= min(60, (int)s[0].size()); len++ ) {
      long long hash = 0;
      for ( int st = 0; st < len; st++ ) {
          hash = hash * 2 + (s[0][st] == 'a' ? 0 : 1);
      }
      frecv[hash]++;
      cout << len << ": ";
      cout << hash << ", ";
      for ( int st = 1; st < s[0].size() - len + 1; st++ ) {
        int dr = st + len - 1;
        hash = hash * 2 + (s[0][dr] == 'a' ? 0 : 1);
        hash = hash - ( 1 << len ) * (s[0][st - 1] == 'a' ? 0 : 1);
        frecv[hash++];
        cout << hash << ", ";
      }
      cout << "\n";
    }
    for ( int i = 1; i < n; i++ ) {
      for ( int len = 1; len <= min(60, (int)s[i].size()); len++ ) {
        long long hash = 0;
        for ( int st = 0; st < len; st++ ) {
          hash = hash * 2 + (s[i][st] == 'a' ? 0 : 1);
        }
        auto it = frecv.find(hash);
        if ( it != frecv.end() )
            frecv[hash]++;
        for ( int st = 1; st < s[i].size() - len + 1; st++ ) {
          int dr = st + len - 1;
          hash = hash * 2 + (s[i][dr] == 'a' ? 0 : 1);
          hash = hash - ( 1 << len ) * (s[i][st - 1] == 'a' ? 0 : 1);
          it = frecv.find(hash);
          if ( it != frecv.end() )
            frecv[hash]++;
        }
      }
    }
    maxx = -1;
    for ( pair<long long, int> elem : frecv ) {
      if ( elem.second == n ) {
        maxx = max ( maxx, (int)log2(elem.first) );
      }
    }
    cout << maxx << "\n";
    return 0;
}