QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#72301#3844. LCS Spanning Treeneko_nyaa#AC ✓3419ms422916kbC++234.2kb2023-01-15 12:56:552023-01-15 12:56:59

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-01-15 12:56:59]
  • 评测
  • 测评结果:AC
  • 用时:3419ms
  • 内存:422916kb
  • [2023-01-15 12:56:55]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

struct DSU {
	vector<int> P, S;
	DSU(int n) {
		P.resize(n+1);
		S.resize(n+1, 1);
		for (int i = 1; i <= n; i++) P[i] = i;
	}
	
	int find(int x) {
		if (x == P[x]) return x;
		return P[x] = find(P[x]);
	}
	
	bool merge(int a, int b) {
		a = find(a); b = find(b);
		if (a == b) return 0;
		if (S[a] < S[b]) swap(a, b);
		
		S[a] += S[b];
		P[b] = a;
		return 1;
	}
	
	int same(int a, int b) {
		return find(a) == find(b);
	}
};

struct SuffixArray {
	vi sa, lcp;
	SuffixArray(string& s, int lim=256) { // or basic_string<int>
		int n = sz(s) + 1, k = 0, a, b;
		vi x(all(s)+1), y(n), ws(max(n, lim)), rank(n);
		sa = lcp = y, iota(all(sa), 0);
		for (int j = 0, p = 0; p < n; j = max(1, j * 2), lim = p) {
			p = j, iota(all(y), n - j);
			rep(i,0,n) if (sa[i] >= j) y[p++] = sa[i] - j;
			fill(all(ws), 0);
			rep(i,0,n) ws[x[i]]++;
			rep(i,1,lim) ws[i] += ws[i - 1];
			for (int i = n; i--;) sa[--ws[x[y[i]]]] = y[i];
			swap(x, y), p = 1, x[sa[0]] = 0;
			rep(i,1,n) a = sa[i - 1], b = sa[i], x[b] =
				(y[a] == y[b] && y[a + j] == y[b + j]) ? p - 1 : p++;
		}
		rep(i,1,n) rank[sa[i]] = i;
		for (int i = 0, j; i < n - 1; lcp[rank[i++]] = k)
			for (k && k--, j = sa[rank[i] - 1];
					s[i + k] == s[j + k]; k++);
	}
};

struct H {
	typedef uint64_t ull;
	ull x; H(ull x=0) : x(x) {}
#define OP(O,A,B) H operator O(H o) { ull r = x; asm \
	(A "addq %%rdx, %0\n adcq $0,%0" : "+a"(r) : B); return r; }
	OP(+,,"d"(o.x)) OP(*,"mul %1\n", "r"(o.x) : "rdx")
	H operator-(H o) { return *this + ~o.x; }
	ull get() const { return x + !~x; }
	bool operator==(H o) const { return get() == o.get(); }
	bool operator<(H o) const { return get() < o.get(); }
};
static const H C = (ll)1e11+3; // (order ~ 3e9; random also ok)

struct HashInterval {
	vector<H> ha, pw;
	HashInterval(string& str) : ha(sz(str)+1), pw(ha) {
		pw[0] = 1;
		rep(i,0,sz(str))
			ha[i+1] = ha[i] * C + str[i],
			pw[i+1] = pw[i] * C;
	}
	H hashInterval(int a, int b) { // hash [a, b)
		return ha[b] - ha[a] * pw[b - a];
	}
};

H hashString(string& s){H h{}; for(char c:s) h=h*C+c;return h;}

vector<string> s;
vector<HashInterval> hs;

int lcp(int i1, int j1, int i2, int j2) {
	int lo = 0, hi = min(s[i1].size() - j1, s[i2].size() - j2);
	while (lo < hi) {
		int mid = (lo + hi + 1)/2;
		if (hs[i1].hashInterval(j1, j1+mid) == hs[i2].hashInterval(j2, j2+mid)) {
			lo = mid;
		} else {
			hi = mid - 1;
		}
	}
	return lo;
}

