Submission #38126429


Source Code Expand

#line 2 "/home/zawatin/compro/library/src/string/rollinghash.hpp"

#include <random>
#include <iostream>
#include <algorithm>
#include <string>

namespace zawa {

template <std::size_t max_length>
class rollinghash {
public:
	using hash_type = long long;

	std::random_device seed_gen;
	std::mt19937_64 mt;
	hash_type base;
	const hash_type mod = (1ULL << 61ULL) - 1;
	std::vector<hash_type> pows;

public:

	struct info {
		std::size_t len;
		hash_type hash;
		info() : len(0), hash(0) {}
		bool operator==(const info& a) {
			return len == a.len and hash == a.hash;
		}
	};

	rollinghash() : mt(seed_gen()), pows(max_length + 1, 1LL) {
		base = std::clamp((hash_type)mt() % mod, (hash_type)1e9, mod - 1);
		for (std::size_t i = 0 ; i < max_length ; i++) {
			pows[i + 1] = ((__int128_t)pows[i] * base) % mod;
		}
	}

	template <class T>
	std::vector<info> build(const std::vector<T>& A) {
		std::vector<info> res(A.size() + 1, info());	
		for (std::size_t i = 0 ; i < A.size() ; i++) {
			res[i + 1].len = i + 1;
			res[i + 1].hash = ((__int128_t)base * res[i].hash + (__int128_t)A[i]) % mod;
		}
		return res;
	}

	std::vector<info> build(const std::string& s) {
		return build(std::vector(s.begin(), s.end()));
	}

	info hash(const std::vector<info>& H, int l, int r) {
		info res = H[r];
		res.len -= H[l].len;
		res.hash -= ((__int128_t)H[l].hash * pows[r - l]) % mod;
		if (res.hash < 0) {
			res.hash += mod;
		}
		return res;
	}

	info concate(const info& h1, const info& h2) { 
		info res;
		res.len = h1.len + h2.len;
		res.hash = ((((__int128_t)h1.hash * pows[h2.len]) % mod) + h2.hash) % mod;
		return res;
	}

};

} // namespace zawa
#line 2 "ABC284f.test.cpp"

#line 6 "ABC284f.test.cpp"

int main() {
	int n; std::cin >> n;
	std::string t; std::cin >> t;
	std::string r = t;
	std::reverse(r.begin(), r.end());
	zawa::rollinghash<1000000> roll;
	auto rht = roll.build(t);
	auto rhr = roll.build(r);
	for (int i = 0 ; i < n ; i++) {
		auto f = roll.hash(rht, 0, i);
		auto c = roll.hash(rhr, n - i, 2 * n - i);
		auto b = roll.hash(rht, i + n, 2 * n);
		auto cc = roll.concate(f, b);
		if (c == cc) {
			std::cout << r.substr(n - i, n) << std::endl;
			std::cout << i << std::endl;
			return 0;
		}
	}
	std::cout << -1 << std::endl;
}

Submission Info

