fork(1) download
  1. #include <iostream>
  2. #include <iostream>
  3. #include <string>
  4. #include <regex>
  5. using namespace std;
  6.  
  7. string WordReplaceMatches(string str, regex pattern)
  8. {
  9. smatch matches;
  10. while(regex_search(str, matches, pattern))
  11. {
  12. string matches0;
  13. matches0+=matches[0];
  14. string wordTemp;
  15. if (matches[1]!="")
  16. wordTemp+=matches[1];
  17. else
  18. wordTemp+=matches[3];
  19. size_t position=-1;
  20. int licznik=0;
  21. while((position=matches0.find(wordTemp, ++position))!=string::npos)
  22. {
  23. licznik++;
  24. }
  25. string patternTempReplace;
  26. patternTempReplace+=wordTemp;
  27. patternTempReplace+='/';
  28. patternTempReplace+=to_string(licznik);
  29. str=regex_replace(str, pattern, patternTempReplace, regex_constants::format_first_only);
  30. }
  31. return str;
  32. }
  33. string LetterReplaceMatches(string str, regex pattern)
  34. {
  35. smatch matches;
  36. while(regex_search(str, matches, pattern))
  37. {
  38. string matchTemp=matches[0];
  39. string patternTemp;
  40. patternTemp+=matches[1];
  41. patternTemp+='*';
  42. patternTemp+=to_string(matchTemp.size());
  43. str=regex_replace(str,pattern,patternTemp,regex_constants::format_first_only);
  44. }
  45. return str;
  46. }
  47.  
  48. string Decomp_LetterReplaceMatches(string str, regex pattern)
  49. {
  50. smatch matches;
  51. while(regex_search(str, matches, pattern))
  52. {
  53. string slicznik=matches[0];
  54. slicznik=slicznik.substr(2,slicznik.size()-1);
  55. int licznik=stoi(slicznik);
  56. string letterTemp=matches[0];
  57. letterTemp=letterTemp.substr(0,1);
  58. string patternTemp;
  59. for (int i=0; i<licznik; i++)
  60. {
  61. patternTemp+=letterTemp;
  62. }
  63. str=regex_replace(str,pattern, patternTemp, regex_constants::format_first_only);
  64. }
  65. return str;
  66. }
  67.  
  68. string Decomp_WordReplaceMatches(string str, regex pattern)
  69. {
  70. smatch matches;
  71. while(regex_search(str, matches, pattern))
  72. {
  73. string temp=matches[0];
  74.  
  75. size_t position=temp.find("/");
  76. string tempWord=temp.substr(0,position);
  77. int licznik=stoi(temp.substr(position+1,temp.size()-position));
  78. string patternTemp;
  79. for (int i=0; i<licznik; i++)
  80. {
  81. patternTemp+=tempWord+" ";
  82. }
  83. patternTemp.pop_back();
  84. str=regex_replace(str,pattern, patternTemp, regex_constants::format_first_only);
  85. }
  86. return str;
  87. }
  88.  
  89. int main()
  90. {
  91. string choose;
  92. getline(cin,choose);
  93. if (choose=="KOMPRESJA")
  94. {
  95. string line,str;
  96. while (getline(cin,line) && line!="")
  97. {
  98. if (line[line.size()-1]==' ') str+=line;
  99. else str+=line+" ";
  100. }
  101. if (str[str.size()-1]==' ') str.pop_back();
  102. regex letter_pattern("([a-z])\\1{3,}");
  103. str=LetterReplaceMatches(str,letter_pattern);
  104. regex word_pattern ("([a-z]\\b)(\\s\\1\\b){2,}|([*a-z0-9]{2,}\\b)(\\s\\3\\b)+");
  105. str=WordReplaceMatches(str,word_pattern);
  106. cout<<str;
  107. }
  108. else if (choose=="DEKOMPRESJA")
  109. {
  110. string line,str;
  111. while (getline(cin,line) && line!="")
  112. {
  113. if (line[line.size()-1]==' ') str+=line;
  114. else str+=line+" ";
  115. }
  116. if (str[str.size()-1]==' ') str.pop_back();
  117. regex letter_pattern("[a-z]{1}\\*\\d+");
  118. str=Decomp_LetterReplaceMatches(str,letter_pattern);
  119. regex word_pattern ("\\w+\\/\\d+");
  120. str=Decomp_WordReplaceMatches(str,word_pattern);
  121. cout<<str;
  122. }
  123. return 0;
  124. }
Success #stdin #stdout 0s 15352KB
stdin
KOMPRESJA
aaaabaaaa aaaabaaa
stdout
a*4ba*4 a*4baaa