Submission #36199618


Source Code Expand

#include <bits/stdc++.h>
namespace // to fold that junk code
{
#define filein(x) {freopen(x".in", "r", stdin);}
#define file(x) {freopen(x".in", "r", stdin); freopen(x".out", "w", stdout);}
#define files(x) {freopen(x".in", "r", stdin); freopen(x".ans", "w", stdout);}
#define mem(x, a) memset(x, a, sizeof x);
#define cls(x) mem(x, 0)
using namespace std;
#define cT const T&
template<typename T>
inline T chkmin(T& x, cT y){if (x > y) x = y; return x;}
template<typename T>
inline T chkmax(T& x, cT y){if (x < y) x = y; return x;}
template <typename T>
inline bool inrange(cT x, cT l, cT r){return (l <= x) && (x <= r);}
template <typename T>
inline bool inrange(cT l, cT r, cT L, cT R){return (L <= l) && (r <= R);}
template <typename T>
inline bool separate(cT l, cT r, cT L, cT R){return (R < l) || (L > r);}
template <typename T>
inline T sqr(cT _){return _ * _;}
template <typename T>
inline T read(){T x; cin >> x; return x;}
template <typename T1, typename T2>
inline pair<T1, T2> read(){T1 x; T2 y; cin >> x >> y; return make_pair(x, y);}
template <typename T>
inline void clear(T& q){typename remove_reference<decltype(q)> :: type _; swap(q, _);} // remove_reference_t is since C++14
#undef cT
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
template <typename T>
using pr = pair<T, T>;
template <typename T>
using maxheap = priority_queue<T>;
template <typename T>
using minheap = priority_queue<T, vector<T>, greater<T>>;
template <typename T1, typename T2 = unsigned>
using umap = unordered_map<T1, T2>;
template <typename T1, typename T2 = unsigned>
using uset = unordered_set<T1, T2>;
typedef pr<int> pii;
typedef pr<ll> pll;
typedef pr<db> pdd;
typedef complex<double> cp;
typedef vector<int> vi;
inline void YN(bool x){puts(x ? "Yes" : "No");}
}
const int N = 222222, base1 = 1331, base2 = 131, P = 1e9 + 87;
int n, p, q, a[N], x[N], y[N], pb1[N], pb2[N], dist[N];
inline void initHash(int n)
{
	pb1[0] = pb2[0] = 1;
	for (int i=1; i<=n; i++) pb1[i] = 1ll * pb1[i-1] * base1 % P;
	for (int i=1; i<=n; i++) pb2[i] = 1ll * pb2[i-1] * base2 % P;
}
struct Hasher
{
	int ph1[N], ph2[N];
	template <typename _Iter>
	inline void init(_Iter beg, _Iter end)
	{
		ph1[1] = ph2[1] = beg[0]; int n = end - beg;
		for (int i=1; i<n; i++) ph1[i+1] = (1ll * ph1[i] * base1 % P + beg[i]) % P;
		for (int i=1; i<n; i++) ph2[i+1] = (1ll * ph2[i] * base2 % P + beg[i]) % P;
	}
	pii hash(int l, int r){return make_pair((ph1[r] - 1ll * ph1[l-1] * pb1[r-l+1] % P + P) % P, (ph2[r] - 1ll * ph2[l-1] * pb2[r-l+1] % P + P) % P);}
}A, B;
inline bool check(int len)
{
	set<pii> S;
	for (int i=1; i<=p-len+1; i++) S.insert(A.hash(i, i+len-1));
	for (int i=1; i<=q-len+1; i++)
		if (S.find(B.hash(i, i+len-1)) != S.end()) return true;
	return false;
}
vector<int> g[N];
inline void addedge(int u, int v){g[u].emplace_back(v);}
inline void ade(int u, int v){addedge(u, v); addedge(v, u);}
inline int bfs()
{
	memset(dist, 0x3f, sizeof dist);
	queue<int> Q;
	for (int i=1; i<=p; i++){dist[x[i]] = 0; Q.push(x[i]);}
	for (int i=2; i<=n; i++) ade(a[i], a[i-1]);
	while (!Q.empty())
	{
		int u = Q.front(); Q.pop();
		for (int v : g[u])
			if (dist[v] > dist[u] + 1){dist[v] = dist[u] + 1; Q.push(v);}
	}
	int ans = dist[0];
	for (int i=1; i<=q; i++) chkmin(ans, dist[y[i]]);
	return p + q + 2 * ans - 2;
}
int main()
{
	scanf("%d", &n); initHash(n);
	for (int i=1; i<=n; i++) scanf("%d", a+i);
	scanf("%d", &p);
	for (int i=1; i<=p; i++) scanf("%d", x+i);
	scanf("%d", &q);
	for (int i=1; i<=q; i++) scanf("%d", y+i);
	A.init(x+1, x+1+p); B.init(y+1, y+1+q);
	int l = 1, r = min(p, q);
	while (l <= r)
	{
		int mid = (l + r) >> 1;
		if (check(mid)) l = mid + 1;
		else r = mid - 1;
	}
	int lcs = l - 1;
	if (lcs) printf("%d\n", p + q - 2 * lcs);
	else printf("%d\n", bfs());
	return 0;
}

Submission Info

