problem 1320(当时无法AC原因 没有对输入进行限制 如果输入100000(超过10^30)A,则会溢出重回一个比较小的数)
2011-03-08 22:46
645 查看
Description
Run-length encoding is a simple compression technique which compresses strings of letters by replacing repeated consecutive letters (called runs) by the number of occurrences of the letter, followed by that letter. For example, AAAABBBCDDE compresses to 4A3BC2DE. The number 1 may be omitted in runs consisting of a single letter, as with letters 'C' and 'E' in the previous example.
Any string consisting of uppercase letters where each letter is optionally preceded by a positive integer is called a properly encoded string. Given a properly encoded string text, you must get the decoded string. If the decoded string would be more than 50 characters long, return "TOO LONG" (without the quotes).
Input
There are serveal test case , each case contain a string(the length no more than 50).
Output
For each case , output the decoded string or "TOO LONG"
Sample Input
4A3BC2DE
21Z13S9A8M
Sample Output
AAAABBBCDDE
TOO LONG
Hint
The decoded string would be more than 10^30 characters long, which is more than 50.
Source
Buptacm
自己的:
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
char a[50];
int b[50];
for(int i=0;i<50;i++)
b[i]=0;
while(cin>>a)
{
int i=0,j=0,k=0,m=0;
while(a[i]!='/0')
{
while((a[i]<='9')&&(a[i]>='0'))
{
j=j*10+(int(a[i])-48);
i++;
if(j>50) //对J限制 超过50直接判错!!!!!
{
cout<<"TOO LONG";
goto qq;
}
}
while((a[i]<='Z'&&a[i]>='A'))
{
if(j==0)
{b[k]=1;}
else
{b[k]=j;}
j=0;
i++;
m=m+b[k];
k++;
}
}
if(a[i]=='/0')
{
if(m>50)
cout<<"TOO LONG";
else
{
i=0,j=0;
while(a[i]!='/0')
{
while((a[i]<='9')&&(a[i]>='0'))
{
j=j*10+(int(a[i])-48);
i++;
}
while((a[i]<='Z'&&a[i]>='A'))
{
cout<<a[i];
while(j>1)
{
cout<<a[i];
j--;
}
j=0;
i++;
}
}
}
}
qq:
cout<<endl;
}
return 0;
}
学到的:字符串中的数字转字符 强制转换INT后减去0的ASCII码48 int('9')-48;
Run-length encoding is a simple compression technique which compresses strings of letters by replacing repeated consecutive letters (called runs) by the number of occurrences of the letter, followed by that letter. For example, AAAABBBCDDE compresses to 4A3BC2DE. The number 1 may be omitted in runs consisting of a single letter, as with letters 'C' and 'E' in the previous example.
Any string consisting of uppercase letters where each letter is optionally preceded by a positive integer is called a properly encoded string. Given a properly encoded string text, you must get the decoded string. If the decoded string would be more than 50 characters long, return "TOO LONG" (without the quotes).
Input
There are serveal test case , each case contain a string(the length no more than 50).
Output
For each case , output the decoded string or "TOO LONG"
Sample Input
4A3BC2DE
21Z13S9A8M
Sample Output
AAAABBBCDDE
TOO LONG
Hint
The decoded string would be more than 10^30 characters long, which is more than 50.
Source
Buptacm
自己的:
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
char a[50];
int b[50];
for(int i=0;i<50;i++)
b[i]=0;
while(cin>>a)
{
int i=0,j=0,k=0,m=0;
while(a[i]!='/0')
{
while((a[i]<='9')&&(a[i]>='0'))
{
j=j*10+(int(a[i])-48);
i++;
if(j>50) //对J限制 超过50直接判错!!!!!
{
cout<<"TOO LONG";
goto qq;
}
}
while((a[i]<='Z'&&a[i]>='A'))
{
if(j==0)
{b[k]=1;}
else
{b[k]=j;}
j=0;
i++;
m=m+b[k];
k++;
}
}
if(a[i]=='/0')
{
if(m>50)
cout<<"TOO LONG";
else
{
i=0,j=0;
while(a[i]!='/0')
{
while((a[i]<='9')&&(a[i]>='0'))
{
j=j*10+(int(a[i])-48);
i++;
}
while((a[i]<='Z'&&a[i]>='A'))
{
cout<<a[i];
while(j>1)
{
cout<<a[i];
j--;
}
j=0;
i++;
}
}
}
}
qq:
cout<<endl;
}
return 0;
}
学到的:字符串中的数字转字符 强制转换INT后减去0的ASCII码48 int('9')-48;
相关文章推荐
- 为什么我上传了flv或MP4文件到服务器,可输入正确地址通过http协议来访问总是出现“无法找到该页”的404错误呢?这就表明mp4格式文件是服务器无法识别的,其实,这是没有在iis中将相应的MIME类型进行设置的原因。那该怎样设置MIME
- 编写函数,对标准输入进行扫描,并对“the”出现的次数进行计数。 进行比较时应区分大小写。认为一个单词有一个或多个空格字符分隔, 并且输入行在长度上不会超过100个字符,计数结果输出。
- C#“如果应用程序没有控制台或控制台输入已通过文件进行了重定向,则无法读取键。”的解决方法
- 已知一个几乎有序的数组,几乎有序是指,如果把数组排好顺序的话,每个元素移动的距离可以不超过k,并且k相对于数组来说比较小。请选择一个合适的排序算法针对这个数据进行排序。 给定一个int数组A,同时给定
- SQL SERVER 2005 无法使用SQL SERVER MANAGERMET STUDIO 登陆(原因是没有使用默认端口进行远程连接所造成的)
- Struts2文件上传 超过大小限制无法跳转至指定input页面问题,浏览器提示连接已重置?根本原因
- jarsigner: 无法对 jar 进行签名: 时间戳颁发机构没有响应。 如果要从防火墙后面连接, 则可能需要指定 HTTP 代理。请为 jarsigner 提供以下选项
- 在项目中用到datatables比较多,但是当datatables加载超过1000多的时候就会出现有点缓慢的的样子,这个时候如果继续增加数据量让datatables去处理,那会是一个让人抓狂的事情,幸
- 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字。例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}。由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2。如果不存在
- 题目:将一个正整数分解质因数。例如:输入90,打印出90=2*3*3*5。 解答: 分析:对n进行分解质因数,应先找到一个最小的质数k,然后按下述步骤完成: (1)如果这个质数恰等于n,则说明分解
- 用CSocket基于UDP协议进行网络通信过程中OnReceive函数无法响应消息的一个原因
- 如果某一个对象没有提供创建函数,或创建函数无法达到期望的效果,如果创建新的这个类型的对象?
- :输入10个整数,保存在一个数组中,在数组中查找某个数,给出是否找到的信息。如果找到了,要求输出该数在数组中所处的位置;如果找不到,输出“没有找到!
- 【程序15】TestCompare.java 题目:输入三个整数x,y,z,请把这三个数由小到大输出。 1.程序分析:我们想办法把最小的数放到x上, //先将x与y进行比较,如果x>y则将x与y的值
- //写一个生成10个100以内随机数数据,再进行冒泡排序,顺序,//二分查找法找到一个值得位置,如果没有则为-1,有则返回数组位置
- 结构体如果没有重载比较运算符,不能直接进行比较
- p16.2编写一个程序,从标准输入读取几行输入,每行输入都要打印到标准输出上,前面要加上行号,书入行的长度没有限制
- 某朋友说,如果存款超过100万,则入手宝马X5,否则超过50万,则 1. 入手奥迪A6,否则超过20万,则入手比亚迪,否则超过10万则玩玩极品飞车游戏,请编写一个购车选择程序,根据输入的金额,提示可以
- 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字。例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}。由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2。如果不存在
- C# VS中进行编译发现没有错误但是无法生成的原因