Submission #1476883


Source Code Expand

#include "bits/stdc++.h"
using namespace std;
#ifdef _DEBUG
#include "dump.hpp"
#else
#define dump(...)
#endif

//#define int long long
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define all(c) begin(c),end(c)
const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9) + 7;
template<class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; }
template<class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return true; } return false; }

signed main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	int N, K; cin >> N >> K;
	vector<bool> a(N);
	vector<int> A(K); rep(i, 0, K) {
		cin >> A[i];
		A[i]--;
		a[A[i]] = true;
	}
	vector<string> S(N); rep(i, 0, N) {
		cin >> S[i];
	}
	auto f = [&](int x) {
		bool ok = true;
		string s;
		rep(i, 0, N) {
			if (a[i]) {
				if (S[i].size() < x) {
					return true;
				}
				if (s.size() == 0 && x != 0)
					s = S[i].substr(0, x);
			}
		}
		rep(i, 0, N) {
			if (a[i]) {
				if (S[i].substr(0, x) != s) {
					ok = false;
					break;
				}
			}
			else {
				if (S[i].size() < x)continue;
				if (S[i].substr(0, x) == s) {
					ok = false;
					break;
				}
			}
		}
		return ok;
	};
	auto binarySearch = [&](int ng, int ok) {
		if (f(ng))return ng;
		while (ng + 1 < ok) {
			int m = (ng + ok) / 2;
			if (f(m))
				ok = m;
			else
				ng = m;
		}
		return ok;
	};
	int c = binarySearch(0, 100010);
	rep(i, 0, N) {
		if (a[i] && S[i].size() < c) {
			cout << -1 << endl;
			return 0;
		}
	}
	cout << S[A[0]].substr(0, c) << endl;
	return 0;
}

Submission Info

Submission Time
Task C - 検索
User minaminao
Language C++14 (GCC 5.4.1)
Score 0
Code Size 1707 Byte
Status WA
Exec Time 28 ms
Memory 6144 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 400
Status
AC × 3
AC × 29
WA × 1
Set Name Test Cases
Sample sample1.txt, sample2.txt, sample3.txt
All sample1.txt, sample2.txt, sample3.txt, in1.txt, in10.txt, in11.txt, in12.txt, in13.txt, in14.txt, in15.txt, in16.txt, in17.txt, in18.txt, in19.txt, in2.txt, in20.txt, in3.txt, in4.txt, in5.txt, in6.txt, in7.txt, in8.txt, in9.txt, long.txt, long2.txt, sample1.txt, sample2.txt, sample3.txt, long.txt, long2.txt
Case Name Status Exec Time Memory
in1.txt AC 2 ms 384 KB
in10.txt AC 2 ms 384 KB
in11.txt AC 4 ms 896 KB
in12.txt AC 1 ms 384 KB
in13.txt AC 2 ms 512 KB
in14.txt WA 2 ms 384 KB
in15.txt AC 1 ms 384 KB
in16.txt AC 1 ms 256 KB
in17.txt AC 2 ms 384 KB
in18.txt AC 2 ms 384 KB
in19.txt AC 3 ms 768 KB
in2.txt AC 4 ms 896 KB
in20.txt AC 2 ms 512 KB
in3.txt AC 20 ms 5760 KB
in4.txt AC 28 ms 6144 KB
in5.txt AC 1 ms 384 KB
in6.txt AC 1 ms 256 KB
in7.txt AC 1 ms 384 KB
in8.txt AC 4 ms 896 KB
in9.txt AC 28 ms 6144 KB
long.txt AC 2 ms 512 KB
long2.txt AC 8 ms 2304 KB
sample1.txt AC 1 ms 256 KB
sample2.txt AC 1 ms 256 KB
sample3.txt AC 1 ms 256 KB