bool cmp(pair<int, int> p1, pair<int, int> p2) {
	auto [i1, j1] = p1;
	auto [i2, j2] = p2;

	int lc = lcp(i1, j1, i2, j2);
	char c1 = '0', c2 = '0';
	if (j1 + lc != s[i1].size()) {
		c1 = s[i1][j1 + lc];
	}
	if (j2 + lc != s[i2].size()) {
		c2 = s[i2][j2 + lc];
	}
	return c1 < c2;
}

signed main() {
	ios::sync_with_stdio(0); cin.tie(0);

	int n; cin >> n;
	s.resize(n);
	for (int i = 0; i < n; i++) {
		cin >> s[i];
		hs.push_back(HashInterval(s[i]));
	}

	vector<pair<int, int>> sa;
	
	// let's try something new
	string lg; vector<int> lens;
	for (int i = 0; i < n; i++) {
		lg += s[i]; lg += "#";
		if (lens.empty()) {
			lens.push_back(s[i].size() + 1);
		} else {
			lens.push_back(lens.back() + s[i].size() + 1);
		}
	}
	SuffixArray saf(lg);
	for (int i = 1; i < saf.sa.size(); i++) {
		int id = upper_bound(lens.begin(), lens.end(), saf.sa[i]) - lens.begin();
		int ii = id;
		int jj = (ii == 0 ? saf.sa[i] : (saf.sa[i] - lens[ii-1]));
		if (jj == s[ii].size()) continue;

		sa.emplace_back(ii, jj);
	}
	// done

	/*
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < s[i].size(); j++) {
			sa.emplace_back(i, j);
		}
	}
	sort(sa.begin(), sa.end(), cmp);
	*/

	vector<tuple<int, int, int>> ed;
	for (int i = 1; i < sa.size(); i++) {
		auto [i1, j1] = sa[i];
		auto [i2, j2] = sa[i-1];

		int w = lcp(i1, j1, i2, j2);
		ed.emplace_back(-w, i1, i2);
	}
	sort(ed.begin(), ed.end());

	DSU d(n);
	int ans = 0;
	for (auto [w, u, v]: ed) {
		if (d.merge(u, v)) {
			ans -= w;
		}
	}
	cout << ans << '\n';

	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 3420kb

input:

4
icpc
macau
regional
contest

output:

4

result:

ok single line: '4'

Test #2:

score: 0
Accepted
time: 2ms
memory: 3500kb

input:

3
ababa
babab
aba

output:

7

result:

ok single line: '7'

Test #3:

score: 0
Accepted
time: 584ms
memory: 106972kb

input:

26
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

output:

0

result:

ok single line: '0'

Test #4:

score: 0
Accepted
time: 2ms
memory: 3524kb

input:

7
jia
ran
jin
tian
chi
shen
me

output:

9

result:

ok single line: '9'

Test #5:

score: 0
Accepted
time: 2ms
memory: 3428kb

input:

10
theysaynothinglastsforever
weareonlyheretoday
loveisnowornever
bringmefaraway
takemetoyourheart
takemetoyoursoul
givemeyourhandandholdme
showmewhatloveis
bemyguidingstar
itiseasytakemetoyourheart

output:

55

result:

ok single line: '55'

Test #6:

score: 0
Accepted
time: 5ms
memory: 3928kb

input:

100
dblkekaekijliimalcaidjjfaghdmhifkiebieffbddjmflkhagajcfmkccjjadgiijdbdldgbbhgcfdcadbeiabkemiefdccmhdcfilhkfabmfdmigfgigdcibgaeicedfiidgecbhdamiaiefbmbgbjhklbhafmhckklbmmiemkcbfgjihmdjkai
bciiecmbc
cdjailkkbefkbmlekiefdhklcbdccfbgkagflfemjjmkjmcgiibldlmhbcldjajgafmakfbhecgcckkkglklljhmliehidbkicm...

output:

476

result:

ok single line: '476'

Test #7:

score: 0
Accepted
time: 1025ms
memory: 109376kb

input:

2000
ecbhcebgbcjgjiihdefajfbbaajfjdedggciaegdiijhijgedbgejhgjjfhabdfhbihdeegcehbcjhgebcjachbdeiefejefhcjdihfcfgeegdahhjhjiiffjjadifiijjbhhjjeffabiaagcjhaachjbiecfeceefddecjchjfibgedfdghgdijdcdahfeddjihbhbbghjjffdcibaggiiadbaajhfcgdbaafbicahjhabfdbeacccfdehebciafaaffdfjdciafbhidbahdccjhjdadcciecfbhac...

output:

17765

result:

ok single line: '17765'

Test #8:

score: 0
Accepted
time: 953ms
memory: 107636kb

input:

1413
gjjmlceglbmbibjmmfcfmickcllfekgmicmifdbfgdgbeecgjgalbfkdfljjkclfgkaacdgigblaiaiehkeiccbjamikdgcjfemhhfebicddelklibjafmjhleebkimeblljfembgcgacdlkhjmbijjgacjaajebjfkcllffalheefeaedbmmkicaeecckdmedddbikeieimjmmcfdcgamicfbeimkjfamidjfhejdgiehkjkbdaaaeieldfibkkcgallieiamfehcdggiigkabblgigjgdlmflmafj...

output:

11429

result:

ok single line: '11429'

Test #9:

score: 0
Accepted
time: 3419ms
memory: 422916kb

input:

2000000
j
o
v
p
h
b
t
s
x
y
u
c
t
n
p
l
b
e
c
v
d
c
p
s
u
m
d
d
i
m
h
t
a
e
j
i
i
c
c
h
d
x
j
w
m
a
j
p
f
l
n
i
q
c
c
k
q
g
q
b
u
z
u
v
d
d
f
n
i
j
v
n
i
e
l
w
h
v
m
m
i
z
w
y
d
z
l
w
h
b
x
b
a
r
y
d
x
f
r
h
p
o
u
x
u
f
u
b
c
i
p
l
j
o
j
o
n
u
k
w
x
h
z
x
z
y
d
y
d
w
h
u
k
b
u
e
i
o
g
n
y
x
y
h
l
j
...

output:

1999974

result:

ok single line: '1999974'

Test #10:

score: 0
Accepted
time: 964ms
memory: 107852kb

input:

2
adwkgmoosmodblpylbpymmnbzyjzddfegdqppefjstpueeurhjpuvdxudgtgmaksfdjwhogcivwmbqeiavcxybubknkwqwqxzaujoclyzhnaztibonzpotsaicwaznyapujfwugvdtxfmhgcuhrlcklnminnxnoojzvfwbczubgijnldwcqzocodmdqltgzcguicwmdsombxbmcxotyfvmijsaulhtnppxtnkygakhzltbjjwjqrwyaozwzgroxhmquyocazjzkecvqzfgqdhttgpuojilqwbmurruscvd...

output:

8

result:

ok single line: '8'

Test #11:

score: 0
Accepted
time: 1113ms
memory: 107748kb

input:

1413
ababaaaababbababbabbaababaabaaaabaabbabababbbabbaaababbbbabbababaaababbbaabbaaabaaabababbbbbbbabbaababbababaaabaabbaaaaabbbaaabbabaaabaabaababaaabbabbabaabaabbaabbbbbbabbabaaaabbababaabbababbbaaabababaabaababaaaabaaaaababbbbbbbbabbabaabbaabbbbabbbbbaaabbbbbaabbbaabababababaaabbaaaaabbaaaabaaaaa...

output:

42405

result:

ok single line: '42405'

Test #12:

score: 0
Accepted
time: 992ms
memory: 109052kb

input:

1413
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

output:

1995156

result:

ok single line: '1995156'

Test #13:

score: 0
Accepted
time: 1024ms
memory: 108272kb

input:

1
baababbbbbaaabbaabbabaabbabaababaabaaabbbbbbbabbbababbbabaaababbbabbabaaabbbbbaabbaabbaabaaabaaaabbbabbbaaabbbbbabbbaaabaabbabbbbaabbbabbbababaaabaababaabbbababbabbbbaaabbaabbbbbbabbabaaaabaaaabbbaaabbbbbbaaabaaabbabbababbabaabbabababababbbbaaababaaaabaabaaababbbbbabbbaaaaaababbbbbababaabbbabaaaab...

output:

0

result:

ok single line: '0'

Test #14:

score: 0
Accepted
time: 1289ms
memory: 108588kb

input:

1413
fnjfbnhskrerdxmxrlthhclkgcfrwukmccsfpbazsetxbfwvsseauqmcuutqofeshwxyxxasbtpfaocsgguayjcsdxitcnllliljglbjesqggubmbvozessjpcctrngdwsoedrpkjgsqatedrtfvlzhddjyvxfavxozcfooowhgzalctgjrriywmhvjeajzzxadepsslbbkdmpxsaoljmixvdgafpbgocjstdmegnrkbijvizjtnrqiwykdpmjfquxjympeziqdsbydahwpapyypkbfwmkohkxfwplv...

output:

464827

result:

ok single line: '464827'

Test #15:

score: 0
Accepted
time: 1570ms
memory: 107116kb

input:

1413
vrotfwuoloxhchhuitgryunhjurviyookrumnoabeziigrdpmlglfvurrlmblapvtlkhbmkgmuosltjxerkkmfdfspgeqwbjhlslycrkwhehdohhhrtuwwbpmivegwppikaimtulbyugobnwvjsrhtivddzjcrbmbbupvoayoxefatdvexnmqxhwdjhfairbmlsjbvjrspwsibonydbrcinfrwkrfduropcrkljeahyijoxhryaujnzakmurpqkgfmxpxivzsuhbihkchnwsaxtmlpfbfphldgccrab...

output:

254582

result:

ok single line: '254582'

Test #16:

score: 0
Accepted
time: 1450ms
memory: 107292kb

input:

1413
bbbbbabaabbabbbbbbaaabbaabaaabaabbabbaabaaabbbaaaaaabbaaabaaaabbabbbbbbbabbbbbabaaabaababbbaaabababbaaaaabbbbbabaaaabaaabbabbabbabaabaabaaabbbabbabaaababbbbbbaaaaababbabbbaabaaabaabbabbaaabaababaaabbbbabbabbabaaabbbabbbabbbbabaabbbabbbbabbbbaaabbbabaaabbaaaaabbababbababbbbbbaaaabaaaabaaaaabbabb...

output:

1498740

result:

ok single line: '1498740'

Test #17:

score: 0
Accepted
time: 1363ms
memory: 108280kb

input:

1413
lcoqmejczgztniltuwtqgzmgyjdvkiqvlgozlzdmhchwksqerjuxkigjslgetycefqfqriezkmvwgndxyzvjiducocihbkogctyuecvqqqnxgsuerrpbwldkooupyixbdvxjzsaskppqhlvafmcbyfmmtgduqaxvxwfwohoawftfrofwahxyplmktpglrfjobolpxbsvhlaabhydrxeijqueibqzgzoipbzmctjlelnfsllllptufqaptqpzrdjgeluigdwlolsjzdwkusgcigrbzgcqlozpbvahzys...

output:

1285703

result:

ok single line: '1285703'

Test #18:

score: 0
Accepted
time: 1663ms
memory: 107728kb

input:

500
eqrouzrzqavpwipjhbpbbsypakjkmpithzhdazdqzyivgzhpawsdorsrdcdstllansenrjgsjqqspxhrvaumtnfwrioktzmiusynbegluwdlvmcsoyghlleetuopwdzqxxdzdlmzjrkgdozunhbythfoetdaydswcxxgllfucsbhgcbtjduemztquysvdkrikqovbvpdnsmuwopitjmhkhyfiqaldtvnjjikhzpwubsqriymtnueuulcmnqrglepxobgqqcktvaldbzwovcirnneafvisczpsavisauo...

output:

93437

result:

ok single line: '93437'

Test #19:

score: 0
Accepted
time: 1484ms
memory: 108756kb

input:

500
aababbabbbabbbaabbaaababaabbabbaaaabbaaababababbbbbbbaabaaaabaabaabaababaabbaaabaaabaaaaabaababbaabaaabaaabababbabaabaaabaaaabaaabaabbabbbbababbbbbbabbbbbabaababbabaaaaaaabaabababbaabbbbbbbaaaaababaababaabbaaabaabbbaaaabbabbabbbbaabbbbaaaaabaabbabbababbaaababaaabbbabbabaaaabbaabbababbbbabbabbbaa...

output:

621482

result:

ok single line: '621482'

Test #20:

score: 0
Accepted
time: 1484ms
memory: 108496kb

input:

500
xkhhkzemvoilwwysloxrngtnalowphqotywezcsuqahpwhjjwjalpvysipqxyhoshkyioqchqlyothxrqacivacersdqbrkkdisuywdqibgdidwbfylbanhsfmkjgutlemfeiiusjaxuftvltgoutxoajajpwfbatiedtawcavwhptsizieufsxpuigqnjqguwknwirydqgujadlljfqeehynopazfhtetgabazgqhbzjgkupsltwusjijmkqtduomvpmwvcfydoeoluiypesjqbscnjxqdleacsyzwd...

output:

1305055

result:

ok single line: '1305055'

Test #21:

score: 0
Accepted
time: 1366ms
memory: 108620kb

input:

500
afdppltdvwipholvfwehnlbcmcxujuxlbabqnwtiudzbunzzjfiwqjzclyzwswiuylmmjiwdlygflfhumrtcbuhosgnmexieivsrdpuopcwckdywwrzxliofqdswacaixtbckxtzzpbjubgqthqgrgmhjgdjidtiyukitdhuukeqmmhhvzpmfbvqjazsfuceqomjuwdoijhwvodqptewwzmgznafhejxsvimtldcvkasagjpbnnzpbhoohtxnewgceuvrxzojahjgsajhniwoqlezgbnlzvndpixpkly...

output:

1248710

result:

ok single line: '1248710'

Test #22:

score: 0
Accepted
time: 1182ms
memory: 108860kb

input:

4000
gdlglqbydbkucqucvkhjoicbrutfyulgywmlpqlurxpvqhkywpnnwerqgjnnxyheruhcotfjpohhyrfwiulgbcyjnnwlofrfcykqspchfjqfcyemupuvutghifuykmeokohcfjlmueuycykwpeujzctkhpibmfvorjdmqztnqyqzjgzcsekanoxzescqxxeojjczivoywhzhpzjhftkvjhunzwfzwlphvsifpweldfstgdcoymjktfzvdfubqlilfqrvpxiuednlgqzhekrypstuxpwqagbgzubotli...

output:

587656

result:

ok single line: '587656'

Test #23:

score: 0
Accepted
time: 1257ms
memory: 108828kb

input:

4000
baabaabbababbbbbabaabaabababaabaabbbabbaaaabbbabbaaababbbaaaabaababbbbbbbbaaaaabbabaabbabbabaabbaaabbbaaaabbbbaabaabbbbaabbaaaaaaaaaaaaabbbaaaabaabbbabbaabaabaaaaaabbaaaaabaaaaabbbaaaaabaabaaaaaaabbaabbaaabbaabaabbabbbaaaaaaabbbabbbabbaaabaababbaaabbaabbababaabbbbaaaaabbaabbbaaabbabababbbbbaaaa...

output:

867646

result:

ok single line: '867646'

Test #24:

score: 0
Accepted
time: 1204ms
memory: 108280kb

input:

4000
baaaabbabbaaaabbbaaabbababaaaababbbaaabbbbbbbaabaabbbbbbbbbbbabbaabaabbbaabaabbaaaababbbaaabbbabaababaababbaaaaabaabbbabbababbbbbaaaaaaabbabababbaabbaaaabbbbababbbbbbaaaababbbbbbaababbaabbaabbbababbaabbabbaaababababaaababbbaaabbaababbbbbaaaabababaabbabaabaaaaabbaaaabbababaaaaaaababaabbbbabbbabb...

output:

870160

result:

ok single line: '870160'

Test #25:

score: 0
Accepted
time: 1219ms
memory: 109892kb

input:

4000
jnbnjwgrxbxyqkbxyktdciatkisupcgkaanotwsywpwnczkakgfmohiypavecbrddgrwemuadsphrchdioswvaxnastcvmhasbwynsbsmqafsjjcybzvbwidqchnqpnjqjjfdkzpbzuvldyjgzejxpmntuajlpufrlzkaisoonwikhonlrvvrqszrenfgtmyeokhwcxkrwtbdjqqalltddvvhicqbroifyrbcootmjwrsmhuxspdykahscxwtnganxepnskabgrvuzrutgxttxdlbajvqpvdmbtatfc...

output:

913862

result:

ok single line: '913862'

Test #26:

score: 0
Accepted
time: 915ms
memory: 108432kb

input:

4000
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

output:

1979531

result:

ok single line: '1979531'

Test #27:

score: 0
Accepted
time: 1258ms
memory: 109176kb

input:

4000
fdhdijdfcdeedcbjgafdgcdegdbibaahdhaeifbdafhfhaefacigedcdhfbifdhbcjhaeidhdccdchicbjafceabjdchgjaabhbffgcajhdbecghdiehffjgiddhcfhdcabhhfigcdjihffagaibhjhhcdegiecbedabaajdgjbefbdbadcdeefgbibbhgaedhbhjgejchfjiididfcibjefhfgebiagfdfejjgbhdcfefghdafgjbbijaeciebghgcgiijhedeijahecfabgcbhefacbbaijdghdhe...

output:

1228665

result:

ok single line: '1228665'

Test #28:

score: 0
Accepted
time: 1251ms
memory: 108016kb

input:

4000
rjdknocpagvrnghrpyukwifkkddprokianvndxdwgczcauhwrkpqmhqsmxqpmoxhazmujpspurtwndsfphxcrnwfnmuzauictvwfmowghscvvwckgzoxnkcawybzukbohyvtmrcwjgrgdvzovoosphzmirifzwmbfzuauwkmgoaodbfasvtjcuxmnftmcfohhkhdgtppkrfnvmpnhiwletasztebbskidtnjocaywgnrsdecnapsvpkuloxojjymelxtpyrlgfcakiexkurglbcwscmblkpqvmjlmcw...

output:

911422

result:

ok single line: '911422'

Test #29:

score: 0
Accepted
time: 1218ms
memory: 126804kb

input:

100000
tduxfndk
uneyosepeblysz
myyzqqfxlyol
hueyo
vxkovfhrybgmvlhweq
jkcclhdeyo
dyggb
mokfduurtqfsmaeynq
gluuajukjnjc
dfcjqnme
xyrupyxgfnwd
hvdjjbewg
jjeyjmmlyol
fiqebs
otpjpq
nfmezyyuyme
wxyiuymslehvl
coshvqaht
tftlwgbnjyicrvjmuwrx
owpmmwkqpvhwxhiujh
vxrvymrouzryawtwombk
ukkzjbon
gokkrpnzmhonmhkfpj...

output:

989636

result:

ok single line: '989636'

Test #30:

score: 0
Accepted
time: 1105ms
memory: 124800kb

input:

100000
baaabbbbabbabaaaba
baabbaababbababbb
bbbbbaababab
baaabbaa
abbabbbb
baaabba
baaabbbbbab
bababbabbbb
aababbbbbaabbbbabbbb
abaababbaaaaabbb
abaaabaaabbbbabbb
bababaababbb
aababbabbbbbbababba
abaabbabab
abbabbaaabaaaabbaaba
abbaaabbaabbbba
bbaababbabbaaabaaaa
aaababaaabbbabb
babbbb
bbbbaaaabaabb...

output:

1282986

result:

ok single line: '1282986'

Test #31:

score: 0
Accepted
time: 1045ms
memory: 125580kb

input:

100000
pkhvbtnxfvxaem
uxvbtnxcgcsmwurthw
rfvxaem
wgzfvxaem
fafvxaem
nghhlpjfdga
wcljhoesadwmfwhvbtnx
uvbtnx
bcbffvxaemvbtnx
jhoesadwmfwhvbtnx
ncgcsmwurthwvbtnx
kyvbtnxjhoesadwmfwh
wavbtnxjhoesadwmfwh
ivbtnxssu
qnghhlpjfdgarjw
vbtnxibjhoesadwmfwh
zfvxaemigqvbtnx
swcgcsmwurthwsc
sjhoesadwmfwh
dfvxaemp...

output:

1170625

result:

ok single line: '1170625'

Test #32:

score: 0
Accepted
time: 1274ms
memory: 126772kb

input:

100000
aaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaa
aaaaaaa
aaaaa
aaaaa
aaaaaaaaa
aaaaaaaaaaa
aaaaaaaaaaaaaaaa
aaaaaaaaaaa
aaaaaaaaaa
aaaaaaaaaaaaa
aaaaaa
aaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaa
aaaaaaaa
aaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaa
aaaaaaaaaaa
aaaaaaaaaaaaaa
aaaaa
aaaaaaaaa
aaaaaaaaaaaaaaa
aaaaaaaaaaaaa
...

output:

1950179

result:

ok single line: '1950179'

Test #33:

score: 0
Accepted
time: 1624ms
memory: 107548kb

input:

20
cqhcbnnuobmgcqfdzbuvyajaidogyrtcjbemouifxulzlqcmtwiryttiwhpuykxjldvaxefuastnkvaeukxsdtdcauwbevkqziswmzrmrmabkchemhyrabtavqdajwzphqhuggbrujigzfhqxcrtcifvvvfgrevavfwdlauhjenkjudgyubcfhxcccivjfyemujywfhfjxsutvcsreuwnuypwimfqmlcjucfzklkhdeaahurnrqalqrjzfrrsctbzygaktltyrvyyqsnjinwyghzmyqgdmhekpeulrnjj...

output:

201946

result:

ok single line: '201946'

Test #34:

score: 0
Accepted
time: 1583ms
memory: 107380kb

input:

20
baaababababababbaaaabababaaaabbabbaaabbbaabbaabaabbbabaabbbabbaabbbbabaaaabaabaabbbbaaaabbaaaaabaaaabaabbbbbbaaabbbabbaaaababbbbbbbaabbbababbabaababaaabbbbaabbaaaaababbbabaaaaabbaabababaabbbbabaaaaaababbabbbbabbabaaaabbaabaabaabaaaabbbbaaabbbbabbababbbabbbabaaaabbbaaaaabbbababbaaabbbbaabbbbbbabba...

output:

187803

result:

ok single line: '187803'

Test #35:

score: 0
Accepted
time: 1544ms
memory: 107952kb

input:

20
wighspqotssudabkedkygymrryunfudbdlkxbdnwxfqbitnmwxmeyyypysfgoihtuuqxxgtullvvgjhcivigwiyucdatnkmhefgblbxymtusedwozhxxrvruaunngbxkifnvhkwvfqolvmawvxtnunjuhwjnzizioofvabqgtgtpvtnqshfmwoidmssmatwohblhdepwmjnhcbavkfjgwslikfumysdfwombscxfnnopqomcwyksqvaaaxoaprflgtmaxmfkbwcrkqtgodaeabgcyvepmjduervefcqgy...

output:

265680

result:

ok single line: '265680'

Test #36:

score: 0
Accepted
time: 610ms
memory: 108560kb

input:

20
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

output:

1799368

result:

ok single line: '1799368'

Test #37:

score: 0
Accepted
time: 1495ms
memory: 106980kb

input:

2
aabaaabababbaababaaabaaaaababaabaabaabaabbbbbbaabaaabaabaabaabbbbbbaaaabbbabaabbbabaaabbaabababbbbbbbbaaabbbbbaabaababaabbabaaabbbbaabaaaababaaaabbaaaaababbabbbaababaaaaaabaababaaaaaaaabbbaaaaaaaaaaaaaabaaaaaaaabaabbbaabbabbabbabbaabbaabbaababaaabbababbbbaaaababbbaababbaabbabbbabaababaabaababaaaab...

output:

92934

result:

ok single line: '92934'

Test #38:

score: 0
Accepted
time: 1819ms
memory: 108520kb

input:

2
jcmgevxqwiaflstvldrnmkeoeczrruqbxfjdagtyptfcoyybepqqzvggxgxpcpsbubfxhyvyubbkbznfqdysyeywmxlrodslgxipyfougrgkstriwpavwatkzgbolwbrtjgtownxnthfoyqlqsimvbwuhakuwllrurllqakdwtlkrdvjvfvqiunruxlslwjhqwsgzcjvvwfpzdyxkwntqakmawglurskonqnpcknarecwezhaoasrmgrkmefgwpujijbmvqomwqjarsbguvxgcqrqwwwsnsexdhjazanvq...

output:

91737

result:

ok single line: '91737'

Test #39:

score: 0
Accepted
time: 557ms
memory: 109680kb

input:

2
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

output:

93662

result:

ok single line: '93662'

Test #40:

score: 0
Accepted
time: 1539ms
memory: 107324kb

input:

10
cdvthwmepkhrkkvpucesqgrdwtzdvicynxrutfykridcsbrpfvhwdabzucdpkhpxhgtcospfashuvajdxqzarcclefcrosqvakyoenughdadgmgdchgvjiinpleddvbfhzuloqtvvtabfihkpnfaxqqmburgrslathljxehvahwhsftjszkpradswxaqrlqlvewukdpmlouftnomoydfajzzgzpxnhkilhcykskxiecburpkrtsuctvktkhitsqhzuflopjwwnysgneaiwumqpwlbetwwldxfiiuatqwh...

output:

735652

result:

ok single line: '735652'

Test #41:

score: 0
Accepted
time: 1768ms
memory: 108916kb

input:

4000
fcrfjdagkknhhvvklrmnnnqcrmjnifrphkwsnyaxiqfpnczytnngbejusuhabapjkodzmgwvydmlfomabzhedsywmhyohqzreeqqmpnkuawcoicrhlwndwjsehilzjfdnxwcmjmjjklzsptcbrpenaytjgfjpmmfjihyuvhfguhyjacktrrgrpadyhmptaueldcfzjkfdrfptizmbqvvixyiekxzmudxxgcwrpdxilmcvnqweltokrwqdtpsnvpndulujpttchxxvotzbppmilcmguewpvjupuoalku...

output:

555122

result:

ok single line: '555122'

Test #42:

score: 0
Accepted
time: 1958ms
memory: 107704kb

input:

4000
abbaaabaabbabbbbaaaaaabaaaaaaababbabbaaabbaaabbabaaaababaabaaaabbbbbbbbaabaaaaaaaaaababbaababababababaabbabaabaababbaaaabaaaababaabbabbabaaaaaababaaababbabbbbabbbbaaaabaabaaaaabaabbaaaabbbbbbaaaaaaaaaabbabbabbaababbaababababbabbbbbbbaaababbbaabbbabaabbbbaabbbabaaabaaaaabbabbbbbabbbaaaaaaababbab...

output:

777185

result:

ok single line: '777185'

Test #43:

score: 0
Accepted
time: 975ms
memory: 109284kb

input:

2
baaaaaaabaaabaaaaabbaababbabbababbabbaabaabbbbbbbbabbbabaaabaabbbbbbabaabbaaabbbabaaaabbababbaabbbabaaaabaabbaaabaaabbbbabaabbbbababbabaabbabbabbababaabbabbbaaabababaabbbbbabaabbaabaaabbbaababababbaabaaaabaabbbbabbaababbbbabbaaabbbbbbbabababbaaabbbbaaabbaababababaabbbbaaabaaabbbababbbbbababbbabbba...

output:

54

result:

ok single line: '54'