本文主要是介绍【PAT】1112. Stucked Keyboard (20)【字符串处理】,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目描述
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.
翻译:在一个坏掉的键盘上,有些键总是被卡住。所以当你输入一些句子时,这些键对应的字符会在屏幕上重复出现k次。
现在,给定屏幕上的结果字符串,你需要列出所有可能的键和原始字符串。
注意,可能有一些字符是重复输入的。无论何时按下,被卡住的键总会重复输出固定的k次。例如,当k=3时,从字符串thiiis iiisss a teeeeeest中我们知道键i和e可能被卡住,但s不会,尽管它有时会重复出现。原始的字符串可以是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.
翻译:每个输入文件包含一组测试数据。对于每组输入数据,第一行给出一个正整数k(1<k≤100) ,代表卡住的键的重复次数。第二行包含屏幕上的结果字符串,它由不超过1000个来自{a-z}、{0-9}和_的字符组成。数据保证字符串是非空的。
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
解题思路
对出现的连续相同字母进行计数,如果有k次就标记,如果不到k次则记为-1,代表不是卡住的键。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<vector>
#include<algorithm>
#define INF 99999999
#define bug puts("Hello\n")
using namespace std;
int k;
char s[1010];
bool p[1010];
char ans[50],anscount=0;
int v[128];
int vc[128];
int main(){scanf("%d\n%s",&k,s);int length=strlen(s);int ccount=0;char c=' ';for(int i=0;i<length;i++){if(c!=s[i]){if(ccount>0)v[c]=-1;c=s[i];ccount=1;}else{ccount++;if(ccount==k){if(v[c]!=-1)v[c]=1;ccount=0;}}}ccount=0;c=' ';for(int i=0;i<length;i++){p[i]=true;if(c==s[i]){if(v[c]==1){ccount++;p[i]=false; if(!vc[c]){ans[anscount++]=c;vc[c]=1;} }if(ccount==k){ccount=0;c=' ';}}else{c=s[i];ccount=1;}}for(int i=0;i<anscount;i++){printf("%c",ans[i]);}printf("\n");for(int i=0;i<length;i++){if(p[i])printf("%c",s[i]);}printf("\n");return 0;
}
这篇关于【PAT】1112. Stucked Keyboard (20)【字符串处理】的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!