HDU1020 Encoding
来源:互联网 发布:大富豪棋牌源码论坛 编辑:程序博客网 时间:2024/05/22 00:40
Encoding
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 36924 Accepted Submission(s): 16374
Problem Description
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.
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.
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 10000.
Output
For each test case, output the encoded string in a line.
Sample Input
2ABCABBCCC
Sample Output
ABCA2B3C
题目大意:
题目的意思就是要我们求出相同子字符串序列的个数并表示出来,题目还告诉我们,所有的字符都是 'A' - 'Z' 之间;并且如果该段连续的该字符只有一个则不需要表示出来,否则需要表示出来;
分析:
由题目所给的输入测试数据得到的输出数据 INPUT: ABBCCC OUTPUT:A2B3C; 我们可以知道,连续段只要有相同的字符只表示出一个就好了,那么我们可以通过从后面往前面覆盖的方式来得到正确的输出字符,然后通过整型数组记录改字符在这连续段出现的次数;
覆盖相同的字符:
例如:字符串 str =" ABBCCC " 首先我们让 str[0]='A'; 然后定义一个遍历指针,让该指针指向第二个字符,那么就有 str[1]='B'; str[2]='B'; str[3]='C'; str[4]='C'; str[5]='C'; 然后我们只需要每次都判断下前面的和后面的是否相等,如果相等的话那么就依次覆盖前面的字符:如当我们找到 str[1]==str[2] 相等时,那么我们就把从str[2]开始的字符 依次向前面覆盖,并且将字符串长度缩短 1 (len--);那么就会有 str=" ABCCC " 直到最后遍历完成,变成了 str=" ABC ";
每次找同一段相同的字符时候,我们让其对应的整型数组中的值加 1 就好了;
给出AC代码:
#include<iostream>using namespace std;int main(){int n;cin >> n;char str[10000];int num[1000];while (n--){memset(num, 0, sizeof(num));cin >> str;int len;len = strlen(str);for (int i = 1; i < len; i++){if (str[i] == str[i - 1]) //如果前面的字符串和后面字符串相等则将前面的字符串覆盖。{for (int j = i; j < len; j++){str[j - 1] = str[j];}num[i-1]++;//每次找到相同的就加 1 ;len--;//覆盖之后需要将字符串缩短;i--; //防止指针没有缘由的向前,保证指针始终指向覆盖的位置;}}for (int i = 0; i < len; i++){if (num[i])cout << num[i]+1; //控制输出格式;输出的时候需把本身这个也算上,便多加个一;cout << str[i];}cout << endl;}return 0;}
1 0
- HDU1020 Encoding
- hdu1020 Encoding
- HDU1020:Encoding
- Encoding(HDU1020)
- HDU1020 Encoding
- HDU1020 Encoding
- hdu1020 - Encoding
- hdu1020 Encoding
- Encoding hdu1020 水题
- hdu1020(Encoding)
- hdu1020--Encoding HDU(133)
- hdu1020 Encoding(A2B3C)
- HDU1020 - Encoding (模拟)
- (解题报告)HDU1020---Encoding
- HDU1020 ZOJ2478 Encoding
- HDU1020——Encoding
- hdu1020
- hdu1020
- Spring进阶之路(1)-Spring核心机制:依赖注入/控制反转
- Eclipse Plugin Development 从现在开始
- 在Eclipse中导出带有自己签名的Android apk文件
- 什么样的项目算是成功的?项目目标有什么特点?
- Windows Embedded Standard 7 快速入门指南 1 of 4
- HDU1020 Encoding
- Extjs4.1 - Ext.util工具包详解
- Linux下使用popen()执行shell命令
- Android系统架构
- String的subString()方法实现内部机制
- 欢迎使用CSDN-markdown编辑器
- 给网站的URL加上锚点#
- Fast R-CNN论文笔记《Fast R-CNN》
- Injection for Xcode:成吨的提高开发效率