Advertisement
Guest User

Untitled

a guest
Sep 25th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. class Token {
  8. public:
  9. int occurences;
  10. char value;
  11. private:
  12. };
  13.  
  14. void check_validity(string user_input) {
  15. for (int c : user_input) {
  16. if (c < 'a' || c > 'z') throw runtime_error("invalid input");
  17. }
  18. }
  19.  
  20. int main() {
  21. try {
  22. string input;
  23. vector<Token> results = {};
  24. int count = 1;
  25. cin >> input;
  26. check_validity(input);
  27.  
  28. for (int i = 0; i < input.size() - 1; i++) {
  29. if (input[i] == input[i++]) {
  30. count++;
  31. }
  32. else {
  33. Token t;
  34. t.occurences = count;
  35. t.value = input[i];
  36. results.push_back(t);
  37. }
  38. }
  39.  
  40. cout << count;
  41.  
  42. for (Token t : results) {
  43. cout << t.occurences << t.value;
  44. }
  45.  
  46. cout << endl;
  47. }
  48. catch (runtime_error& e) {
  49. cerr << "error: " << e.what() << endl;
  50. }
  51.  
  52. return 0;
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement