fork download
  1. /**
  2. Template by Akikaze (秋風) - formerly proptit_4t41.
  3. Code written by a random fan of momocashew and Chiho.
  4.  
  5. H△G x Mili - November 27th, 2013
  6. Mag Mell (Mili) - Sep 17th, 2014
  7. H△G x Mili Vol.2 - May 9th, 2015
  8. Miracle Milk (Mili) - Oct 12th, 2016
  9. 青色フィルム (H△G) - February 14th, 2018
  10. Millennium Mother (Mili) - April 25th, 2018
  11. **/
  12.  
  13. /** -----PRAGMA----- **/
  14. #pragma comment(linker, "/stack:200000000")
  15. #pragma GCC optimize("Ofast")
  16. #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
  17.  
  18. #include <bits/stdc++.h>
  19. using namespace std;
  20.  
  21. /** -----BASIC MACROES----- **/
  22. #define endl '\n'
  23. #define i64 long long
  24. #define ld long double
  25. #define pub push_back
  26. #define mp make_pair
  27. #define fi first
  28. #define se second
  29. typedef vector<i64> vi;
  30. typedef vector<ld> vd;
  31. typedef vector<string> vs;
  32. typedef vector<bool> vb;
  33. typedef pair<i64, i64> pii;
  34. typedef pair<i64, pii> pip;
  35. typedef pair<pii, i64> ppi;
  36. const long long MOD = 1000000007LL, INF = 1e9, LINF = 1e18;
  37. const long double PI = 3.141592653589793116, EPS = 1e-9, GOLD = ((1+sqrt(5))/2);
  38.  
  39. /** -----BIT CONTROLS----- **/
  40. template<class T> int getbit(T s, int i) { return (s >> 1) & 1; }
  41. template<class T> T onbit(T s, int i) { return s | (T(1) << i); }
  42. template<class T> T offbit(T s, int i) { return s & (~(T(1) << i)); }
  43. template<class T> int cntbit(T s) { return __builtin_popcount(s); }
  44.  
  45. /** -----IDEAS/ALGORITHMS-----
  46.  
  47.   -------------------------- **/
  48.  
  49. /** -----CUSTOM TYPEDEFS/DEFINES----- **/
  50.  
  51.  
  52. /** -----GLOBAL VARIABLES----- **/
  53. i64 N, Base = 257, res = -1; string S, R;
  54. vi hashS, hashR, powBase;
  55.  
  56. /** -----EXTENSIVE FUNCTIONS----- **/
  57. void GenerateHashVal() {
  58. for (i64 i=1; i<N; i++) {
  59. powBase[i] = (powBase[i-1] * Base) % MOD;
  60. }
  61. for (i64 i=0; i<N; i++) {
  62. if (i == 0) {
  63. hashS[i] = ((i64)int(S[i]) * powBase[i]) % MOD;
  64. hashR[i] = ((i64)int(R[i]) * powBase[i]) % MOD;
  65. }
  66. else {
  67. hashS[i] = (hashS[i-1] + ((i64)int(S[i]) * powBase[i]) % MOD) % MOD;
  68. hashR[i] = (hashR[i-1] + ((i64)int(R[i]) * powBase[i]) % MOD) % MOD;
  69. }
  70. }
  71. }
  72.  
  73. bool match(i64 stS, i64 enS, i64 stR, i64 enR) {
  74. i64 valS = hashS[enS], valR = hashR[enR];
  75. if (stS > 0) {
  76. valS -= hashS[stS-1]; while (valS < 0) valS += MOD;
  77. }
  78. valS *= powBase[max(stS,stR)-stS]; valS %= MOD;
  79. if (stR > 0) {
  80. valR -= hashR[stR-1]; while (valR < 0) valR += MOD;
  81. }
  82. valR *= powBase[max(stS,stR)-stR]; valR %= MOD;
  83. // cout << "valS = " << valS << endl;
  84. // cout << "valR = " << valR << endl;
  85. return (valS == valR);
  86. }
  87.  
  88. /** -----COMPULSORY FUNCTIONS----- **/
  89. void VarInput() {
  90. //ios_base::sync_with_stdio(0); cin.tie(NULL);
  91. cin >> N >> S; R = S; reverse(R.begin(), R.end());
  92. hashS.resize(N, 0); hashR.resize(N, 0); powBase.resize(N, 1);
  93. }
  94.  
  95. void ProSolve() {
  96. GenerateHashVal();
  97. i64 top = 0, bot = N / 2;
  98. while (top <= bot) {
  99. i64 z = (top + bot) / 2;
  100. i64 ans = z * 2 + 1;
  101. // tracker3(ans, top, bot);
  102. i64 flag = false;
  103. for (i64 i=0; i<=N-ans; i++) {
  104. i64 stS = i, enS = i+ans-1;
  105. i64 stR = N-1-enS, enR = N-1-stS;
  106. if (match(stS, enS, stR, enR)) {
  107. flag = true; break;
  108. }
  109. }
  110. i64 q = top, r = bot;
  111. if (flag) {top = z + 1; res = max(res, ans);} else bot = z;
  112. if (q == r) break;
  113. }
  114. top = 1; bot = N / 2;
  115. while (top <= bot) {
  116. i64 z = (top + bot) / 2;
  117. i64 ans = z * 2;
  118. //tracker3(ans, top, bot);
  119. i64 flag = false;
  120. for (i64 i=0; i<=N-ans; i++) {
  121. i64 stS = i, enS = i+ans-1;
  122. i64 stR = N-1-enS, enR = N-1-stS;
  123. if (match(stS, enS, stR, enR)) {
  124. flag = true; break;
  125. }
  126. }
  127. i64 q = top, r = bot;
  128. if (flag) {top = z + 1; res = max(res, ans);} else bot = z;
  129. if (q == r) break;
  130. }
  131. cout << res;
  132. }
  133.  
  134. /** -----MAIN FUNCTION----- **/
  135. int main() {
  136. #ifdef Akikaze
  137. //freopen("FILE.INP", "r", stdin);
  138. //freopen("FILE.OUT", "w", stdout);
  139. #endif
  140. VarInput();
  141. #ifdef Akikaze
  142. auto TIME1 = chrono::steady_clock::now();
  143. #endif
  144. ProSolve();
  145. #ifdef Akikaze
  146. auto TIME2 = chrono::steady_clock::now();
  147. auto DIFF = TIME2 - TIME1;
  148. cout << "\n\nTime elapsed: " << chrono::duration<double>(DIFF).count() << " seconds.";
  149. #endif
  150. return 0;
  151. }
  152.  
  153. /**
  154. OOO###$-^`^^.--~~~-.~~~~~-.^~~~~__~:++;+;~^^^^-!%eiiio?O##?i==e####!iiiii:.^^
  155. ##OO##$-^`^^.-~~~~~..~~~~~.^~~~~__~:+;;;;~^``^-*!!**o*e$##!=++*O###e=====:.^^
  156. ##OOOO$-^```.~~~~~~..~~~~~.^~~~~-_~:+;;;;~```^-e!eee**e$##!+;;oO###*====+_^^^
  157. ###OOO$_^```^~~~~~~..~~~~~.^.~~~-_~:+;;;;.```^-?!ee***e$##e;;;oO###*+++++_^`^
  158. ######O_^```^~~~~~~.......^^...~--~:;::::.````~ee***ooe$##e;::iO##Oo+++++-^``
  159. ####O$%_^```^.^^^^^^^^^^^^^^^^^^....~~~~~.````~;;:::::;=ii+_-_;o***+:::::-^`^
  160. O$$O$%e-^```^^.~....^.....^^.....^^.^^^^^^````^......^....................^``
  161. i++iiee-^```^.-;__:_.-:__:~._:-__..-----.^````^~-~~-~.~-~~-..~~~~..~~~~~.^^``
  162. :::;;i=-^```^.-;_-;_._;__;-.:;_:;~~::_:_.^````^-:__;-._;_:;~~___;-.__-::.^^``
  163. +;;;=i;~^````^-_--_-.-_--:~._:-__.~__-__.^````^~_--:-.-_-::.~_--:-.__-__.^^``
  164. ii=io*+~^````^-:--:-.-:__:-.__-__.._--_-.^````^~_--_~.---__.~_--:~.__-__.^```
  165. ee*e!!o-^````^-:_-:_._;__;-._:-::.~:_-;_.`````^-;__:-._:-::.~;__;-._:_;:.^```
  166. ??????e_^````^~~~~-~.~-~~-~.--~--.._--_-.^````^~_--_~.-_-__.~:--:~.__-:_.^```
  167. %%%??%!;^````^^^^^^^^^^^^^^^^^^^^^^^^...^^````^......^.....^......^.....^^```
  168. %%%%?%%o~`````````````````````````````^``````^^``^^^^^^^``^^^^^^^^^^^^^^^^```
  169. $$$$%%$!:^``````````````````````````````````^^```````````````````````````````
  170. #OOOOO$%o~````````````````````````````^^^^^..^`^^^..^``````````````````^^^^^^
  171. #####OO$!_^``^^^^^`^...^^``````````._+++++-~-.^.^..-.^````````````````^..~--:
  172. O######O%:...-____-_::_-~~~~-~.~~~_=ioo*eo===;_~.~~--~^^^^^^^^^^^^^^^^~--__::
  173. OOOOO##!:~^^.;!!?????o;__++===o*i:;+ii*!**oooi==;:_--:_:+=iii====;:;;__:+;__:
  174. OO%%$O#e~^^^^:!!$OOO?e=:__;+::_*=ii===oo**e***o*ooi;_--:=?%!!!!eo;:+;__;++;:;
  175. #O%?%$OO+.^`^:?%%##$i;::::_____oe!!?e*==oo*e**ee!?**+-_:_=oe!ee?=;;+;;;++::+=
  176. ##OO$%$O%_..~+??!%O?+;______:_=!?!$$?%!eo*****?!$!?!e+;:_:;o*o*!i;:;;+=+:_:;+
  177. #OO$?!?%$eiio*ooi==+::_:__;::;!%$$O$$$$%!e***!!$!%O$!oi:_;;ooiio*+;==+i+;+=ii
  178. $$?e*o*e!!!*o=;::;:_-__:::-::o$OOOOO#OO$$%!!ee%?O#$!!o+;::+i=+++=;++;+=+=i==o
  179. %?!*i=ioooo=+;:_:+=:__-___-_:%O#OOO###OOO%%%$$$OO$?%%=-_::_::;::::;;::::++++*
  180. OO%?*+;=o!e*o=+;;==:__:__--_iOO#######O#OOO##OO$$%$O?+_.:__;;;--;=i=++===++o!
  181. #O!i+;;:;+;:;+==o=:--_::::__?###O############O$OO#OO*+:.~__;=;--e$eo!!o*oiii=
  182. O?*=;:;;:_~~-~-_;__--_;:_::;O###############OO###OO!=;_-~-__i+__!O*+;:+ii=i_.
  183. ??!*=+=;;_-~~~-_:;+;_;+:_;;*##############&######O?*+:__----____ie=;_;+=+::.^
  184. e$e!*oi;:__--_:;::;;::+;:;i%#############&&######$!i;:_____-:;__=;-_::_:_~-~^
  185. *O?!ooi+;:_~-_---_;;::=+_;oO#####&####&#&&&#####?o+;:____;:__:-:+_--::__-.^^^
  186. *#Oe++;;__:~_;__:;;;+:+i;;o##########&##&&&&&&#%=;;:__;+;=;_-~~_--__+;:::.^^`
  187. eO$o+;::_;=+i=++;:-__;io;:*#########&##&##&&&&#e;::::=*%!o=:--:__::;++;+=:~^^
  188. eO%=;+===*e?%*=;:__:;+o!+_?#####OO####&#O#&&&#$i:::+o*=oo=::__;:_-+=+:+;_:_.^
  189. e$%=+oe*o?$?oo;-~~_:;o*e*e$#####OO######OO###O!=:_:=?%%*i+;;_--:+;o**;o=_~..^
  190. e$%e**!*?%!;-_i+-~_++**oe?$O###OO####&&##%$#O%*;:___i?$o+:;_-~_oiii=eieo=;-^^
  191. !$%!e!ee!?i++_:;+;===o*o=$####OO##&&&&&%O??$%?o;__-~-==_:_;=:;++i++io!$!+:-^`
  192. !%?*o!!ii+_==::;i=;e!*e*?O######&&&&&#&%??!?%?i;:__-~-----;e=:+====*!*!%;~.~^
  193. e%?*ioi*!++*?oo=e*;ooieo?O###O#&&&&&&&&%!!e??e=;;;;:_--_~:;=__+;;:;e!*+!i.^~.
  194. e%?eoi+o***e!e%!$%ii****%O#####&&&&&&&&Oeeee!o+++;+=+_-:~:;_-::_;;;oi;_=i~^^^
  195. !?!!??*=ie?ooe?oo=_-:i*!O#####&&&&&&&&&##$e*o=;;++=++:;_:;i;+;+io+;ii;~_;;_:;
  196. !ee?$%i=o!i+=*!ei-_::;;?O####&&&&&&&&&&##$ei=+;;+++=+;oi*=;-i*=**++oi:~~_;;=o
  197. !?!%$?=o?%o*%e!%=_+==++?OO###&&&&&&&&&&#O$!i=+;;++==i+oe=:+:??oo*===+;-.~_+*!
  198. !%?%O!i?#O###$%$!e!*=+*$O###&&&&&&&&&&&#$$!o=++++==ii=;io+i*%!oooi==i::~~~_!%
  199. e??%$?=i%###Oo*%$!o*ee?$####&&&&&&&&&&&#$%?*i=====iooeoo==oeeooo+!?!oo=:___*$
  200. e?%%%?==!###?=e$!o=o%$$O###&&&&&&&&&&&&#$??eo====ioo!?*i+=oi=++o!eeoi=~:;;:+!
  201. e?%$$%o*$###O!%?eo*e$%$####&&&&&&&&&&&&O???e*i==iio*o*o+====*!+o$!oi=_^.-:===
  202. ?%$##O%%O####OO%?i*%$$O###&&&&&&&&&&&&#$!??!eoiiio**iiiio*ooe$*?#!eii;-~_+oi=
  203. %%$#&#OO#######O!!%?$O###&&&&&&&&&&&&&O?e!?!e*oio*ooi+++o!e!e$?$O?!+i****ee*o
  204. ??$###OOOOO####$!$$%$?###&&&&&&&&&&&&&%!*e?!e*iiioooi;:=ie?%%$O!%e*+o?%?*ooii
  205. e%O#&#O$OOO$O##O$OO$!?##&&#&&&&&&&&&&O?**e!!e?%oiiioo-_:+??$%%?=o!o+:+i+:_---
  206. ?$O###OOO#$?%O##O$OO$O##&#&&&&&&&&###%e*o*ee!%**%e:i=.:=*??%?!?eoei:-_:_~~~..
  207. O$$OO##O##$!e%OO$$OO$##&###&&##&####%!*oo*ee%%%%$?i*e=o;oe!%?ie!o*=:___----~~
  208. #$%?!?O#O%%!e!%$%!!$O##&##&&####O##%eoiio**??!%$#O%!!*ei;=;i?e??e*i=++;;;++::
  209. #Oe-.:$#$%?????!!o=?##&&#&&###O$##!i+==io**?%?$$O#%??!=i=+:-o%O%!o!%?!e!!?!ee
  210. O?-``:%$$%%%??%?!=i$O#&##&&#&O?$%=--_;+=o*?%??O%OOO?e?iioi!o*%O%?o?%%%$$$$$$$
  211. ?_``~o!???!!!!!ee*!OO#&##&##%??+.``^~_;+i*O$OOO$$OO%%?e*ie%%e%O?eo?e!$O$?!%$$
  212. :` .+e!!eeeeeee**!!O#&#&&&#$?+. `._:+=o*$OOO%%$%$$?*;o%?*!?*=e$$$###$$OOO
  213. ^ ^::i!e*o****e**!!$##&&&#O?-^ ``~_:_*$OOOOOOOO?!!;ie!io!*i?O##########
  214. ``_o;=*ooooo*****ee$#&&&&#?.` ``^~-_%$OO#OOOOe*$?*o!io!=i!%OOOOO$$%$$
  215. ~_i*+oooooooo**o*ee$##&&##-. ```.~*$$$OOOOO?o?$!o*i*o+o!%%?%%%????%
  216. i;=i+iooooiioo**ee!$#&&&#e^ ``^^:$?$O$%OO$e!$?=+io==ee???????!!!?
  217. ?;_+=i=iooiioo**e!!O&&&&#:` `^^^?OOO$?$OO%e?ei+i**oo*eeeee******
  218. ?*;;=iiiiiiioio**!!O&&&&?^ ` ``` ``^^:OO$O$OO$O$?i++io*==iooooooooooo
  219. ?*++=iiiiooioi**e!?O&&&&+``` ~=^-____~```^^*OOO##O$%$?o*!i=iiiiiiiiiiiii==
  220. ?o+=oiioooioo****!%#&&&O.`^` -=_---_;+;^``^.%O####O$!!!?!i=i========++++++
  221. !i=ioooiooo**e!e!?%#&&&*`^``` `` ` .:.```;O####OO?!??!==i==++++;;;;;;;;
  222. *=io*oooi*e!!???%$O#&&#-^.`````` ```..^^`` .^```e##OO$$%?!*o+===+;;:::::::___
  223. oooeeee**!!%%%?%$O##&&?^.^`````^ `~.^^^`^.`````.$OOOO$**?!ooi=+;::_____-----
  224. ee*eee!?%%%$$$%$OO#&&&+^.^`````^^`~~.^.^``.~^^```+OO#O$oe%*ioo++;::___:_-----
  225. e*o**e?%%$%$OOO$$O#&&#~^.^^^^``^.~-....^``.~.~````?##O$e?%*+ii++;;::::;::____
  226. eooo*%OOO#####OO$O#&&$^^.^^^``^^~-~....^^`^~~~^```_O#O$?%$$io=;;;;;;;;;;;;;::
  227. *oo*!O#######O$%$O#&&e^..^^^`^^^.~.....^^`^.~~~^```*#O$%%O#$e;:;;;;:::_::____
  228. *oe??%$%$%?%%%??%$##&i`..^^^^^^^.~.~...^``^.~~-.```.OO%%$O##!;_;+;:__-~--~~~~
  229. *i*e*eeee!!!!e!!%%#O#+^^.....^^^.~.~~..^``^^.~-~^```+%$%?$##!;_;+;;:_~~-_-~~~
  230. *ioe*ee*!!e!!e!!%$O?#+^^...~.^^^.~~~~.^```^^.~~-~```.%$?!$OO!;_;++;:_-~---~~~
  231. o=ie*e**!e*ee*e%$##?#+`^..~~^^^^.~~...^```^^^.~--^```!%!%%OO?+:;++;;:_-~--~~~
  232. o==**e*e!e*e?!oeO#O$&=``^.~~^^^.~~~...^` ``^^.~~~~```o%?%!?%?=::;;;;:-~~~~~~~
  233. *i=*!e*eeeeee?%%O$$##=^`^.~.^^..~~~..``````^^^~~~~^``=$?%%%$$?i;++++;-~--~~~~
  234. !oie!!e!!**eoo?%$####=_`^.~^....~~....~--_~.~....~~``~%*i!?!**i=====+:-::-~~~
  235. !*ie!**!!***ooe%%#&&&i:.^^.^^..^.~~~__--:;;..~..~~~.``ee=***=iooiii==+++:-~~~
  236. $?eeeoo!*ooi=i**io#&&e_~^^.^^..~::::__~-:::.-~-~+.~.^`iO$$$$???!!i=+++==;--~~
  237. $%??*o*eo+oooi*o;i$O&$--^^.^^._::__:____::_.~--~*~..^^+####OO?%%?+;::;;;:----
  238. !???!!!*oieo==*=;;+=e#:-.^^^.:___-_:__::__-~-.~~oi.^..;######$??e:______-----
  239. ??%?!e!eeee*ii*=;+=++*+_.^^.:__--------_-_.~-~--;?-..~+O######$%!:___--------
  240. !!??ee?!ee*oii+;++i+;+i:-.._:-_--_-__--_--^~~.~-.**..-iO##O####$?:_______----
  241. e!%?eee*o*o====+++=+;+i:;~.;_-_~~--------~^~-~.-.-!:~-*#OOO##O#$!+;;;:;;;:::_
  242. ee??!eiii*oi==*=+;==+=o=;:~+_:_.~--~~~~~~..~~~.-~.i*.-*OOO#O##OOo=ii======+++
  243. !**e!!oii*oi=o*+;;====oi;+-+::-~--~~~~~~~^.~~~.-~-~!:_eOO######Oe=iii=====+++
  244. ?!**ee*oo*ii=oo;:;==+=*o+;:=;;_~~~~~~~~~~^~~~~.-~_~o=_!#OO#####O$o======+++++
  245. ?*ii!eoooi===oi;;:+++=**o++=;;_.~~~~~....^.~~~.~~:_;o_?#OOOO###O%i+++++++++;;
  246. !iii!oiooi==ioi===iiio*oi=+o=;_~~~~~~...^.~~~~.~~:_-o_%OOOO###OO%i;;;;;;;;;;;
  247. !**e!ooo**iio*o+;+ii==iooo==o;:.~~~~~...^..~~~..~_:_=_$O#OOO##OO$e;;;::;;;:::
  248. e**e*i=ioii=o!*i++===+ioi*i=o+:~~~~~~...`..~~~..~:;;+:OO#OOOOO##O%+::::::::__
  249. ee*!oi=ioiii*!!o++==i=oooooi==;~~~~~~...^...~...-;;+;+$$O####O#OO$+::________
  250. *oe*=iiooi=i*e*i==oi=+=ioi=ii+;~~~~~...^^...~~..-;+*;*OOO#######$%=::________
  251. io!i+io**i=ie*i===ii==oe!ee?i=;-~~~....^^......._;e*;%OOO#####O#O$*;:;:______
  252. =ie*ii=ii==ie*=++;=i=i**ee!?!i=_~~~....^........:;O*i$##OOO#O#O##O$+:;::_____
  253. iiooii=i==io**=;;;====io*eeeeoi:-~~....^.....~..;=#!oOO##OOOO###O#Oi;;:__---_
  254. **/
  255.  
  256. // Tribute to a friend I owe countless. Thanks, TN.
Success #stdin #stdout 0s 4584KB
stdin
5
abacd
stdout
3