Submission #100842

#TimeUsernameProblemLanguageResultExecution timeMemory
100842dalgerokSavez (COCI15_savez)C++17
0 / 120
172 ms65528 KiB
#include<bits/stdc++.h> #define int long long using namespace std; const int N = 505, M = 2e6 + 5, MOD1 = 1e9 + 1337, MOD2 = 999888777; int n, p1[M], p2[M], dp[N], a[N], ans; vector < int > h1[N], h2[N]; bool used[N]; vector < int > g[N]; string s[N]; map < pair < int, int >, int > ss; map < pair < int, int >, vector < int > > q; inline pair < int, int > get(int i, int l, int r){ return make_pair( (h1[i][r] - h1[i][l - 1] + MOD1) % MOD1 * p1[M - r] % MOD1, (h2[i][r] - h2[i][l - 1] + MOD2) % MOD2 * p2[M - r] % MOD2 ); } void dfs(int v){ used[v] = true; for(int to : g[v]){ if(!used[to]){ dfs(to); } dp[v] = max(dp[v], dp[to]); } dp[v] += a[v]; ans = max(ans, dp[v]); } main(){ ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); p1[0] = p2[0] = 1; for(int i = 1; i < M; i++){ p1[i] = p1[i - 1] * 59 % MOD1; p2[i] = p2[i - 1] * 45 % MOD2; } cin >> n; for(int i = 1; i <= n; i++){ cin >> s[i]; int sz = (int)s[i].size(); h1[i].resize(sz + 2); h2[i].resize(sz + 2); for(int j = 1; j <= sz; j++){ h1[i][j] = (h1[i][j - 1] + 1LL * s[i][j - 1] * p1[j]) % MOD1; h2[i][j] = (h2[i][j - 1] + 1LL * s[i][j - 1] * p2[j]) % MOD2; } int hh1 = h1[i][sz] * p1[M - sz] % MOD1, hh2 = h2[i][sz] * p2[M - sz] % MOD2; ///cout << hh1 << " " << hh2 << "\n"; if(ss[make_pair(hh1, hh2)]){ a[ss[make_pair(hh1, hh2)]] += 1; } else{ ss[make_pair(hh1, hh2)] = i; a[i] += 1; q[make_pair(hh1, hh2)].push_back(i); } } for(int i = 1; i <= n; i++){ int sz = (int)s[i].size(); for(int j = 1; j <= sz; j++){ auto it1 = get(i, 1, j), it2 = get(i, sz - j + 1, sz); if(it1 == it2){ ///cout << i << " " << j << " " << it1.first << " " << it1.second << "\n"; for(auto it3 : q[it1]){ if(it3 != i){ g[it3].push_back(i); } } } } } for(int i = 1; i <= n; i++){ if(!used[i]){ dfs(i); } } cout << ans; }

Compilation message (stderr)

savez.cpp:36:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
 main(){
      ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...