lcs_via_lis_ut.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #include <util/generic/utility.h>
  2. #include <util/string/util.h>
  3. #include <library/cpp/testing/unittest/registar.h>
  4. #include "lcs_via_lis.h"
  5. class TLCSTest: public TTestBase {
  6. UNIT_TEST_SUITE(TLCSTest);
  7. UNIT_TEST(LCSTest);
  8. UNIT_TEST_SUITE_END();
  9. private:
  10. size_t Length(TStringBuf s1, TStringBuf s2) {
  11. TVector<TVector<size_t>> c;
  12. c.resize(s1.size() + 1);
  13. for (size_t i = 0; i < c.size(); ++i) {
  14. c[i].resize(s2.size() + 1);
  15. }
  16. for (size_t i = 0; i < s1.size(); ++i) {
  17. for (size_t j = 0; j < s2.size(); ++j) {
  18. if (s1[i] == s2[j])
  19. c[i + 1][j + 1] = c[i][j] + 1;
  20. else
  21. c[i + 1][j + 1] = Max(c[i + 1][j], c[i][j + 1]);
  22. }
  23. }
  24. return c[s1.size()][s2.size()];
  25. }
  26. void CheckLCSLength(TStringBuf s1, TStringBuf s2, size_t size) {
  27. size_t len = NLCS::MeasureLCS<char>(s1, s2);
  28. UNIT_ASSERT_VALUES_EQUAL(len, Length(s1, s2));
  29. UNIT_ASSERT_VALUES_EQUAL(len, size);
  30. }
  31. void CheckLCSString(TStringBuf s1, TStringBuf s2, TStringBuf reflcs) {
  32. TString lcs;
  33. size_t len = NLCS::MakeLCS<char>(s1, s2, &lcs);
  34. const char* comment = Sprintf("%s & %s = %s", s1.data(), s2.data(), reflcs.data()).c_str();
  35. UNIT_ASSERT_VALUES_EQUAL_C(Length(s1, s2), len, comment);
  36. UNIT_ASSERT_VALUES_EQUAL_C(lcs.size(), len, comment);
  37. UNIT_ASSERT_VALUES_EQUAL_C(NLCS::MeasureLCS<char>(s1, s2), len, comment);
  38. UNIT_ASSERT_VALUES_EQUAL_C(reflcs, TStringBuf(lcs), comment);
  39. }
  40. void LCSTest() {
  41. CheckLCSString("abacx", "baabca", "bac");
  42. const char* m = "mama_myla_ramu";
  43. const char* n = "papa_lubil_mamu";
  44. const char* s = "aa_l_amu";
  45. CheckLCSString(m, n, s);
  46. CheckLCSString(n, m, s);
  47. CheckLCSString(m, m, m);
  48. CheckLCSString(m, "", "");
  49. CheckLCSString("", m, "");
  50. CheckLCSString("", "", "");
  51. {
  52. const char* s1 =
  53. "atmwuaoccmgirveexxtbkkmioclarskvicyesddirlrgflnietcagkswbvsdnxksipfndmusnuee"
  54. "tojygyjyobdfiutsbruuspvibmywhokxsarwbyirsqqnxxnbtkmucmdafaogwmobuuhejspgurpe"
  55. "ceokhgdubqewnyqtwwqfibydbcbxhofvcjsftamhvnbdwxdqnphcdiowplcmguxmnpcufrahjagv"
  56. "cpjqsyqurrhyghkasnodqqktldyfomgabrxdxpubenfscamoodgpocilewqtbsncggwvkkpuasdl"
  57. "cltxywovqjligwhjxnmmtgeuujphgtdjrnxcnmwwbgxnpiotgnhyrxuvxkxdlgmpfeyocaviuhec"
  58. "ydecqmttjictnpptoblxqgcrsojrymakgdjvcnppinymdrlgdfpyuykwpmdeifqwupojsgmruyxs"
  59. "qijwnxngaxnppxgusyfnaelccytxwrkhxxirhnsastdlyejslrivkrpovnhbwxcdmpqbnjthjtlj"
  60. "wnoayakfnpcwdnlgnrghjhiianhsncbjehlsoldjykymduytyiygrdreicjvghdibyjsmxnwvrqb"
  61. "jjkjfrtlonfarbwhovladadlbyeygvuxcutxjqhosuxbemtwsqjlvvyegsfgeladiovecfxfymct"
  62. "ulofkcogguantmicfrhpnauetejktkhamfuwirjvlyfgjrobywbbitbnckiegbiwbtmapqrbbqws"
  63. "wviuplyprwwqoekiuxsmwgwyuwgeurvxempguwmgtadvrkxykffjxfdyxmsibmdlqhldlfbiaegt"
  64. "kswcmfidnrhaibuscoyukwhdtoqwlpwnfgamvfqjpfklgurcwvgsluyoyiuumrwwsqgxatxnxhil"
  65. "ywpkeaugfaahmchjruavepvnygcmcjdnvulwcuhlolsfxcsrjciwajbhdahpldcfggubcxalqxrl"
  66. "coaiyeawxyxujtynodhnxbhs";
  67. const char* s2 =
  68. "yjufxfeiifhrmydpmsqqgjwtpcxbhqmfpnvsvsapqvsmqmugpqehbdsiiqhcrasxuhcvswcwanwb"
  69. "knesbuhtbaitcwebsdbbxwyubjoroekjxweeqnqmydbdbgbnfymcermhpbikocpsfccwjemxjpmc"
  70. "hkhtfaoqgvvtpipujsxesiglgnpsdwfjhcawkfpffuyltqqhdkeqwkfpqjhnjdsnxlevyvydikbr"
  71. "hnicihnevsofgouxjcnjjknxwwgaaaikdcxmhxfowadqudrapvwlcuwatrmiweijljdehxiwqrnq"
  72. "tnhgukbwmadcjpfnxtswhmwnvvnwsllkyshfobrdmukswpgwunysxpnnlmccolvqyjsvagulpcda"
  73. "ctsnyjleqgttcgpnhlnagxenuknpxiramgeshhjyoesupkcfcvvpwyweuvcwrawsgvfshppijuug"
  74. "hdnujdqjtcdissmlnjgibdljjxntxrgytxlbgvsrrusatqelspeoyvndjifjqxqrpduwbyojjbhi"
  75. "tmondbbnuuhpkglmfykeheddwkxjyapfniqoic";
  76. CheckLCSLength(s1, s2, 247);
  77. }
  78. {
  79. const char* s1 =
  80. "ssyrtvllktbhijkyckviokukaodexiykxcvlvninoasblpuujnqnilnhmacsulaulskphkccnfop"
  81. "jlhejqhemdpnihasouinuceugkfkaifdepvffntbkfivsddtxyslnlwiyfbbpnrwlkryetncahih"
  82. "vqcrdvvehvrxgnitghbomkprdngrqdswypwfvpdrvqnavxnprakkgoibsxybngvenlbfcghcyldn"
  83. "kssfuxvpvfhaawqiandxpsrkyqtiltmojmmwygevhodvsuvdojvwpvvbwpbbnerriufrwgwcjlgx"
  84. "jcjchsfiomtkowihdtcyewknlvdeankladmdhwqxokmunttgaqdsbuyhekkxatpydfgquyxuucda"
  85. "dllepofxoirmaablfyyibcnqkdbnsaygkqkbvupdhajfshouofnokwlbdtglrbklpgknyuiedppl"
  86. "chxbnnmbumdtrsgwitjlmkkdxysvmsvcdulmanmsdeqkmwgfthmntdbthdthdodnubqajpfyssea"
  87. "hwxymiyubkhhxlbmjptujiemrdljqkskdkuokvimencavihwqdaqtcljrgwvxpuegnoecobfllwu"
  88. "upsfhjrrpiqtjlwigjkpltwfpoqxsdrojtawpaximslojqtadsactemuhpnshkgyoyouldanktcg"
  89. "dhxdpwawabxwjhnjdmewrwtciquuiqnwdsbdvnuvjyewmjppkwvcotptmyrsqaovmaysjuvtenuy"
  90. "orvdsssgjgcgksdwaaladocotgveuscwauawdhqlkqsjtmltvkkcfkgwpdtormkefkigkppwpvsy"
  91. "fpblccsbyboouahotiifixsjuxlvqpqmpmkcgbvsefkqasearilhlxuqdfqqxxohhbdyiudqsree"
  92. "btwfxdtxlaynrusgbffhltthkejitseuqyeqvhqgyobijwmspwbsisghsarji";
  93. const char* s2 =
  94. "lcnygawhsrprxafwnwxnrxurpnqwwtwuciuexxvswckwopnmhhmhvdwmxtgceitqofivvavnqlmo"
  95. "hbieyaxcysagyqcvijxyowiognsntxcdlmueoafqvqwafyhgyoewhwxvqswvwfkqohtutawwnmhr"
  96. "pjmhyiygdekonlxhkbeaheqvnqbwhambhrrhkyfcehjfblgriumapavbcxxdqytiuylxmhtjjloa"
  97. "fvnyhqsgalacknksxxxilgfcankxreaburvmxukmfprfpmfqgqhmirlnltkjxslumhtamtndaffh"
  98. "ybfywiwjlafnsgsvywflsfkeeidwptigidvhnqdjiwvgkfynncyujodsjiglubptsdofftfxayno"
  99. "txoykiivbvvipdpcgjadvbanaeljdbxxynlqqpdphirrbjqfqtnxabitncafatbosjnlimxfxlrh"
  100. "thlfsdyfbhtwywewqubjcvskmwxwjyhesqbviflihnjfejcgjtkgicgmmcurynmdaoifojmedcqb"
  101. "aeqalxnljvglnvyverrtosfeeuajyxkmmpjgcioqxnnqpjokxaenfoudondtahjduymwsyxpbvrf"
  102. "kpgiavuihdliphwojgjobafhjshjnmhufciepexevtfwcybtripqmnekkirxmljumyxjpqbvmftt"
  103. "xmfwokjskummmkaksbeoowfgjwiumpbkdujexectnghqjjuotofwuwvcgtluephymdkrscbfiaeg"
  104. "aaypnclkstfqimisanikjxocmhcrotkntprwjbuudswuyuujfawjucwgifhqgepxeidmvcwqsqrv"
  105. "karuvpnxhmrvdcocidgtuxasdqkwsdvijmnpmayhfiva";
  106. CheckLCSLength(s1, s2, 288);
  107. }
  108. }
  109. };
  110. UNIT_TEST_SUITE_REGISTRATION(TLCSTest)