Guest User

Untitled

a guest
Aug 14th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.36 KB | None | 0 0
  1. Longest common substring constrained by pattern
  2. #include <iostream>
  3.  
  4. const std::string longestCommonSubstring(int, std::string const& s1, std::string const& s2, std::string const& s3);
  5.  
  6. int main(void) {
  7. std::string s1="hello 5", s2="bolo 7", s3="lo 9sdf";
  8. std::cout << "Trying to get "lo 5", actual result: "" << longestCommonSubstring(5, s1, s2, s3) << '"';
  9. }
  10.  
  11. const std::string longestCommonSubstring(int must_include, std::string const& s1, std::string const& s2, std::string const& s3) {
  12. std::string longest;
  13.  
  14. for(size_t start=0, length=1; start + length <= s1.size();) {
  15. std::string tmp = s1.substr(start, length);
  16. if (std::string::npos != s2.find(tmp) && std::string::npos != s3.find(tmp)) {
  17. tmp.swap(longest);
  18. ++length;
  19. } else ++start;
  20. }
  21.  
  22. return longest;
  23. }
  24.  
  25. //find all pattern sequences
  26. N1 = findAllPatterns(s1, number);
  27. for i = 2 to n:
  28. for item in Ni-1:
  29. for match in findAllPatterns(si, nextPattern(item))
  30. Ni.add([item, (match, indexOf(match))]);
  31.  
  32. //for all pattern sequences identify the max common substring
  33. maxCommonLength = 0;
  34. for sequence in Nn:
  35. temp = findLCS(sequence);
  36. if(length(temp[0]) > maxCommonLength):
  37. maxCommonLength = length(temp[0]);
  38. result = temp;
  39.  
  40. return result;
  41.  
  42. #include <iostream>
  43. #include <string>
  44. #include <sstream>
  45. #include <vector>
  46.  
  47. typedef std::pair<std::pair<std::string, std::string>, std::pair<std::pair<std::string, std::string>, std::pair<std::string, std::string>>> pairStringTrio;
  48. typedef std::pair<std::string,std::pair<std::string,std::string>> stringPairString;
  49.  
  50. stringPairString longestCommonSubstring(const pairStringTrio&);
  51. std::string strFindReplace(const std::string&, const std::string&, const std::string&);
  52.  
  53. int main(void) {
  54. std::string s1= "6 HUMAN ACTIONb", s2="8 HUMAN ACTIONd", s3="10 HUMAN ACTIONf";
  55. pairStringTrio result = std::make_pair(std::make_pair(s1, "6"), std::make_pair(std::make_pair(s2, "8"), std::make_pair(s3, "10")));
  56.  
  57. stringPairString answer = longestCommonSubstring(result);
  58. std::cout << '"' << answer.first << ""t"" << answer.second.first << ""t"" << answer.second.second << '"';
  59. }
  60.  
  61.  
  62. stringPairString longestCommonSubstring(const pairStringTrio &foo) {
  63. std::string longest;
  64.  
  65. for(size_t start=0, length=foo.first.first.size()-1; start + length <= foo.first.first.size();) {
  66. std::string s1_tmp = foo.first.first.substr(start, length);
  67. std::string s2_tmp = strFindReplace(s1_tmp, foo.first.second, foo.second.first.second);
  68. std::string s3_tmp = strFindReplace(s1_tmp, foo.first.second, foo.second.second.second);
  69.  
  70. if (std::string::npos != foo.second.first.first.find(s2_tmp) && std::string::npos != foo.second.second.first.find(s3_tmp)) {
  71. s1_tmp.swap(longest);
  72. ++length;
  73. } else ++start;
  74. }
  75.  
  76. return std::make_pair(longest, std::make_pair(strFindReplace(longest, foo.first.second, foo.second.first.second), strFindReplace(longest, foo.first.second, foo.second.second.second)));
  77. }
  78.  
  79. std::string strFindReplace(const std::string &original, const std::string& src, const std::string& dest) {
  80. std::string answer=original;
  81. for(std::size_t pos = 0; (pos = answer.find(src, pos)) != answer.npos;)
  82. answer.replace(pos, src.size(), dest);
  83. return answer;
  84. }
Add Comment
Please, Sign In to add comment