811
备注:
|
1385
|
删除的内容标记成这样。 | 加入的内容标记成这样。 |
行号 1: | 行号 1: |
## page was renamed from 程序设计练习27——zju2478——Encoding | |
行号 2: | 行号 3: |
Time limit: 1 Seconds | Time limit: 1 Seconds |
行号 4: | 行号 5: |
Memory limit: 32768K | Memory limit: 32768K |
行号 10: | 行号 11: |
行号 13: | 行号 12: |
The first line contains an integer N (1 <= N <= 100) which indicates the number of test cases. The next N lines contain N strings. Each string consists of only 'A' - 'Z' and the length is less than 100. |
The first line contains an integer N (1 <= N <= 100) which indicates the number of test cases. The next N lines contain N strings. Each string consists of only 'A' - 'Z' and the length is less than 100. |
行号 18: | 行号 15: |
行号 27: | 行号 23: |
行号 34: | 行号 29: |
行号 35: | 行号 31: |
{{{#!cplusplus /*written by 洪峰 AC了哦*/ #include<iostream> using namespace std; int main() { char s[200],temp; int i,j,n,flag; cin>>n; for (i=0;i<n;i++) { cin>>s; temp=s[0]; flag=1; for (j=1;j<strlen(s);j++) if (temp==s[j]) flag++; else { if (flag==1) cout<<temp; else cout<<flag<<temp; flag=1; temp=s[j]; } if (flag==1) cout<<temp<<endl; else cout<<flag<<temp<<endl; } system("pause"); return 0; } }}} |
Encoding
Time limit: 1 Seconds
Memory limit: 32768K
Given a string containing only 'A' - 'Z', we could encode it using the following method:
- Each sub-string containing k same characters should be encoded to "kX" where "X" is the only character in this sub-string.
- If the length of the sub-string is 1, '1' should be ignored.
1. Input
The first line contains an integer N (1 <= N <= 100) which indicates the number of test cases. The next N lines contain N strings. Each string consists of only 'A' - 'Z' and the length is less than 100.
2. Output
For each test case, output the encoded string in a line.
3. Sample Input
2 ABC ABBCCC
4. Sample Output
ABC A2B3C
http://acm.zju.edu.cn/show_problem.php?pid=2478
1 /*written by 洪峰
2 AC了哦*/
3
4 #include<iostream>
5 using namespace std;
6
7 int main()
8 {
9 char s[200],temp;
10 int i,j,n,flag;
11 cin>>n;
12 for (i=0;i<n;i++)
13 {
14 cin>>s;
15 temp=s[0];
16 flag=1;
17 for (j=1;j<strlen(s);j++)
18 if (temp==s[j])
19 flag++;
20 else
21 {
22 if (flag==1)
23 cout<<temp;
24 else
25 cout<<flag<<temp;
26 flag=1;
27 temp=s[j];
28 }
29 if (flag==1)
30 cout<<temp<<endl;
31 else
32 cout<<flag<<temp<<endl;
33 }
34 system("pause");
35 return 0;
36 }