描述

One day, there is a kind of new delicious food from one of the windows in the canteen. All students want to have a taste. Therefore, they all go to this window to line up and form a sequence. We need to simplify this sequence. For example, if a sequence is 12221133345678899, we can simplify it as 1213456789.

输入

Each test case contains a sequence. There are only numbers and letters in the sequence. The max length of the sequence is 100. The input is terminated by the end of file (EOF).

输出

For each test case, you must print the simplyfied sequence in one line.

输入样例 1

12221133345678899
aabbccddeeffgg

输出样例 1

1213456789
abcdefg

思路:
字符串去重问题,这个题有两种解法,一种是占用一个额外存储空间,另一种是就在原字符串上进行修改,先说第一种。

第一种就是建立一个新的空字符串ans,先把原字符串s的首位加进去,然后从s的第一位开始遍历s。如果s[i]表示的字符与ans的最后一位字符(表示方法:ans[ans.length()-1] )不相等,可以认为s中一段相同字符的序列已经结束了,那么把这个字符加进ans里,最后输出ans,注意格式!!

代码如下:

#include<iostream>
#include<bits/stdc++.h>
using namespace std;int main()
{string s;while (cin >> s){string ans = "";ans += s[0];for (int i = 1; i < s.length(); i++){if (s[i] != ans[ans.length() - 1])ans += s[i];}cout << ans <<endl;}return 0;
}

第二种不占用额外存储空间,就在原字符串上进行修改,那么就要用两个指针遍历该字符串,一个是用来塑造新的字符串,一个是不停往后遍历来寻找与现在不一样的字符。

图解:

代码如下:

#include<iostream>
#include<bits/stdc++.h>
using namespace std;int main()
{string s;while (cin >> s){int fast = 1, slow = 0; //fast是跑的快的那个指针,slow是慢的while(fast < s.length()){if (s[slow] != s[fast]) //指的字符不一样{slow++; //slow向后挪一位s[slow] = s[fast]; //让fast指向的值覆盖slow指向的值}fast++;}cout << s.substr(0, slow + 1) << endl;}return 0;
}

北林oj-算法设计与分析-Line up in the canteen(两种解法,附思路)相关推荐

  1. 北林oj-算法设计与分析-Removing the Wall(C++,思路+代码)

    描述 There is a wall in Tom's garden, which is builded by N different bricks. Bricks may have differen ...

  2. 北林oj-算法设计与分析-Tom palindrome number

    描述 Tom is studing math these days. If there is a number X, whose binary form and decimal form are al ...

  3. 北林oj-算法设计与分析-Don‘t touch my cake(题意+代码)

    描述 A boy bought N different cakes someday. They were arranged randomly in a line given by permutatio ...

  4. 北林oj-算法设计与分析-Simple Count(C++,思路+代码)

    描述 Count how many numbers do not contain 4 or 7 in the N numbers from 1 to N. 输入 Each test case star ...

  5. 北林oj-算法设计与分析-A dice game(思路+代码)

    描述 One day, boy A and girl B are playing a dice game. First, they write a number between 1~6 in a pi ...

  6. 【算法设计与分析】经典常考三十三道例题AC代码

    ❥小虾目前大三,我校在大一下开设<数据结构>这门课,大二上开了<算法设计与分析>这门课,很庆幸这两门课的上机考试总成绩一门100,一门99,最后总分也都90+.下文会给出机试的 ...

  7. 算法设计与分析考前复习

    算法设计与分析考前复习 qiwang的NOJ系统在考前一天崩了,强烈建议开发新OJ 另外,由于以下这些题是我上完数电实验用了下午和晚上时间写的,可能会出一些错. 分治法复习 二分查找 描述 给定一个单 ...

  8. C++ 算法设计与分析 地图着色问题(中国+美国)

    文章目录 中国+美国可选择地图着色问题 地图着色问题 解决方法 回溯法 队列 代码展示 美国地图着色 代码(c++) 结果 中国+美国地图着色 代码(c++) 结果 可视化 中国+美国可选择地图着色问 ...

  9. 【算法设计与分析】屈婉玲教授—课程笔记

    网课地址与课件 B站:[北大公开课] 算法设计与分析 屈婉玲教授 (76p) 课件:来源于评论区小伙伴分享(百度云)     提取码:1111 笔记 课程知识框架 算法设计思想 设计思想:尽量选复杂度 ...

最新文章

  1. Windows10下Python3做OpenGL的编程
  2. linux的网络命令整理 更新中
  3. ES什么时候会平衡分片
  4. delphi 打印指定地点文件_2020年度电脑、打印机耗材及相关配件采购招标公告
  5. RabbitMQ 延迟队列,消息延迟推送
  6. xpath之string(.)方法
  7. SDOI 2009 BIll的挑战
  8. 招聘ASP.net高级Web开发工程师
  9. python实训day5
  10. 为什么我感觉生活不易挣钱太难
  11. 计算机大学等级评估,“计算机专业”学科评估,四所高校获A+评级
  12. 【Hadoop】安装Apache Hadoop
  13. Python入门--函数传递过程中实参的变化
  14. Mac 如何保护您的数据安全?
  15. seata xid是什么_Spring Cloud Alibaba分布式事务解决框架Seata概念入门篇
  16. 股票历史数据下载工具_免费在线批量下载股票历史数据工具
  17. Mybatis 任务二:配置文件深入
  18. golang对接支付宝支付
  19. Christmas Gift圣诞创意衬线字体 for mac
  20. Share 很喜欢的派大星图片

热门文章

  1. BZOJ.2000.[HNOI2010]stone取石头游戏(博弈)
  2. PyCharm Python迁移项目
  3. 2018-12-08 acm日常 HDU - 6292(模拟找最小)
  4. 在Oracle中添加用户登录名称
  5. java 关于日期的操作
  6. Google Go:初级读本
  7. 其实“自己就是一座宝藏”:如何改变一生的命运
  8. Mr.J--简易的判断输入两次密码是否一致(静态)
  9. 7.6 T1 深度优先搜索(dfs)
  10. 单点登录的原理与简单实现