Submission Time
Task F - ABCBAC
User zawatin
Language C++ (GCC 9.2.1)
Score 500
Code Size 2346 Byte
Status AC
Exec Time 336 ms
Memory 79516 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 500 / 500
Status
AC × 4
AC × 60
Set Name Test Cases
Sample 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 00_sample_03.txt
All 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 00_sample_03.txt, 01_small_00.txt, 01_small_01.txt, 01_small_02.txt, 01_small_03.txt, 01_small_04.txt, 01_small_05.txt, 01_small_06.txt, 01_small_07.txt, 02_large_00.txt, 02_large_01.txt, 02_large_02.txt, 02_large_03.txt, 02_large_04.txt, 02_large_05.txt, 02_large_06.txt, 02_large_07.txt, 02_large_08.txt, 02_large_09.txt, 02_large_10.txt, 02_large_11.txt, 02_large_12.txt, 02_large_13.txt, 02_large_14.txt, 02_large_15.txt, 03_max_00.txt, 03_max_01.txt, 03_max_02.txt, 03_max_03.txt, 03_max_04.txt, 03_max_05.txt, 03_max_06.txt, 03_max_07.txt, 04_min_00.txt, 04_min_01.txt, 05_two_types_00.txt, 05_two_types_01.txt, 05_two_types_02.txt, 05_two_types_03.txt, 06_repeat_00.txt, 06_repeat_01.txt, 06_repeat_02.txt, 06_repeat_03.txt, 06_repeat_04.txt, 07_killer_00.txt, 07_killer_01.txt, 07_killer_02.txt, 07_killer_03.txt, 07_killer_04.txt, 08_killer2_00.txt, 08_killer2_01.txt, 08_killer2_02.txt, 09_killer3_00.txt, 09_killer3_01.txt, 09_killer3_02.txt, 09_killer3_03.txt, 09_killer3_04.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 47 ms 11060 KiB
00_sample_01.txt AC 45 ms 10984 KiB
00_sample_02.txt AC 44 ms 11100 KiB
00_sample_03.txt AC 43 ms 11160 KiB
01_small_00.txt AC 45 ms 11168 KiB
01_small_01.txt AC 45 ms 10988 KiB
01_small_02.txt AC 42 ms 11104 KiB
01_small_03.txt AC 43 ms 11168 KiB
01_small_04.txt AC 45 ms 11012 KiB
01_small_05.txt AC 41 ms 11040 KiB
01_small_06.txt AC 38 ms 11012 KiB
01_small_07.txt AC 49 ms 11040 KiB
02_large_00.txt AC 312 ms 79512 KiB
02_large_01.txt AC 228 ms 79440 KiB
02_large_02.txt AC 327 ms 79420 KiB
02_large_03.txt AC 229 ms 79396 KiB
02_large_04.txt AC 275 ms 79412 KiB
02_large_05.txt AC 262 ms 79428 KiB
02_large_06.txt AC 333 ms 79340 KiB
02_large_07.txt AC 334 ms 79412 KiB
02_large_08.txt AC 325 ms 79408 KiB
02_large_09.txt AC 251 ms 79516 KiB
02_large_10.txt AC 322 ms 79408 KiB
02_large_11.txt AC 270 ms 79316 KiB
02_large_12.txt AC 315 ms 79404 KiB
02_large_13.txt AC 232 ms 79320 KiB
02_large_14.txt AC 333 ms 79444 KiB
02_large_15.txt AC 335 ms 79372 KiB
03_max_00.txt AC 256 ms 79416 KiB
03_max_01.txt AC 335 ms 79444 KiB
03_max_02.txt AC 233 ms 79324 KiB
03_max_03.txt AC 335 ms 79412 KiB
03_max_04.txt AC 320 ms 79328 KiB
03_max_05.txt AC 333 ms 79508 KiB
03_max_06.txt AC 332 ms 79324 KiB
03_max_07.txt AC 332 ms 79444 KiB
04_min_00.txt AC 48 ms 11104 KiB
04_min_01.txt AC 42 ms 11116 KiB
05_two_types_00.txt AC 288 ms 79280 KiB
05_two_types_01.txt AC 332 ms 79396 KiB
05_two_types_02.txt AC 228 ms 79432 KiB
05_two_types_03.txt AC 333 ms 79452 KiB
06_repeat_00.txt AC 226 ms 79412 KiB
06_repeat_01.txt AC 228 ms 79280 KiB
06_repeat_02.txt AC 226 ms 79516 KiB
06_repeat_03.txt AC 224 ms 79508 KiB
06_repeat_04.txt AC 225 ms 79396 KiB
07_killer_00.txt AC 333 ms 79320 KiB
07_killer_01.txt AC 333 ms 79516 KiB
07_killer_02.txt AC 336 ms 79440 KiB
07_killer_03.txt AC 333 ms 79508 KiB
07_killer_04.txt AC 334 ms 79408 KiB
08_killer2_00.txt AC 333 ms 79456 KiB
08_killer2_01.txt AC 333 ms 79512 KiB
08_killer2_02.txt AC 333 ms 79344 KiB
09_killer3_00.txt AC 334 ms 79416 KiB
09_killer3_01.txt AC 335 ms 79280 KiB
09_killer3_02.txt AC 227 ms 79452 KiB
09_killer3_03.txt AC 226 ms 79508 KiB
09_killer3_04.txt AC 280 ms 79396 KiB