Source: http://cs101.openjudge.cn/practice/12556/ 12556: encoded string
Total time limit: 1000ms memory limit: 65536kB
Description
In data compression, a common method is the stroke length encoding compression. For a string to be compressed, we can sequentially record each character and the number of repetitions. For example, the string to be compressed is "AAABBBBCBB", and the compression result is (a,3) (b,4) (c,1) (b,2). This compression is effective in cases where the adjacent data is repeated more often, and the compression is less efficient if there are fewer repetitions.
It is now required to first convert all uppercase letters in a string to lowercase letters based on the input string, and then compress the strings.
input
A string with a length greater than 0 and no more than 1000, all of which consist of uppercase or lowercase letters.
Output
The output is the encoded string, in the form: (a,3) (b,4) (c,1) (d,2), which is the lowercase character and the number of repetitions in each pair of parentheses, with no spaces.
Sample Input
Aaabbbbcccaaaaa
Sample Output
(a,3) (b,4) (c,3) (a,5)
-----------------------------------------------------
Thinking of solving problems
Simulation questions
-----------------------------------------------------
Code
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main ()
{
string s;
Cin >> S;
int len = s.length (), i = 0;
for (i=0; i<len; i++)
{
if (s.at (i) >= ' a ' && s.at (i) <= ' Z ')
{
s.at (i)-= ' A '-' a ';
}
}
int cnt = 1;
char c = 0;
Vector<char> VECC;
Vector<int> Veci;
Vecc.push_back (s.at (0));
for (I=1; i<len; i++)
{
if (s.at (i)! = s.at (i-1))
{
vecc.push_back (s.at (i));
Veci.push_back (CNT);
CNT = 1;
}
else
{
cnt++;
}
}
Veci.push_back (CNT);
for (i=0; I<vecc.size (); i++)
{
cout << ("<< vecc.at (i) <<", "<< veci.at (i) <&L T ")" ;
}
}