Broken Keyboard (a.k.a. Beiju Text) 思路
问题:You’re typing a long text with a broken keyboard. Well it’s not so badly broken. The only problem with the keyboard is that sometimes the “home” key or the “end” key gets automatically pressed (internally). You’re not aware of this issue, since you’re focusing on the text and did not even turn on the monitor! After you finished typing, you can see a text on the screen (if you turn on the monitor). In Chinese, we can call it Beiju. Your task is to find the Beiju text.
输入:
There are several test cases. Each test case is a single line containing at least one and at most 100,000 letters, underscores and two special characters ‘[’ and ‘]’. ‘[’ means the “Home” key is pressed internally, and ‘]’ means the “End” key is pressed internally. The input is terminated by end-of-file (EOF).
输出:
For each case, print the Beiju text on the screen
示例输入
This_is_a_[Beiju]_text
[[]][][]Happy_Birthday_to_Tsinghua_University
示例输出
BeijuThis_is_a__text
Happy_Birthday_to_Tsinghua_University
题意:假如你在打字,home键和end键不定时就会被按下,用[代表home,用]代表end,最终打出来的结果是什么?
(home键功能:光标跳到行开始.end:光标跳到行末尾)
思路:
1.最简单的想法是用链表做,用一个指针记录当前输入光标,每读一个字母就加一个节点. 但是代码比较复杂,懒得敲,
另解2.
用字符串做.
假设 [ 是1, ] 是2,
定义一个变量mod存放这个状态,刚开始默认mod=2
把字符一个个的读到c数组.
如果再读到[ 或 ],就判断mod,
if(mod==2){
strcat (b,c);
更改mod为新状态;
memset将c置零;
} if(mod=1){
strcat(c,b);
strcpy(b,c);
更改mod为新状态;
memset将c置零;
}
记得最后再多一步这个,把最后那段也放到b里.最后输出b即可.
另解3.确定字符串里哪部分先输出后输出
观察到输出时是最后面的 [后面的内容先输出,直到再遇到括号.其次是前一个[后面的内容,以此类推...等所有左括号的全输出完了,再把剩下的输出.
算法:
把字符串存到数组里:
先找到[字符的位置,存到b数组里,并把 [ 和] 都置0.注意,不要把]也存到b里.
然后根据b的索引,依次输出a[6]串,a[3]串,同时输出完就写0销毁,:
最后把剩下的输出即可,用putchar一个个的输出.
代码:
#include<stdio.h>
#include<string.h>
int main()
{
int b[100002];//存放 [ 的位置
char a[100002];//存放字符串
int k,i,lena;
while(gets(a)){//gets遇到EOF就返回0
k=1;
lena=strlen(a);
for(i=0;i<lena;i++){
if(a[i]=='['||a[i]==']'){
if(a[i]=='[') b[k++]=i; //把第K个 [ 的位置序号放到b[k]
a[i]='\0';//把[ ]位置写0
}
}
k--;//消去多加的那个 while(k){ //从最右边的 [ 开始,依次输出
printf("%s",a+b[k]+1);
memset(a+b[k]+1,0,strlen(a+b[k]+1));//输出完就写0销毁,防止下次重复输出
k--;
} for(i=0;i<lena;i++){//把剩下没有前置的字符全输出
if(a[i]!='\0') putchar(a[i]);
} putchar(10);//换行
}
return 0;
}
我用的第三种方法,注意memset的使用,以及循环的控制即可.
Broken Keyboard (a.k.a. Beiju Text) 思路的更多相关文章
- B - Broken Keyboard (a.k.a. Beiju Text)
Problem B Broken Keyboard (a.k.a. Beiju Text) You're typing a long text with a broken keyboard. Well ...
- uva - Broken Keyboard (a.k.a. Beiju Text)(链表)
11988 - Broken Keyboard (a.k.a. Beiju Text) You’re typing a long text with a broken keyboard. Well i ...
- 破损的键盘(悲剧文本)(Broken Keyboard(a.k.a. Beiju Text),Uva 11988)
破损的键盘(悲剧文本)(Broken Keyboard(a.k.a. Beiju Text),Uva 11988) 题意描述 你在输入文章的时候,键盘上的Home键和End键出了问题,会不定时的按下. ...
- N - Broken Keyboard (a.k.a. Beiju Text)(DFS,链表)
N - Broken Keyboard (a.k.a. Beiju Text) Time Limit:1000MS Memory Limit:0KB 64bit IO Format:% ...
- UVA——11988 Broken Keyboard (a.k.a. Beiju Text)
11988 Broken Keyboard (a.k.a. Beiju Text)You’re typing a long text with a broken keyboard. Well it’s ...
- UVA 11988 Broken Keyboard (a.k.a. Beiju Text)(链表)
题目代号:UVA 11988 题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&pa ...
- B - Broken Keyboard (a.k.a. Beiju Text) 数组模拟链表
You're typing a long text with a broken keyboard. Well it's not so badly broken. The only problem wi ...
- UVa 11988 Broken Keyboard (a.k.a. Beiju Text)(链表)
You're typing a long text with a broken keyboard. Well it's not so badly broken. The only problem wi ...
- Broken Keyboard (a.k.a. Beiju Text) UVA - 11988
You're typing a long text with a broken keyboard. Well it's not so badly broken. The only problem wi ...
随机推荐
- idea 项目转 eclipse项目
接到一个很紧急的活,我很着急,也很兴奋,打开邮件一看,有点懵逼. idea项目.idea不熟啊,网上搜攻略.我做个总结,归根结底就是一句话. 下个idea,然后一步一步的安装好. 然后也是 ...
- localStorage单页面及不同页面监听变动
分析 H5本地存储有两个API,一个是Web Storage,还有一个是Web SQL.不管是哪一个,都是基于JavaScript语言来使用,而Web Storage提供了两种存储类型 API: s ...
- GoldenGate实时投递数据到大数据平台(5) - Kafka
Oracle GoldenGate是Oracle公司的实时数据复制软件,支持关系型数据库和多种大数据平台.从GoldenGate 12.2开始,GoldenGate支持直接投递数据到Kafka等平台, ...
- Java学习笔记之Linux下的Java安装和配置
0x00 概述 由于使用 yum 或者 apt-get 命令 安装 openjdk 可能存在类库不全,从而导致用户在安装后运行相关工具时可能报错的问题,所以此处我们推荐采用手动解压安装的方式来安装 J ...
- 那些不错的 [ Html5 + CSS3 + Canvas ] 效果!
apng制作工具:http://isparta.github.io/how.html apng制作文章:http://isux.tencent.com/introduction-of-apng.htm ...
- mysql/mariadb应该使用utf8mb4而不是utf8
详情参考https://mp.weixin.qq.com/s?__biz=MzIwMzg1ODcwMw==&mid=2247487968&idx=1&sn=2ff7b511f6 ...
- rabbitmq级联之shovel插件和exchange.bind
有时候,由于各方面原因比如安全原因.系统间隔离,我们需要实现在多个rabbitmq实例或者一个rabbitmq实例的多个vhost间推送消息.在上一版的实现中,公司使用java自行实现了一个类似转发器 ...
- openstack cloud init set password
设置代理和password #!/bin/bash cd /home/ubuntu wget otcloud-gateway.bj.intel.com/script.tar.gz ]; then cu ...
- ldap集成jenkins
jenkins版本:2.5.3,ldap插件:1.15 jenkins ldap支持需要安装ldap plugin,强烈建议插件安装版本为1.15及以上(支持ldap 配置测试) 安装插件: 系统管理 ...
- Python中对象的引用与复制
在python进行像b = a这样的赋值时,只会创建一个对a的新引用,使a的引用计数加1,而不会创建新的对象: >>> a = 'xyz' >>> import s ...