1112 Stucked Keyboard (PAT甲级)

news/2024/10/21 3:38:48/

写完后看柳婼的解法,其实可以直接数重复了几次,我这个写法有点复杂化了。

原代码:

#include <iostream>
#include <string>
#include <set>int main(){int k, j;bool flag;std::set<char> st, printed;std::string str;std::cin >> k;std::cin >> str;for(int i = 1; i < str.size(); ++i){if(str[i] == str[i - 1]){flag = true;for(j = 1; j <= k - 2; ++j){if(str[i + j] != str[i]){flag = false;break;}}if(flag){st.insert(str[i]);}i += j;}}for(int i = 0; i < str.size(); ++i){if(st.find(str[i]) != st.end()){for(j = 1; j <= k - 1; ++j){if(str[i + j] != str[i]){st.erase(str[i]);break;}}i += j - 1;}}for(int i = 0; i < str.size(); ++i){if(st.find(str[i]) != st.end() && printed.find(str[i]) == printed.end()){std::cout << str[i];printed.insert(str[i]);}}std::cout << std::endl;for(int i = 0; i < str.size(); ++i){std::cout << str[i];if(st.find(str[i]) != st.end()){i += k - 1;}}return 0;
}

修改后的代码:

#include <iostream>
#include <string>
#include <set>int main(){int k, cnt;std::set<char> stucked, notStucked, printed;std::string str;std::cin >> k;std::cin >> str;cnt = 1;for(int i = 1; i < str.size(); ++i){if(str[i] == str[i - 1]){++cnt;} else{if(cnt % k){notStucked.insert(str[i - 1]);} else{stucked.insert(str[i - 1]);}cnt = 1;}}if(cnt % k){notStucked.insert(str.back());} else{stucked.insert(str.back());}for(int i = 0; i < str.size(); ++i){if(stucked.find(str[i]) != stucked.end()&& notStucked.find(str[i]) != notStucked.end()){stucked.erase(str[i]);}}for(int i = 0; i < str.size(); ++i){if(stucked.find(str[i]) != stucked.end()&& printed.find(str[i]) == printed.end()){std::cout << str[i];printed.insert(str[i]);}}if(!printed.empty()){std::cout << std::endl;}for(int i = 0; i < str.size(); ++i){std::cout << str[i];if(stucked.find(str[i]) != stucked.end()){i += k - 1;}}return 0;
}

题目如下:

On a broken keyboard, some of the keys are always stucked. So when you type some sentences, the characters corresponding to those keys will appear repeatedly on screen for k times.

Now given a resulting string on screen, you are supposed to list all the possible stucked keys, and the original string.

Notice that there might be some characters that are typed repeatedly. The stucked key will always repeat output for a fixed k times whenever it is pressed. For example, when k=3, from the string thiiis iiisss a teeeeeest we know that the keys i and e might be stucked, but s is not even though it appears repeatedly sometimes. The original string could be this isss a teest.

Input Specification:

Each input file contains one test case. For each case, the 1st line gives a positive integer k (1<k≤100) which is the output repeating times of a stucked key. The 2nd line contains the resulting string on screen, which consists of no more than 1000 characters from {a-z}, {0-9} and _. It is guaranteed that the string is non-empty.

Output Specification:

For each test case, print in one line the possible stucked keys, in the order of being detected. Make sure that each key is printed once only. Then in the next line print the original string. It is guaranteed that there is at least one stucked key.

Sample Input:

3
caseee1__thiiis_iiisss_a_teeeeeest

Sample Output:

ei
case1__this_isss_a_teest

http://www.ppmy.cn/news/90008.html

相关文章

codeforce第874轮(div3)

地址&#xff1a;codeforce第84轮&#xff08;div3&#xff09; A&#xff1a; 题目的意思是&#xff1a;给定我们一堆包含两个字符的字符串&#xff0c;字符串a和字符串b只要a的后一个字母和b的前一个字母相等即可链接&#xff0c;现在给出最后链接好的字符串&#xff0c;问…

〖Web全栈开发⑤〗— CSS基础

〖Web全栈开发⑤〗— CSS基础 (一)CSS基础1.1CSS介绍1.2CSS样式1.3CSS 格式 &#xff08;二&#xff09;CSS 选择器2.1标签选择器2.2类选择器2.3层级选择器2.4id选择器2.5组选择器2.6伪类选择器2.7通配符选择器 &#xff08;三&#xff09;样式表引入3.1外部样式表3.2内部样式表…

二总线-MBus讲解

二总线的叫法演变是从多线到总线再到二总线这么一个过程&#xff0c;尤其在楼宇的消防领域&#xff0c;报警的设备总线基本已经是二总线了&#xff0c;其特点就是电源与通信一起传输&#xff0c;本质上是一个电力载波的思路。那么现在的powerbus二总线又是一个极端&#xff0c;…

知识变现海哥:如何把自己的想法变现?

高手都懂得&#xff0c;简单三招&#xff0c;卖掉自己的想法。 把自己的思维装入别人的大脑&#xff0c;把别人的钱装进自己的口袋。 招一&#xff1a;把已知的事情和知识&#xff0c;变成未知的。 网络自媒体大行其道&#xff0c;你经常会看到听到一些新名词&#xff0c;仔细…

学习go的操作(本人已有c的基础,请思考后再看)

建立一个文件&#xff08;我的第一个文件是hellow.go&#xff09;&#xff0c;后在终端执行一下几步&#xff1a;我用的是go build先编译成了可执行文件&#xff08;.exe&#xff09;【1.go build hellow.go 2.hellow.exe】。当然&#xff0c;你也可以用go run直接运行【…

00后学什么技术有前途?2023年Java和前端发展前景分析!

00后的你还在想着进厂吗&#xff1f;每天在流水线上打螺丝&#xff0c;过着一成不变的日子&#xff0c;而且每个月就休息那么几天。如果你不想进厂&#xff0c;特别是对那些20岁刚出头或者学历不是那么有优势的年轻人&#xff0c;好程序员建议还是应该去学习一门技术&#xff0…

论文解读 | IROS 2022:MV6D:在RGB-D图像上使用深度逐点投票网络进行多视角6D姿态估计

原创 | 文 BFT机器人 01 研究背景 在计算机视觉领域&#xff0c;6D姿态估计是一种重要的任务&#xff0c;用于确定物体在3D空间中的位置和方向。它在许多应用领域具有广泛的应用&#xff0c;如机器人操作、虚拟现实、增强现实、物体跟踪等。 然而&#xff0c;传统的6D姿态估计方…

VMWare ESXI6.7创建虚拟机

VMware ESXi&#xff1a;专门构建的裸机 管理程序 首先开启ESXI主机 登录ESXI 打开浏览器输入物理机ip&#xff0c;输入账号密码进行登录 创建虚拟机 选择创建类型 创建RedHat7.6 选择存储类型和数据存储 仅一个存储&#xff0c;直接点下一页即可 配置虚拟机硬件和虚拟机附…