K - Run-Length Encoding, Run!
2989 단어 시뮬레이션
The compression scheme is rather simple. When encoding a text string, repeated consecutive characters are replaced by a single instance of that character and the number of occurrences of that character (the character’s run length). Decoding the encoded string results in the original string by repeating each character the number of times encoded by the run length. Forrest calls this encoding scheme run-length encoding. (We don’t think he was actually the first person to invent it, but we haven’t mentioned that to him.)
For example, the string HHHeelllo is encoded as H3e2l3o1. Decoding H3e2l3o1 results in the original string. Forrest has hired you to write an implementation for his run-length encoding algorithm.
Input
Input consists of a single line of text. The line starts with a single letter: E for encode or D for decode. This letter is followed by a single space and then a message. The message consists of 1
to 100
characters.
Each string to encode contains only upper- and lowercase English letters, underscores, periods, and exclamation points. No consecutive sequence of characters exceeds 9
repetitions.
Each string to decode has even length. Its characters alternate between the same characters as strings to encode and a single digit between 1
and 9
, indicating the run length for the preceding character.
Output
On an input of E output the run-length encoding of the provided message. On an input of D output the original string corresponding to the given run-length encoding.
Sample Input 1
Sample Output 1
E HHHeellloWooorrrrlld!!
H3e2l3o1W1o3r4l2d1!2
Sample Input 2
Sample Output 2
D H3e2l3o1W1o3r4l2d1!2
HHHeellloWooorrrrlld!!
#include
#include
using namespace std;
int main()
{
char ch, str[15000], str1[15000];
cin >> ch >> str;
int len = strlen(str);
int num = 0;
if(ch == 'E')
{
char c = str[0];
for(int i=1;i= '1' && str[i] <='9')
{
for(int j=0;j
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
파이썬으로 주가를 시뮬레이션 해 보았습니다.파이썬을 사용하여 미래의 주가를 시뮬레이션하려고합니다. 상장 지수 펀드 TOPIX (1308)의 2011/7/22 ~ 2021/7/21 데이터에 대한 분석 전 데이터의 5%를 이상, 95%를 정상이라고 가정하고, O...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.