POJ-1782 Run Length Encoding-相同字元個數
Run Length Encoding
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 4331 | Accepted: 1406 |
Description
Your task is to write a program that performs a simple form of run-length encoding, as described by the rules below.
Any sequence of between 2 to 9 identical characters is encoded by two characters. The first character is the length of the sequence, represented by one of the characters 2 through 9. The second character is the value of the repeated character. A sequence of more than 9 identical characters is dealt with by first encoding 9 characters, then the remaining ones.
Any sequence of characters that does not contain consecutive repetitions of any characters is represented by a 1 character followed by the sequence of characters, terminated with another 1. If a 1 appears as part of the sequence, it is escaped with a 1, thus two 1 characters are output.
Any sequence of between 2 to 9 identical characters is encoded by two characters. The first character is the length of the sequence, represented by one of the characters 2 through 9. The second character is the value of the repeated character. A sequence of more than 9 identical characters is dealt with by first encoding 9 characters, then the remaining ones.
Any sequence of characters that does not contain consecutive repetitions of any characters is represented by a 1 character followed by the sequence of characters, terminated with another 1. If a 1 appears as part of the sequence, it is escaped with a 1, thus two 1 characters are output.
Input
The input consists of letters (both upper- and lower-case), digits, spaces, and punctuation. Every line is terminated with a newline character and no other characters appear in the input.
Output
Each line in the input is encoded separately as described above. The newline at the end of each line is not encoded, but is passed directly to the output.
Sample Input
AAAAAABCCCC 12344
Sample Output
6A1B14C 11123124
Source
Ulm Local 2004
#include<stdio.h>
#include<string.h>
int main()//空格也算字元
{
char str[200];
int i,j,cnt;
while(gets(str))
{
if (!str[0])
{
putchar(10);
continue;
}
cnt=1;
for(i=0; str[i]; i++)
{
if (str[i] == str[i+1])
{
for ( j = i ; str[i] == str[i+1] && (i-j) < 8 ; i++ );
printf("%d%c", i-j+1, str[i]);
}
else
{
printf("1");
for(j=i; str[j]; j++)
{
if(str[j]!=str[j+1])
{
if(str[j]=='1')
printf("1");
printf("%c",str[j]);
}
else
break;
}
printf("1");
i=j-1;
}
}
printf("\n");
memset(str, 0, sizeof(str));
}
return 0;
}
相關文章
- 輸入兩個長度相同的字串,比較兩個數在相同位置的字元是否相同字串字元
- 如何獲取字串中相同字元出現的次數字串字元
- 從Function.length 與 Argument.length 區別談到如何傳遞任意個數引數Function
- 編寫判斷一個字元序列是否為迴文。迴文是指一個字元序列以中間 字元為基準兩邊字元完全相同,即順著看和倒著看是相同的字元序列。字元
- Emoji.prototype.length —— Unicode 字元那些事兒Unicode字元
- JavaScript 比較相同的字元返回falseJavaScript字元False
- 統計字串字元個數字串字元
- 如何確定一個字串中是否所有字元全部互不相同字串字元
- js 英文中文混擷取 相同個數JS
- 040統計數字字元的個數字元
- 利用HashMap統計字元個數HashMap字元
- 關於SAP ABAP字元變數和字串變數字元個數的一個知識點,和一個血案字元變數字串
- mysql相同數值排序MySql排序
- java查詢字串裡與指定字串相同的個數Java字串
- Matlab統計陣列中相同元素的個數Matlab陣列
- 1102:與指定數字相同的數的個數(C C++)C++
- C++語言演算法之求任意兩個相同字元的最大距離C++演算法字元
- mysql char_length和lengthMySql
- Java中 length、length()、size()區別Java
- 如何用Python統計不同字元個數?Python字元
- C語言: 分類統計字元個數C語言字元
- 關於字串中取相同的字元問題(小學題)字串字元
- 怎麼實現名稱相同,引數不同的多個介面
- MySQL查詢某個列中相同值的數量統計MySql
- . 【JAVA】給定任意 字串"yekmaakkccekymbvb",求出字串中有多少種字元,以及每個字元的個數?Java字串字元
- 兩個相同路徑、不同包、相同名稱類載入
- 如何在MATLAB中統計陣列中相同元素的個數?Matlab陣列
- C語言:計算輸入字元的個數C語言字元
- C語言計算輸入字元的個數C語言字元
- JavaScript統計字串中重複字元的個數JavaScript字串字元
- jQuery限制“Text-Area”域中的字元的個數jQuery字元
- 獲取一個字串中出現最多的字元和他的個數字串字元
- for迴圈得到的隨機數相同隨機
- 字元數統計字元
- 比較兩個table是否相同
- isAlnum判斷字元是否為字母數字字元(字母和數字都屬於字母數字字元)字元
- 將一個字串中含有全形的數字字元、字母、空格或'%+-()'字元轉換為相應半形字元字串字元
- 1374 生成每種字元都是奇數個的字串字元字串