Time limit:1 Sec Memory limit:128 MB
submit:117 solved:16
[Submit] [Status] [Web Board] Description
Your task is to write a program that implements a simple character-width coding method. The rules are as follows:
Encodes any sequence of 2~9 of the same character into 2 characters: The 1th character is the length of the sequence, denoted by a numeric character 2~9, and the 2nd character is a string of characters in the same sequence of characters. More than 9 of the same characters
The sequence encoding method consists of encoding the first 9 characters before encoding the remaining characters.
Encode any sequence that does not include consecutive identical characters: first the character "1", then the character sequence itself, and finally the character "1". If the character "1" is a character in a sequence, then each "1"
Replace with two characters "1".
For example, the string "12142" is encoded with "111211421". This is because the string does not have the same character consecutively, then it is preceded by the character 1, the string itself, and the string itself
Contains two "1" for each "1", replaced with two "1".
Input
The input file contains several lines, each with uppercase and lowercase characters, numeric characters, or punctuation marks, with no other characters.
Output
Encodes the character width for each line in the input file and outputs it.
Sample InputAAAAAABCCCCSample Output6a1b14c Address: (Requires VPN) http://172.21.2.10/JudgeOnline/problem.php?id=1035The first thought is that the string is segmented, counted on each character, followed by a careful look at the repetition of the character count, not repeating the original output, at the end of the Add ' 1 ', and, if there is ' 1 ', then output two ' 1 '. So the idea is to use the structure to save the character count (not used to count, but the output is used to determine whether to add ' 1 ') and string, where the string is saved with string, feel more convenient. There is the detail section, in the sample "BCC" section, read to the second ' C ' when the "BC" in the ' C ' delete and put ' B ' into the queue, then empty the string, the ' C ' into a string, and the count to become 2. Finally, consider the case where the last digit of the character is read, and four cases are added to the code that reads to the last incoming queue.
#include <cstdio>#include<iostream>#include<string>#include<sstream>#include<cstring>#include<stack>#include<queue>#include<algorithm>#include<cmath>#include<map>using namespacestd;//#define LOCALstructnode{intCNT; stringstr;};intMain () {#ifdef LOCAL freopen ("In.txt","R", stdin);#endif //LOCAL //Start Chara[ -]; Memset (A,0,sizeofa); Node tmp; while(cin>>a) {queue<Node>Q; while(!q.empty ()) Q.pop (); Tmp.cnt=1, Tmp.str.clear (); Tmp.str.push_back (a[0]); for(intI=1, Len=strlen (a); i<len; i++) { if(tmp.str.size () = =1) { if(* (--tmp.str.end ()) = =A[i]) {tmp.cnt++; if(i==len-1) Q.push (TMP); } Else { if(tmp.cnt==1) {tmp.str.push_back (a[i]); Tmp.cnt++; } Else{Q.push (TMP); Tmp.cnt=1; Tmp.str.clear (); Tmp.str.push_back (A[i]); } if(i==len-1) Q.push (TMP); } } Else { if(* (--tmp.str.end ()) = =A[i]) {Tmp.str.erase (--tmp.str.end ()), tmp.cnt--; //tmp.str.push_back (' 1 ');Q.push (TMP); Tmp.cnt=2; Tmp.str.clear (); Tmp.str.push_back (A[i]); if(i==len-1) Q.push (TMP); } Else{tmp.str.push_back (a[i]), tmp.cnt++; if(i==len-1) Q.push (TMP); } } } while(!Q.empty ()) {tmp=Q.front (); Q.pop (); if(tmp.str.size () = =1&&tmp.cnt!=1) printf ("%d%c", tmp.cnt,tmp.str[0]); Else{cout<<"1"; string:: Iterator it=Tmp.str.begin (); for(; It!=tmp.str.end (); it++) { if(*it=='1') cout<<" One"; Elsecout<<*it; } cout<<"1"; }} printf ("\ n"); } return 0;}
Zufe 1035 Character width encoding