map初步(由ABBC--->A2BC)
2024-09-09 20:34:47
1.题目:
Given a string containing only 'A' - 'Z', we could encode it using the following method:
1. Each sub-string containing k same characters should be encoded to "kX" where "X" is the only character in this sub-string.
2. If the length of the sub-string is 1, '1' should be ignored.
InputThe 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 10000.
OutputFor each test case, output the encoded string in a line.
Sample Input
2
ABC
ABBCCC
Sample Output
ABC
A2B3C 2.代码:
//本题使用G++编译
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--){
int sum=;
string a;//定义字符数组
cin>>a;//输入字符数组内容
for(int i=;i<a.length();i++){
if(a[i]==a[i+])
sum++;//若相邻两个字符相同则sum+1
else//若相邻字符不相同则进行else
{
if(sum!=)//若sum!=1,即相邻字符相同则输出sum再输出字符
cout<<sum<<a[i];
else//若相邻字符不相同则直接输出字符
cout<<a[i];
sum=;
}
}
cout<<endl;
}
}
3.总结:
应用了map算法!!!
最新文章
- jquery选择器(综合)
- 控制Wordpress对搜索引擎的可见性
- 打包.a 文件时, build phases- Link Binary With Libraries
- Class org.apache.struts2.json.JSONWriter can not access a member of class org.springframework.aop.TruePointcut with modifiers ";public";
- Python Tomcat Script(多实例)
- mysql 日期 时间戳 转换
- hdu 3594 仙人掌图
- 设计模式 Template Method模式 显示程序猿的一天
- Linux 典型应用之常用命令
- [TFS教程]TFS: Get Command
- DirectX SDK (June 2010)安装错误S1023,解决方法
- 【小程序】页面无法更新tabbar角标属性时
- vim改善生活的几个插件
- 通过TortoiseSVN进行文件(夹)外链 External File
- 踩坑记(1)——使用slf4j+logback记录日志
- C# Atomic<;T>; Generic
- Check类中的incl、union,excl,diff,intersect
- lua闭包与简易迭代器实现
- flex作图
- 理解HashSet及使用