ZUFE 1035 字符宽度编码(字符串)
Time Limit: 1 Sec Memory Limit: 128 MB
Description
你的任务是编写一个程序实现简单的字符宽度编码方法。规则如下:
将任何2~9个相同字符的序列编码成2个字符:第1个字符是序列的长度,用数字字符2~9表示,第2个字符为这一串相同字符序列中的字符。超过9个相同字符
构成的序列编码方法是先编码前面9个字符,然后再编码剩余的字符。
将任何不包括连续相同字符的序列编码成:先是字符“1”,然后是字符序列本身,最后还是字符“1”。如果字符“1”是序列中的字符,则对每个“1”
用两个字符“1”替换。
例如,字符串“12142”,编码后为“111211421”。这是因为这个字符串没有连续相同的字符,则编码后前后都是字符1,中间是字符串本身,而字符串本身又
包含了两个“1”对每个“1”,用两个“1”替换。
Input
输入文件包含若干行,每行的字符都是大小写字母字符、数字字符或标点符号,没有其他字符。
Output
对输入文件中每行进行字符宽度编码,并输出。
Sample Input
Sample Output
#include <cstdio>
#include <iostream>
#include <string>
#include <sstream>
#include <cstring>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#include <map>
using namespace std;
//#define LOCAL
struct Node
{
int cnt;
string str;
};
int main()
{
#ifdef LOCAL
freopen("in.txt", "r", stdin);
#endif // LOCAL
//Start
char a[];
memset(a,,sizeof a);
Node tmp;
while(cin>>a)
{
queue<Node>q;
while(!q.empty())q.pop();
tmp.cnt=,tmp.str.clear();
tmp.str.push_back(a[]);
for(int i=,len=strlen(a); i<len; i++)
{
if(tmp.str.size()==)
{
if(*(--tmp.str.end())==a[i])
{
tmp.cnt++;
if(i==len-)q.push(tmp);
}
else
{
if(tmp.cnt==)
{
tmp.str.push_back(a[i]);
tmp.cnt++;
}
else
{
q.push(tmp);
tmp.cnt=;
tmp.str.clear();
tmp.str.push_back(a[i]);
}
if(i==len-)q.push(tmp);
}
}
else
{
if(*(--tmp.str.end())==a[i])
{
tmp.str.erase(--tmp.str.end()),tmp.cnt--;
//tmp.str.push_back('1');
q.push(tmp);
tmp.cnt=;
tmp.str.clear();
tmp.str.push_back(a[i]);
if(i==len-)q.push(tmp);
}
else
{
tmp.str.push_back(a[i]),tmp.cnt++;
if(i==len-)q.push(tmp);
}
}
}
while(!q.empty())
{
tmp=q.front();
q.pop();
if(tmp.str.size()==&&tmp.cnt!=)printf("%d%c",tmp.cnt,tmp.str[]);
else
{
cout<<"";
string::iterator it=tmp.str.begin();
for(;it!=tmp.str.end();it++)
{
if(*it=='')cout<<"";
else cout<<*it;
}
cout<<"";
}
}
printf("\n");
}
return ;
}
ZUFE 1035 字符宽度编码(字符串)的更多相关文章
- day4-基础 字符串操作,文件操作,字符转编码
1.字符串用法 name = 'daniel' print(name.capitalize()) #首字母大写 >>>daniel print(name.count('a')) #统 ...
- php将长字符串拆分为指定最大宽度的字符串数组
/** * 将字符串拆分为指定最大宽度的字符串数组.单字节字符宽度为1,多字节字符通常宽度为2 * @param string $msg 要拆分的字符串 * @param int $width 结果数 ...
- Base-64 字符数组或字符串的长度无效等问题解决方案
项目特殊需要,调用ActiveX三维控件进行控件某一特殊部位的截图操作,这个截图保存由ActiveX控件控制保存到本地是没问题的,现在需要将这个截图上传到服务器,多人共享,就牵扯到需要读取本地文件…… ...
- WEB开发中的字符集和编码
html,body,div,span,applet,object,iframe,h1,h2,h3,h4,h5,h6,p,blockquote,pre,a,abbr,acronym,address,bi ...
- 中文字符串转换为十六进制Unicode编码字符串
package my.unicode; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Uni ...
- SQL Server获取下一个编码字符串的实现方案分割和进位
我在前一种解决方案SQL Server获取下一个编码字符实现和后一种解决方案SQL Server获取下一个编码字符实现继续重构与增强两篇博文中均提供了一种解决编码的方案,考虑良久对比以上两种方 ...
- C#和VC++字符集和编码
C# char 关键字用于声明 .NET framework 使用 Unicode 字符表示 System.Char 结构的实例. Char 对象的值是 16 位数字 (序号值.)将字符表示为 UTF ...
- 有关UNICODE、ANSI字符集和相关字符串操作
Q UNICODE字符串如何显示 A 如果程序定义了_UNICODE宏直接用 WCHAR *str=L"unicodestring"; TextOut(0,0,str); 否则就需 ...
- 2017计算机学科夏令营上机考试-B编码字符串
B:编码字符串 总时间限制: 1000ms 内存限制: 65536kB 描述 在数据压缩中,一个常用的方法是行程长度编码压缩.对于一个待压缩的字符串,我们可以依次记录每个字符及重复的次数.例如,待 ...
随机推荐
- Word 2016插入公式快捷键
实用的插入公式快捷键"Alt+=", 与君共享
- Shell终端收听音乐--网易云音乐命令行版
Musicbox:网易云音乐命令行版本 高品质网易云音乐命令行版本,简洁优雅,丝般顺滑,基于Python编写. 这款命令行的客户端使用 Python 构建,以 mpg123 作为播放后端: Vim 式 ...
- [读书笔记]telnet与http服务器一次直接对话
1.打开电脑telnet客户端应用 控制面板 >程序和功能 > 打开或者关闭windows功能 > telnet客户端 勾选,并确认. 2.执行telnet命令 a:cmd进入控制台 ...
- [其他]Android SDK离线文件路径以及安装更新方法
一.离线安装Android SDK文件路径 转载自:http://www.oschina.net/code/snippet_1539302_45940 Google TV Addon, Android ...
- Objective-C Runtime 运行时之四:Method Swizzling(转载)
理解Method Swizzling是学习runtime机制的一个很好的机会.在此不多做整理,仅翻译由Mattt Thompson发表于nshipster的Method Swizzling一文. Me ...
- ECS活动真实IP (前端存在SLB)
log_format main 'realip:$http_x_forwarded_for slbip:$remote_addr-$remote_user [$time_local] "$r ...
- ueditor的工具按钮配置
定制工具栏图标 UEditor 工具栏上的按钮列表可以自定义配置,只需要通过修改配置项就可以实现需求 配置项修改说明 修改配置项的方法: 1. 方法一:修改 ueditor.config.js 里面的 ...
- datagrid、easyui-dialog
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- Time Complexity Big-O
It can be inserted anywhere. Note that if you insert it in the beginning the TC will be O(#s +c), bu ...
- C#用 excel 作为模板打印
//打印操作,套打.打印.预览 enum PrintFlag { /// <summary> /// 套打,只打印没 ...