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