pat 1112. Stucked Keyboard
来源:互联网 发布:网络大电影票房 编辑:程序博客网 时间:2024/06/02 05:00
1112. Stucked Keyboard
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:3caseee1__thiiis_iiisss_a_teeeeeestSample Output:
eicase1__this_isss_a_teest
大致思路:以布尔数组isStuck表示某个字符是否是stuck 键。从第一个字母开始,以i表示当前访问的字母下标,取j=i+1;然后从j开始,找到第一个与第i个字符不相等的字符,且满足j-i小于n,n即字符重复出现的次数。
找到第一个这样的字符,其下标为j,若j-i等于n,则令布尔数组中stuck【i】=1;否则,置其为0。然后令i=j继续遍历后续元素。最终的时间复杂度为O(n)。
#include<cstdio>#include<cstring>const int maxn = 1001;bool isStuck[256] = { 0 };bool _print[256] = { 0 }; //某个字符是否被输出过char str[maxn];int main(){int n;scanf("%d", &n);scanf("%s", str);int len = strlen(str);for (int i = 0; i < len;){int j = i+1;while (str[j] == str[i]&&j-i<n)j++;if (j - i == n)isStuck[str[i]] = true;else if (isStuck[str[i]] == true)isStuck[str[i]] = false;i = j;}for (int i = 0; i < len;){//若第i个字符为stuck键值且未被输出过,则将其输出if (isStuck[str[i]] == true&&_print[str[i]]==false){printf("%c", str[i]);_print[str[i]] = true;i += n;}else i++;}printf("\n");for (int i = 0; i < len;){if (isStuck[str[i]] == false)printf("%c", str[i++]);else{printf("%c", str[i]);i += n;}}printf("\n");return 0;}
- PAT--1112. Stucked Keyboard
- 【PAT】1112. Stucked Keyboard
- pat 1112. Stucked Keyboard
- pat 1112. Stucked Keyboard
- pat 1112. Stucked Keyboard (20)
- solutino Of Pat 1112. Stucked Keyboard (20)
- PAT(A) - 1112. Stucked Keyboard (20)
- 1112. Stucked Keyboard (20) PAT甲级
- pat 甲 1112. Stucked Keyboard (字符串)
- PAT A 1112. Stucked Keyboard (20)
- PAT 1112. Stucked Keyboard (20) 随后再看
- 【PAT】【Advanced Level】1112. Stucked Keyboard (20)
- PAT 甲级 1112. Stucked Keyboard (20)
- PAT 1112 Stucked Keyboard
- 1112. Stucked Keyboard (20)-PAT甲级真题
- 1112. Stucked Keyboard (20)
- 1112. Stucked Keyboard (20)
- 1112. Stucked Keyboard (20)
- 最小费用最大流
- Linux 内存管理
- bzoj1040 [ZJOI2008]骑士
- 剑指offer:数组中出现次数超过一半的数字
- Freeline 的使用
- pat 1112. Stucked Keyboard
- 蓝桥杯 java 回形取数
- 用户余额充值、提现操作。
- Http协议概述
- 第一次的再坚持一下
- 浏览器中直接打开apk
- Conditional Random Fields as Recurrent Neural Networks
- JavaScript 精华一页纸
- Android手机UI设计---”知乎“界面外观模仿篇(六)---在Fragment里使用Gallery结合listview实现listview横滑