Cod sursa(job #3356642)

Utilizator lefterache_stefanLefterache Stefan lefterache_stefan Data 2 iunie 2026 22:08:12
Problema Trie Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.02 kb
#include <fstream>
#include <vector>

using namespace std;

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

const int MOD = 1e9 + 7;
const char DIFF = 1 - 'a';
const int P = 53;

int t;
int index[11];
vector<long long> h[10001];

[[nodiscard]] static inline bool check(int x) {
	x -= 1;
	if (x >= h[index[1]].size()) {
		return false;
	}
	for (int i = 2; i <= t; i++) {
		if (x >= h[index[i]].size() || h[index[1]][x] != h[index[i]][x]) {
			return false;
		}
	}
	return true;
}

void solve() {
	int st = 1, dr = 6767;
	int rasp = 0;

	while (st <= dr) {
		const int m = (st + dr) / 2;
		if (check(m)) {
			rasp = m;
			st = m + 1;
		} else {
			dr = m - 1;
		}
	}

	cout << rasp << '\n';
}

int main() {
	int n, q;
	cin >> n >> q;
	for (int i = 1; i <= n; i++) {
		string s;
		cin >> s;
		h[i].resize(s.size());
		h[i][0] = s[0] + DIFF;
		for (int j = 1; j < s.size(); j++) {
			h[i][j] = (h[i][j - 1] * P + (s[j] + DIFF)) % MOD;
		}
	}

	while (q--) {
		cin >> t;
		for (int i = 1; i <= t; i++) {
			cin >> index[i];
		}
		solve();
	}
}