Submission Time
Task E - Keep Being Substring
User Jijidawang
Language C++ (GCC 9.2.1)
Score 700
Code Size 3939 Byte
Status AC
Exec Time 825 ms
Memory 21160 KiB

Compile Error

./Main.cpp: In function ‘int main()’:
./Main.cpp:100:7: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
  100 |  scanf("%d", &n); initHash(n);
      |  ~~~~~^~~~~~~~~~
./Main.cpp:101:32: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
  101 |  for (int i=1; i<=n; i++) scanf("%d", a+i);
      |                           ~~~~~^~~~~~~~~~~
./Main.cpp:102:7: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
  102 |  scanf("%d", &p);
      |  ~~~~~^~~~~~~~~~
./Main.cpp:103:32: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
  103 |  for (int i=1; i<=p; i++) scanf("%d", x+i);
      |                           ~~~~~^~~~~~~~~~~
./Main.cpp:104:7: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
  104 |  scanf("%d", &q);
      |  ~~~~~^~~~~~~~~~
./Main.cpp:105:32: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
  105 |  for (int i=1; i<=q; i++) scanf("%d", y+i);
      |                           ~~~~~^~~~~~~~~~~

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 700 / 700
Status
AC × 2
AC × 62
Set Name Test Cases
Sample example0.txt, example1.txt
All 000.txt, 001.txt, 002.txt, 003.txt, 004.txt, 005.txt, 006.txt, 007.txt, 008.txt, 009.txt, 010.txt, 011.txt, 012.txt, 013.txt, 014.txt, 015.txt, 016.txt, 017.txt, 018.txt, 019.txt, 020.txt, 021.txt, 022.txt, 023.txt, 024.txt, 025.txt, 026.txt, 027.txt, 028.txt, 029.txt, 030.txt, 031.txt, 032.txt, 033.txt, 034.txt, 035.txt, 036.txt, 037.txt, 038.txt, 039.txt, 040.txt, 041.txt, 042.txt, 043.txt, 044.txt, 045.txt, 046.txt, 047.txt, 048.txt, 049.txt, 050.txt, 051.txt, 052.txt, 053.txt, 054.txt, 055.txt, 056.txt, 057.txt, 058.txt, 059.txt, example0.txt, example1.txt
Case Name Status Exec Time Memory
000.txt AC 9 ms 8748 KiB
001.txt AC 125 ms 20544 KiB
002.txt AC 37 ms 11504 KiB
003.txt AC 665 ms 21160 KiB
004.txt AC 42 ms 12104 KiB
005.txt AC 42 ms 13452 KiB
006.txt AC 825 ms 18196 KiB
007.txt AC 816 ms 18184 KiB
008.txt AC 821 ms 18112 KiB
009.txt AC 818 ms 18160 KiB
010.txt AC 817 ms 18220 KiB
011.txt AC 467 ms 16932 KiB
012.txt AC 657 ms 17524 KiB
013.txt AC 780 ms 18148 KiB
014.txt AC 782 ms 18108 KiB
015.txt AC 750 ms 18040 KiB
016.txt AC 769 ms 18104 KiB
017.txt AC 744 ms 18108 KiB
018.txt AC 777 ms 18112 KiB
019.txt AC 62 ms 18268 KiB
020.txt AC 63 ms 18304 KiB
021.txt AC 67 ms 18288 KiB
022.txt AC 61 ms 18204 KiB
023.txt AC 63 ms 18212 KiB
024.txt AC 64 ms 18224 KiB
025.txt AC 63 ms 18336 KiB
026.txt AC 33 ms 14608 KiB
027.txt AC 30 ms 14628 KiB
028.txt AC 33 ms 14552 KiB
029.txt AC 40 ms 15888 KiB
030.txt AC 38 ms 15876 KiB
031.txt AC 37 ms 15876 KiB
032.txt AC 30 ms 13372 KiB
033.txt AC 8 ms 8920 KiB
034.txt AC 133 ms 12120 KiB
035.txt AC 13 ms 9076 KiB
036.txt AC 111 ms 13140 KiB
037.txt AC 174 ms 13256 KiB
038.txt AC 381 ms 14304 KiB
039.txt AC 82 ms 11884 KiB
040.txt AC 208 ms 13324 KiB
041.txt AC 138 ms 12896 KiB
042.txt AC 162 ms 13140 KiB
043.txt AC 56 ms 16412 KiB
044.txt AC 48 ms 15268 KiB
045.txt AC 43 ms 14960 KiB
046.txt AC 39 ms 14904 KiB
047.txt AC 40 ms 14624 KiB
048.txt AC 35 ms 14912 KiB
049.txt AC 35 ms 14336 KiB
050.txt AC 34 ms 14720 KiB
051.txt AC 35 ms 14712 KiB
052.txt AC 33 ms 14324 KiB
053.txt AC 35 ms 14544 KiB
054.txt AC 35 ms 14928 KiB
055.txt AC 31 ms 14668 KiB
056.txt AC 34 ms 14324 KiB
057.txt AC 34 ms 14108 KiB
058.txt AC 81 ms 17052 KiB
059.txt AC 50 ms 13036 KiB
example0.txt AC 9 ms 8748 KiB
example1.txt AC 7 ms 8816 KiB