結果

問題 No.515 典型LCP
ユーザー tansunogontansunogon
提出日時 2017-05-06 07:35:38
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 114 ms / 1,000 ms
コード長 3,025 bytes
コンパイル時間 2,207 ms
コンパイル使用メモリ 177,536 KB
実行使用メモリ 14,932 KB
最終ジャッジ日時 2023-10-12 14:44:48
合計ジャッジ時間 3,460 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 108 ms
14,932 KB
testcase_01 AC 114 ms
14,876 KB
testcase_02 AC 44 ms
8,240 KB
testcase_03 AC 3 ms
7,528 KB
testcase_04 AC 4 ms
7,292 KB
testcase_05 AC 22 ms
8,088 KB
testcase_06 AC 22 ms
8,092 KB
testcase_07 AC 23 ms
8,224 KB
testcase_08 AC 31 ms
8,208 KB
testcase_09 AC 25 ms
8,272 KB
testcase_10 AC 25 ms
8,040 KB
testcase_11 AC 25 ms
8,028 KB
testcase_12 AC 25 ms
8,028 KB
testcase_13 AC 23 ms
8,136 KB
testcase_14 AC 7 ms
8,088 KB
testcase_15 AC 23 ms
8,120 KB
testcase_16 AC 23 ms
8,056 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
using namespace std;

struct iostream_init_struct
{ 
	iostream_init_struct()
	{
		std::cin.tie(0);
		std::cin.sync_with_stdio(false);
	}
} iostream_init;

// thank you, tookunn
// http://tookunn.hatenablog.com/entry/2016/07/13/211148
#include <vector>
#include <algorithm>
#include <bits/stdc++.h>
class RMQ_SparseTable
{
public:
	// table[k][i] means index of minimum value in [i, i + 2^k)
	std::vector<std::vector<int>> table;
	int * values;

	RMQ_SparseTable(int* A, size_t N)
	{
		table = std::vector<std::vector<int>>(log(N) + 1, vector<int>(N));
		values = A;

		for (int i = 0; i < N; ++i)
		{
			table[0][i] = i;
		}

		for (int k = 1; (1 << k) <= N; k++)
		{
			for (int i = 0; i + (1 << k) <= N; ++i)
			{
				int first = table[k - 1][i];
				int second = table[k - 1][i + (1 << (k - 1))];
				if (A[first] < A[second])
				{
					table[k][i] = first;
				}
				else
				{
					table[k][i] = second;
				}
			}
		}
	}

	// returns min value in [s, t)
	inline int getMin(int s, int t)
	{
		int range = t - s;
		int k = log(range);

		int value1 = values[table[k][s]];
		int value2 = values[table[k][t - (1 << k)]];
		return min(value1, value2);
	}

	inline static int log(int n)
	{
		return std::__lg(n);
	}
};

#include <string>
#include <algorithm>
int N;
pair<string, int> s[100000];
int M;
long long x, d;
int i, j;

int index_table[100000];
int LCP_table[100000];

// same as pekempey's submission
// http://yukicoder.me/submissions/172137
int di, dj, ii, jj;
void next_init()
{
	di = d / (N - 1);
	dj = d % (N - 1);
	ii = x / (N - 1);
	jj = x % (N - 1);
	
	i = ii;
	j = jj;
	if (i <= j)
	{
		++j;
	}
}
inline void next()
{
	jj += dj;
	if (jj >= N - 1)
	{
		jj -= N - 1;
		++ii;
	}

	ii += di;
	if (ii >= N)
	{
		ii -= N;
	}

	i = ii;
	j = jj;
	if (i <= j)
	{
		++j;
	}
}

int LCP(const char* s1, const char* s2, size_t min_length)
{
	int ret = 0;

	//const uint64_t* s64i = reinterpret_cast<const uint64_t*>(s1);
	//const uint64_t* s64j = reinterpret_cast<const uint64_t*>(s2);
	//while (ret < min_length / 8)
	//{
	//	if (s64i[ret] == s64j[ret])
	//	{
	//		++ret;
	//	}
	//	else
	//	{
	//		break;
	//	}
	//}
	//ret *= 8;

	while (ret < min_length)
	{
		if (s1[ret] == s2[ret])
		{
			++ret;
		}
		else
		{
			break;
		}
	}

	return ret;
}

int main()
{
	// get input
	cin >> N;
	for (int i = 0; i < N; ++i)
	{
		cin >> s[i].first;
		s[i].second = i;
	}
	cin >> M >> x >> d;

	// sort
	sort(s, s + N);
	// reindex
	for (int i = 0; i < N; ++i)
	{
		index_table[s[i].second] = i;
	}
	// calc LCP
	for (int i = 0; i < N - 1; ++i)
	{
		string& s1 = s[i].first;
		string& s2 = s[i + 1].first;
		LCP_table[i] = LCP(s1.c_str(), s2.c_str(), min(s1.size(), s2.size()));
	}
	// make Sparse Table
	RMQ_SparseTable sparse_table(LCP_table, N - 1);

	// calc ans
	long long sum = 0;
	next_init();
	for (int indx = 0; indx < M; ++indx)
	{
		int i_ = index_table[i];
		int j_ = index_table[j];
		if (i_ > j_)
			std::swap(i_, j_);
		sum += sparse_table.getMin(i_, j_);
		next();
	}

	cout << sum << endl;
}
0