stack
é¢ç½ãã£ãï¼ äº¤å·®æ°ã«é¢ããåé¡ã«å¸°çãããã åé¡ã¸ã®ãªã³ã¯ åé¡æ¦è¦ 座æ¨å¹³é¢ä¸ã«ã4 é ç¹ã®åº§æ¨ã ã§ãããããªé·æ¹å½¢ãããããã®é·æ¹å½¢ã®å¨ä¸ã¾ãã¯å é¨ã«ã ã¨ã©ãã«ã®ã¤ãã åã®ç¹ãããï¼ã©ã® 2 ç¹ãç¸ç°ãªãï¼ã ã«ã¤ãã¦ãåãã©ãã« ã® 2 ç¹â¦
ããã«ã stack ãç»å ´ããããªåé¡ï¼ åé¡ã¸ã®ãªã³ã¯ åé¡æ¦è¦ ä¸ã®å³ã®ããã«ãé«ãã ã®ä»åããçééã«ä¸¦ãã§ãã¦ããã®éã¨ä¸¡ç«¯ã® ã«æã®ã¹ãã¼ã¹ã表ãçªå·ã ã¨ããããããã¯å¹ ãçããã ä»ãã¹ãã¼ã¹ 0 ã«æ°´ãå ¥ãã¦ããã1 ååã®ã¹ãã¼ã¹ã«ã¤ãâ¦
弦ã交ãããã©ãããå¤å®ããã¨ãããã¹ã¿ãã¯ã®å ¸ååé¡ï¼ åé¡ã¸ã®ãªã³ã¯ åé¡æ¦è¦ 次ã®å³ã®ããã«ãåå¨ä¸ã« åã®ç¹ ãããã æ¬ã®å¼¦ãããï¼å³ã¯ ã®ã¨ãï¼ã 弦ã«äº¤å·®ããããã©ãããå¤å®ããã å¶ç´ èããã㨠ãã®åé¡ã¯ stack ã§è§£ãããã¨ã§æåâ¦
ã¹ã¿ãã¯ã®å¿ç¨åé¡ï¼ ãã¹ãã°ã©ã ã®æ大é·æ¹å½¢ãæ±ããã¢ã«ã´ãªãºã ã¨ä¼¼ããããªã¹ã¿ãã¯ã®ä½¿ãæ¹ãããï¼ åé¡ã¸ã®ãªã³ã¯ åé¡æ¦è¦ é·ã ã®æ°å ãä¸ããããããããã®å¤ã¯äºãã«ç¸ç°ãªãã å ã«å¯¾ãã¦ã次ã®æ¡ä»¶ãæºããæ´æ° ã®åæ°ãæ±ããã ãªãä»»â¦
ããçµæ§é£ããã¦ãåµã¾ã人ã¯åµã¾ã£ã¦ãã¾ãã¨æãï¼ï¼ åé¡ã¸ã®ãªã³ã¯ åé¡æ¦è¦ ãã³ã¸ã§ã³ã«ã¯ããã¼ã·ã§ã³ ã¨ãæµ ãããã ä»ããã³ã¸ã§ã³ã§ åã®ã¤ãã³ããèµ·ãããã¤ãã³ã 㯠2 ã¤ã®æ´æ° ã§è¡¨ãããã ã®ã¨ãï¼ãã¼ã·ã§ã³ ãç¾ãã ãããæ¾ããã©ãâ¦
ã¹ã¿ãã¯ã«ããã·ãã¥ã¬ã¼ã·ã§ã³åé¡ï¼ åé¡ã¸ã®ãªã³ã¯ åé¡æ¦è¦ æ´åã®ã¨ããã«ãã³åã«å¯¾ãã¦ãè±å°æåãããã¤ãæ¿å ¥ããã¦ã§ããæååãä¸ãããã (ãã¨ãã°ã"(a(ba))c")ã ãã®ãããªæååã«å¯¾ãã¦ãé«æ©åãæ°çµ¶ãããã©ãããå¤å®ãããã次ã®ãâ¦
ã¹ã¿ãã¯ã®ããç·´ç¿åé¡ï¼ åé¡ã¸ã®ãªã³ã¯ åé¡æ¦è¦ 空ã®å㨠åã®ãã¼ã«ãããã çªç®ã®ãã¼ã«ã®å¤§ãã㯠ã§ããããããã åã®æä½ãè¡ãã åç®ã®æä½ã§ã¯ã åç®ã®ãã¼ã«ãåã®ä¸çªå³ã«ä»ãå ããå¾ã次ã®æé ãç¹°ãè¿ãã åã«ãããã¼ã«ã 1 ã¤ä»¥ä¸ãªâ¦
ã¾ã¨ãã«è¨ç®ããã¨æ¡æ°ãã¨ãã§ããªããã¨ã«ãªãã®ã§ããLU ã§æ¶ãããRU ã§æ¶ãããæ´»ç¨ãããï¼ åé¡ã¸ã®ãªã³ã¯ åé¡æ¦è¦ é ç¹æ°ãååå¤ãå®å ¨äºåæ¨ãä¸ãããããæ ¹ã®çªå·ã¯ 1 ã§ãããä¸è¬ã«é ç¹ ã®å·¦åé ç¹ã®çªå·ã¯ ãå³åé ç¹ã®çªå·ã¯ ã§ããã æâ¦
ã¹ã¿ãã¯ãç¨ããã¨æ¥½ã«æ§æ解æã§ããï¼ åé¡ã¸ã®ãªã³ã¯ åé¡æ¦è¦ +(1,+(+(2,3)),+(4,+(5))) ã®ãããªå½¢å¼ã®å ¥åã (1+((2+3))+(4+(5))) ã®ããã«å¤æãã (詳ããã¯åé¡æåç §)ã å¶ç´ èããã㨠ã¹ã¿ãã¯ãç¨ãã¦ãæ¼ç®åããæ ¼ç´ãã¦ããã ',' ãæ¥ãâ¦
ã¹ã¿ãã¯ãç¨ãã¦è§£æ±ºã§ããå ¸ååé¡ åé¡ã¸ã®ãªã³ã¯ åé¡æ¦è¦ é·ã ã®æ°å ãä¸ããããã å ã«å¯¾ãã¦ã ã㤠ãæºããæ大㮠ãæ±ãã (åå¨ããªãå ´å㯠-1)ã å¶ç´ ã¡ã¢ ã¹ã¿ãã¯ã使ã㨠ã®è¨ç®éã§è§£ããã詳細ã¯éåæ¬ãåç §ã ã³ã¼ã #include <bits/stdc++.h> usi</bits/stdc++.h>â¦
人çã§æåã«è§£ãããã¹ã¿ãã¯ã®åé¡ åé¡ã¸ã®ãªã³ã¯ åé¡æ¦è¦ 以ä¸ã® 3 種é¡ã®ã¯ã¨ãª ( å) ãé«éã«å¦çããããã°ã©ã ãå®è£ ããã ã¯ã¨ãªã¿ã¤ã 1ï¼ ã¨ããé¡åã®æ¬ãæºã®ä¸çªä¸ã«ç©ã ã¯ã¨ãªã¿ã¤ã 2ï¼ä¸çªä¸ã«ç©ã¾ãã¦ããæ¬ã®é¡åãçãã ã¯ã¨ãªã¿ã¤â¦
ã¹ã¿ãã¯ãç¨ãã¦ããã£ãã®å¯¾å¿ãã¨ãåé¡ï¼ åé¡ã¸ã®ãªã³ã¯ åé¡æ¦è¦ 対å¿ã®ã¨ãã¦ããã«ãã³å ãä¸ããããã対å¿ããå·¦ãã£ã '(' ã¨å³ãã£ã ')' ãããããã ã®ä½çªç®ã¨ä½çªç®ã§ããããé ã«æ±ããã å¶ç´ ã¡ã¢ 詳ãã解説ã¯ããã«ããã®ã§ã¡ã¢ç¨åº¦â¦
ã¹ã¿ãã¯ãç¨ããã·ãã¥ã¬ã¼ã·ã§ã³åé¡ï¼ åé¡ã¸ã®ãªã³ã¯ åé¡æ¦è¦ æåãæ¬ã¯ç©ã¾ãã¦ããªãã次㮠åã®ã¯ã¨ãªã«çãã ãã¯ã¨ãªã åã¯ã¨ãªã§ã¯æåå ãä¸ããããã ã "READ" ã§ã¯ãªãã¨ããã¿ã¤ãã«ã ã§ããæ¬ãæ°ãã«ä¸ã«ç©ã¾ãã ã "READ" ã§ããâ¦
ãã®æã®ã¹ã¿ãã¯ã®åé¡ã¯ãããã£ãã常èã¨åããï¼ åé¡ã¸ã®ãªã³ã¯ åé¡æ¦è¦ ãã« ããã®é ã«ä¸¦ãã§ããããã« ã®é«ã㯠ã§ããã å ã«å¯¾ãã¦ããã« ã®éã«ãã« ããé«ããã®ããªããã㪠() ã®åæ°ãæ±ããã å¶ç´ èããã㨠ããã«ãã¹ã¿ãã¯ã使ããâ¦
å¶ç´ãå°ããã®ã§ãªãã¨ã§ããªããC++ ãªãæååã®æ«å°¾ãåé¤ããé¢æ° pop_back() ãç¥ã£ã¦ããã¨æ¥½ã ã¨æãããã åé¡ã¸ã®ãªã³ã¯ åé¡æ¦è¦ ã¨ãã£ã¿ã¼ä¸ã§ã'0'ã'1'ã'B' ã¨ãã 3 種ã®ã¿ã¤ãã³ã°å ¥åãè¡ãã '0' ã¨æã¤ã¨ãã¨ãã£ã¿ã¼ã«è¡¨ç¤ºãããæåâ¦
ã¹ã¿ãã¯ãæ´»ç¨ããç³»ã®åé¡ï¼ åé¡ã¸ã®ãªã³ã¯ åé¡æ¦è¦ å¤ ã®æ¸ããããã¼ã«ããã®é ã«çã«æ¿å ¥ãã¦ããã æ¿å ¥ãããéã«ãåãæ° ã åé£ç¶ãããããããããã¹ã¦æ¶ãããã®ã¨ããã åç®ã®ãã¼ã«ãæ¿å ¥ãããç¬éã®ãçå ã®ãã¼ã«ã®åæ°ãé次çããã â¦
ã«ãã³åã«é¢é£ãã¦ã·ãã¥ã¬ã¼ã·ã§ã³ãã¦ããåé¡ï¼ åé¡ã¸ã®ãªã³ã¯ åé¡æ¦è¦ è±å°æåã¨æå '('ã')' ãããªãé·ã ã®æåå ãä¸ããããããã®æååã«å¯¾ãã¦ã次ã®æä½ãç¹°ãè¿ãã ãæå '(' 㨠')' ã«æã¾ããé¨åæååã§ãã£ã¦ãã«ãã³æåãå«ã¾â¦
stack ã使ã£ã¦ã«ãã³åå¤å®ãããåé¡ã®äºç¨®ï¼ åé¡ã¸ã®ãªã³ã¯ åé¡æ¦è¦ 3 種é¡ã®æå 'A'ã'B'ã'C' ãããªãæåå ãä¸ããããããã®æååã«å¯¾ãã¦ããå·¦ããè¦ã¦ãã£ã¦ "ABC" ããã£ããæ¶ãããä½åº¦ãç¹°ãè¿ãã¦ãã£ã¦æ®ãæååãçããã å¶ç´ èâ¦
ã¢ãããã¯ã«æ§æ解æãããã«ãã³åã®åé¡ã¨ã¿ãªãã¦ãstack ã§å¦çããã åé¡ã¸ã®ãªã³ã¯ åé¡æ¦è¦ 次ã®ãããªãé åã宣è¨ãããé åã«å¤ãä»£å ¥ãããããå¦çã表ãããã°ã©ã ããä¸ããããã a[2147483647] a[0]=1 B[2] B[a[0]]=2 a[B[a[0]]]=3 a[2147â¦
Cartesian Tree ãå®è£ ãããï¼ å ·ä½çã«ã¯ãYosupo Library Checker ã®åé¡ãCartesian Treeããéããã¨ã«ããã åé¡æ¦è¦ é·ã ã®æ°å (äºãã«ç°ãªã) ããèªå°ããã Cartesian Tree ãæ±ãã¦åºåããã å ·ä½çã«ã¯ Cartesian Tree ã®åé ç¹ã«ã¤ãã¦ããâ¦
ã«ãã³åã®æ´åæ§å¤å®åé¡ã®äºç¨®ã¨è¨ããã åé¡ã¸ã®ãªã³ã¯ åé¡æ¦è¦ æåå ã cat æååã§ããã¨ã¯ã次ã®æ¡ä»¶ãæºãããã¨ãããã ã¯ç©ºæååã§ãã ãã cat æåå ãåå¨ãã¦ã ã¨è¡¨ãã ä¸ããããæåå ã cat æååã§ãããã©ãããå¤å®ããã â¦
ããã¯å¦ã³ã®æ·±ã DP åé¡ï¼ åé¡ã¸ã®ãªã³ã¯ åé¡æ¦è¦ é·ã ã®æ°å ããã (è² å¤ããããã)ããããã®æ°åãããã¤ãã®é£ç¶ããåºéã«åå²ããã åºéåå²ã®ä»æ¹ãæé©åããã¨ãã®ãååºéã«ããããæ大å¤ã¨æå°å¤ã®å·®ãã®ç·åã¨ãã¦ãèããããæ大å¤ãâ¦
ã»ã°æ¨ã¨ãå¿ è¦ãªãã¦ãæ¬å½ã«åç´ãªå®è£ ã§è§£ããã®é¢ç½ã åé¡ã¸ã®ãªã³ã¯ åé¡ å¶ç´ èããã㨠ã¾ãã¯ãçæå¯è½ã§ããããã®åãããããç¹å¾´ä»ããèãããã¨æã£ããæ°åã®çææé ããããã«ãå ¥ãåæ§é ããã¦ããã®ã§ãã«ãã³åãé£æ³ããã ã«ãã³â¦
é»ãã¹ãé¿ããªãããé·æ¹å½¢é åã®å¤ã®ç·åãæ大åããåé¡ã¨ãã¦è§£ããï¼ åé¡ã¸ã®ãªã³ã¯ åé¡æ¦è¦ ã®ã°ãªããããã£ã¦ãåãã¹ ã«ã¯æ£ã®æ´æ° ãæ¸ããã¦ããã ã°ãªããã«å«ã¾ããé·æ¹å½¢é åã®ãã¡ããé·æ¹å½¢é åã«å«ã¾ããå¤ã®ç·åãã¨ãé·æ¹å½¢é åã«å«â¦
ç§ãã¡ã¯ãã°ã©ãã¢ã«ã´ãªãºã ã¨ã㦠DFS ã BFS ãå¦ã¶ã¨ é ç¹ s ããé ç¹ t ã¸è¾¿ãçãããã©ãããå¤å®ãã é£çµæåã®åæ°ãæ±ãã äºé¨ã°ã©ãå¤å®ãã ãããã¸ã«ã«ã½ã¼ããã ãªã©ã¨ãã£ãä¾é¡ã次ã ã¨ããªãã¦ãããã°ã©ãæ¢ç´¢ã¹ãã«ãé«ãã¦ããéãâ¦
ãããæ©ä¼ã« SA-IS ãæ´åããï¼ ä»åã®è¨äºã¯ããã¾ã§èªåãèªãã§ããã以ä¸ãç®æããªãåå¿é²ã¨ãã¦ã åé¡ã¸ã®ãªã³ã¯ åé¡æ¦è¦ 2 ã¤ã®æåå ã«å¯¾ãã¦ãå é ä½æåãä¸è´ãã¦ããããã ã¨è¡¨ããã¨ã«ãã¾ãã é·ã ã®æåå ãä¸ãããã¾ãã ã® æåâ¦
éã«åããªãã£ãï¼ï¼ï¼æããï¼ï¼ï¼ åé¡ã¸ã®ãªã³ã¯ åé¡æ¦è¦ é·ã ã®æ°å ãä¸ãããã¾ãã以ä¸ã®æ¡ä»¶ãæºãããããªãé·ã ã®æ°å ã®åæ°ã 998244353 ã§å²ã£ããã¾ããçããã å¶ç´ èããã㨠ã¨ããæ¡ä»¶ã¯æ±ãã¥ããã®ã§ãå é¤åçã§ããã¨è¯ããããâ¦
ããããªå®è£ æ¹æ³ããããããã§ã ãéã«åãã®ã§ããã¹ã¦ã®æ¸ãæãæ¹æ³ã«ã¤ã㦠ã§ã§ãããªãååã åé¡ã¸ã®ãªã³ã¯ åé¡æ¦è¦ 1 㨠2 㨠3 ã®ã¿ãããªãé·ã ã®æ°åãä¸ãããããåãæ°å¤ã 4 å以ä¸é£ç¶ãããã¨ã¯ãªãã ãã®æ°åã®ãã¡ã® 1 ç®æã®å¤ãâ¦
ã«ãã³åç³»ã®åé¡ï¼ åé¡ã¸ã®ãªã³ã¯ åé¡æ¦è¦ é·ã ã®æåå ãä¸ãããããæååã«å¯¾ãã¦ã以ä¸ã®å¦çãç¹°ãè¿ãè¡ããæä½ã®çµæå¾ãããæååã®é·ãã®æå°å¤ãæ±ããã æååä¸ã® "fox" ãåé¤ãã å¶ç´ èããã㨠ã«ãã³åã§ããä¼¼ãåé¡ã¯ãããæâ¦
BNF ãæ¸ããã¦ããã®ã§ãLL(1)ææ³ãå帰éä¸ã¨ãããã®ãæ¨æºã¿ããã ãã©ãad-hoc ã«ã¹ã¿ãã¯ã§ãªãã¨ããã¦ãã¾ã£ã åé¡ã¸ã®ãªã³ã¯ åé¡æ¦è¦ 以ä¸ã® BNF ã«ãã£ã¦å®ç¾©ãããæååãä¸ããããã <Cipher> ::= <String> | <Cipher><String> <String> ::= <Letter> | '['<Cipher>']' <Letter> ::= '+'<Letter> | '-'<Letter> | 'A' | 'B' |</letter></letter></letter></cipher></letter></string></string></cipher></string></cipher>â¦