Submission #209877

# Submission time Handle Problem Language Result Execution time Memory
209877 2020-03-15T20:11:29 Z ZwariowanyMarcin Savez (COCI15_savez) C++14
120 / 120
248 ms 50236 KB
#include <bits/stdc++.h>
#define LL long long
#define LD long double
#define pb push_back
#define mp make_pair
#define ss(x) (int) x.size()
#define fi first
#define se second
#define cat(x) cerr << #x << " = " << x << endl
#define rep2(i, j, n) for (LL i = j; i <= n; ++i)
#define rep(i, j, n) for (int i = j; i <= n; ++i)
#define per(i, j, n) for (int i = n; j <= i; --i)
#define boost cin.tie(0);ios_base::sync_with_stdio(0);
#define all(x) x.begin(), x.end()

using namespace std;

const int N = 2e6 + 12;
const int MOD1 = 1e9 + 7;
const int MOD2 = 1e9 + 9;
const int P = 31;

int n;
vector <string> v;
char s[N];
string t;

map <pair<int, int>, int> dp;

int h1[N];
int h2[N];
int p1[N];
int p2[N];

void prep(string x) {
	x = '#' + x;
	int n = ss(x) - 1;
	rep(i, 1, n) {
		h1[i] = ((LL) h1[i - 1] * P + (x[i] - 'A' + 1)) % MOD1;
		h2[i] = ((LL) h2[i - 1] * P + (x[i] - 'A' + 1)) % MOD2;
	}
}

pair <int, int> hh(int l, int r) {
	pair <int, int> x;
	x.fi = (h1[r] - (LL) h1[l - 1] * p1[r - l + 1] % MOD1 + MOD1) % MOD1;
	x.se = (h2[r] - (LL) h2[l - 1] * p2[r - l + 1] % MOD2 + MOD2) % MOD2;
	return x;
}

int main() {
	p1[0] = p2[0] = 1;
	rep(i, 1, N - 1) {
		p1[i] = (LL) p1[i - 1] * P % MOD1;
		p2[i] = (LL) p2[i - 1] * P % MOD2;
	}
		
	scanf ("%d", &n);
	rep(i, 1, n) {
		scanf ("%s", s + 1);
		int n = strlen(s + 1);
		t = "";
		rep(j, 1, n) t.pb(s[j]);
		v.pb(t);
	}
	int best = 0;
	for (auto it : v) {
		prep(it);
		int n = ss(it);
		pair<int, int> ja = hh(1, n);
		int x = 1;
		rep(i, 1, n) {
			if (hh(1, i) == hh(n - i + 1, n)) {
				x = max(x, 1 + dp[hh(1, i)]);
			}
		}
		dp[ja] = x;
		best = max(best, dp[ja]);
	}
	printf ("%d\n", best);
		
		
	
		
	return 0;
}

Compilation message

savez.cpp: In function 'int main()':
savez.cpp:58:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf ("%d", &n);
  ~~~~~~^~~~~~~~~~
savez.cpp:60:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf ("%s", s + 1);
   ~~~~~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 24 ms 15992 KB Output is correct
2 Correct 24 ms 15992 KB Output is correct
3 Correct 24 ms 15992 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 24 ms 15992 KB Output is correct
2 Correct 24 ms 15992 KB Output is correct
3 Correct 27 ms 16120 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 147 ms 17224 KB Output is correct
2 Correct 144 ms 17144 KB Output is correct
3 Correct 159 ms 18168 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 27 ms 16120 KB Output is correct
2 Correct 83 ms 17160 KB Output is correct
3 Correct 102 ms 17272 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 75 ms 17396 KB Output is correct
2 Correct 74 ms 18420 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 81 ms 18032 KB Output is correct
2 Correct 78 ms 18792 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 85 ms 19052 KB Output is correct
2 Correct 81 ms 19816 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 91 ms 20964 KB Output is correct
2 Correct 90 ms 21860 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 122 ms 24284 KB Output is correct
2 Correct 118 ms 25052 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 162 ms 32592 KB Output is correct
2 Correct 159 ms 33616 KB Output is correct
3 Correct 248 ms 50236 KB Output is correct