C. Fixing Typos----栈
来源:互联网 发布:mac os10.7.5如何升级 编辑:程序博客网 时间:2024/06/06 19:16
Many modern text editors automatically check the spelling of the user's text. Some editors even suggest how to correct typos.
In this problem your task to implement a small functionality to correct two types of typos in a word. We will assume that three identical letters together is a typo (for example, word "helllo" contains a typo). Besides, a couple of identical letters immediately followed by another couple of identical letters is a typo too (for example, words "helloo" and "wwaatt" contain typos).
Write a code that deletes the minimum number of letters from a word, correcting described typos in the word. You are allowed to delete letters from both ends and from the middle of the word.
The single line of the input contains word s, its length is from 1 to 200000 characters. The given word s consists of lowercase English letters.
Print such word t that it doesn't contain any typos described in the problem statement and is obtained from s by deleting the least number of letters.
If there are multiple solutions, print any of them.
helloo
hello
woooooow
woow
The second valid answer to the test from the statement is "heloo".
题目链接:http://codeforces.com/contest/363/problem/C
题目的意思是说给你一个字符串,现在需满足以下两个条件:
1.一个串若结尾的两个字符相同,则这个串后面的两个字符不能相同。
2.不存在连续相同的三个字符。
求最小的删除字符数。
我们可以用栈来模拟这个过程,按题意来说,有两种情况无法入栈
1. 如果当前的字母和栈顶的栈顶的字母相同,且栈顶第二个字母和栈顶第三个字母相同。
2. 如果当前字母和栈顶字母相同,且栈顶第二个字母和栈顶相同。
最后将栈内的元素从头到尾输出就好了。
代码:
#include <cstdio>#include <iostream>#include <cstring>using namespace std;char st[200010];char str[200010];int main(){ char ch; scanf("%s",str); int top=0; int len=strlen(str); st[top++]=str[0]; st[top++]=str[1]; for(int i=2;i<len;i++){ ch=str[i]; if(ch==st[top-1]&&st[top-2]==st[top-3]){ continue; } else if(ch==st[top-1]&&st[top-1]==st[top-2]){ continue; } else{ st[top]=ch; top++; } } st[top]='\0'; printf("%s\n",st); return 0;}
- C. Fixing Typos----栈
- C. Fixing Typos
- CodeForces 363 C. Fixing Typos
- CodeForce 363C Fixing Typos
- Codeforces 363C. Fixing Typos
- CodeForces 363C - Fixing Typos
- codeforces 363C Fixing Typos
- CodeForces 363C Fixing Typos
- Fixing Typos
- Problem - 363C - Codeforces --C. Fixing Typos
- Fixing Typos CodeForces
- Codeforces 363C Fixing Typos 贪心+字符串模拟
- STL 水题 Fixing Typos(string 容器)
- Fixing TCriticalSection
- Fixing AJAX: XMLHttpRequest
- Fixing Venture Capital
- Fixing "Base SDK Missing"
- Fixing the Errant Microphone
- 树状数组——三种用法详解
- 微信小程序支付中的prepay_id获取方法,以及微信支付统一签名算法闭坑
- 最长回文子串manacher算法
- Win/CentOS/MacOS Chrome/Firefox/IE/Safari DNS缓存时间与刷新
- JS事件循环详解
- C. Fixing Typos----栈
- ng-grid双选盒子
- PLC编程全科班+人机界面编程+电机+变频器通信培训
- Andrid studio 引用多个module时jar包重复的解决
- uva 725 Division
- String 之 搜索字符 indexOf 和 lastindexOf
- 从理论到实践,手把手教你如何用 TensorFlow 实现 CNN
- 【树形dp】poj 1947 Rebuilding Roads
- 安卓实训笔记第一天