Problem p02422 (C++)

Buggy submission (status: Runtime Error):
/**
 * ???????????????

????????? str
?????????????????????????????????????????????????????????????????°???????????????????????????????????????????????\?????????????????????????????§??????

print a b: str ??? a ??????????????? b ??????????????§??????????????????
reverse a b: str ??? a ??????????????? b ??????????????§?????????????????????
replace a b p: str ??? a ??????????????? b ??????????????§??? p
?????????????????????

????????§??????????????? str ????????????????????? 0 ???????????¨????????????

Input
1 ?????????????????? str ????????????????????????str
?????±?°????????????????????????????2 ????????????????????° q
?????????????????????????¶???? q
??????????????????????¨??????¢?????§?????????????????????

Output
??? print ???????????¨???????????????????????????????????????????????????

Constraints
1???str????????????1000
1???q???100
0???a???b<str?????????
replace ????????§??? b???a+1=p?????????

Sample Input 1
abcde
3
replace 1 3 xyz
reverse 0 2
print 1 4

Sample Output 1
xaze

Sample Input 2
xyz
3
print 0 2
replace 0 2 abc
print 0 2

Sample Output 2
xyz
abc
 */

#include <algorithm>
#include <iostream>
#include <sstream>

int main(int argc, char const *argv[]) {
  std::string Source;
  std::string Param;
  unsigned short q = 0;
  std::istringstream iss;

  getline(std::cin, Source);
  getline(std::cin, Param);
  iss.str(Param);
  iss >> q;
  iss.clear();

  for (size_t i = 0; i < q; i++) {
    std::string Command, Arg3;
    unsigned short Arg1, Arg2;
    getline(std::cin, Param);
    iss.str(Param);
    iss >> Command;
    if (Command == "print") {
      iss >> Arg1 >> Arg2;
      Source = Source.substr(Arg1, Arg2 - Arg1 + 1);
      std::cout << Source << std::endl;
    } else if (Command == "reverse") {
      iss >> Arg1 >> Arg2;
      std::reverse(Source.begin() + Arg1, Source.begin() + Arg2 + 1);
    } else if (Command == "replace") {
      iss >> Arg1 >> Arg2 >> Arg3;
      Source.replace(Arg1, Arg2 - Arg1 + 1, Arg3);
    }
    iss.clear();
  }

  return 0;
}

Fixed submission:
/**
 * ???????????????

????????? str
?????????????????????????????????????????????????????????????????°???????????????????????????????????????????????\?????????????????????????????§??????

print a b: str ??? a ??????????????? b ??????????????§??????????????????
reverse a b: str ??? a ??????????????? b ??????????????§?????????????????????
replace a b p: str ??? a ??????????????? b ??????????????§??? p
?????????????????????

????????§??????????????? str ????????????????????? 0 ???????????¨????????????

Input
1 ?????????????????? str ????????????????????????str
?????±?°????????????????????????????2 ????????????????????° q
?????????????????????????¶???? q
??????????????????????¨??????¢?????§?????????????????????

Output
??? print ???????????¨???????????????????????????????????????????????????

Constraints
1???str????????????1000
1???q???100
0???a???b<str?????????
replace ????????§??? b???a+1=p?????????

Sample Input 1
abcde
3
replace 1 3 xyz
reverse 0 2
print 1 4

Sample Output 1
xaze

Sample Input 2
xyz
3
print 0 2
replace 0 2 abc
print 0 2

Sample Output 2
xyz
abc
 */

#include <algorithm>
#include <iostream>
#include <sstream>

int main(int argc, char const *argv[]) {
  std::string Source;
  std::string Param;
  unsigned short q = 0;
  std::istringstream iss;

  getline(std::cin, Source);
  getline(std::cin, Param);
  iss.str(Param);
  iss >> q;
  iss.clear();

  for (size_t i = 0; i < q; i++) {
    std::string Command, Arg3;
    unsigned short Arg1, Arg2;
    getline(std::cin, Param);
    iss.str(Param);
    iss >> Command;
    if (Command == "print") {
      iss >> Arg1 >> Arg2;
      std::string Dest;
      Dest = Source.substr(Arg1, Arg2 - Arg1 + 1);
      std::cout << Dest << std::endl;
    } else if (Command == "reverse") {
      iss >> Arg1 >> Arg2;
      std::reverse(Source.begin() + Arg1, Source.begin() + Arg2 + 1);
    } else if (Command == "replace") {
      iss >> Arg1 >> Arg2 >> Arg3;
      Source.replace(Arg1, Arg2 - Arg1 + 1, Arg3);
    }
    iss.clear();
  }

  return 0;
}