字符统计
来源:互联网 发布:淘宝买岛国片怎么搜索 编辑:程序博客网 时间:2024/05/18 03:46
Problem Description
Givena string containing only 'A' - 'Z', we could encode it using the followingmethod:
1.Each sub-string containing k same characters should be encoded to"kX" where "X" is the only character in this sub-string.
2. Ifthe length of the sub-string is 1, '1' should be ignored.
Input
Thefirst line contains an integer N (1 <= N <= 100) which indicates thenumber of test cases. The next N lines contain N strings. Each string consistsof only 'A' - 'Z' and the length is less than 10000.
Output
Foreach test case, output the encoded string in a line.
Sample Input
2
ABC
ABBCCC
Sample Output
ABC
A2B3C
参考代码:
#include<iostream>using namespace std;int main(){string s;int i,count;char c;cin>>s;c=s[0];count=1; for(i=1;i<s.length();i++){if(c==s[i]){count++;}else{if(count>1)cout<<count;cout<<c;c=s[i];count=1;}}if(count>1)cout<<count;cout<<c<<endl;return 0;}
测试结果:
0 0
- 字符统计
- 统计字符
- 统计字符
- 统计字符
- 统计字符
- 字符统计
- 字符统计
- 字符统计
- 字符统计
- 统计字符
- 统计字符
- 字符统计
- 统计字符
- 统计字符
- 字符统计
- 字符统计
- 字符统计
- 字符统计
- 校招面试题
- 图像读取及基本处理程序片段
- 第一次发表博客
- 15HD_OJ——最少拦截系统
- C++标准库学习笔记(Weak Pointer)-4
- 字符统计
- [].join.call(arguments,"\n")
- java Tomcat数据库连接池
- dbca.sh
- Zend framework2 - 在视图助手中设置元素样式,对所有元素,如multiCheckbox或错误信息加上自定义样式
- 判断一个数组是否是二叉搜索树的后序遍历(java)
- PHP PDO select语句结果行数计算
- 转--大数据学习路线
- Java多线程 synchronized锁的应用