Test instructions: Given a k, keyboard some keyboard stuck, press once will play K times, ask to find the possible bad keys, output in the Order of discovery, and output the correct string order.
Map<char,int> is used to mark whether a key is a bad key, starting at 0, indicating that all keys are bad keys.
Then iterate through each character, counting the number of consecutive occurrences of the current character CNT, then as long as there is cnt%k!=0, then the good key, and the other map=1.
Finally, for the string, store the bad key string and the correct string, the last output can be.
#include <iostream>#include<cstdio>#include<algorithm>#include<string.h>#include<map>using namespacestd;Const intmaxn= ++5;intK;CharStr[maxn];map<Char,int> maps;//0 at the beginning, denoted as stucked keysintMain () {scanf ("%d",&k); scanf ("%s", str); intlen=strlen (str); Charch; intCNT; for(intI=0; i<len;i+=CNT) {CH=Str[i]; intp=i; for(; ch==str[p+1];p + +); CNT=p-i+1; if(cnt%k!=0) {Maps[ch]=1;//if the number of consecutive occurrences is not a multiple of k, it is clearly not stucked keys, labeled 1 } } CharANS[MAXN]; CharSTUCKED[MAXN]; intidx=0, idx2=0;; CNT=0; for(intI=0; i<len;i+=CNT) { if(maps[str[i]]==1) {Ans[idx]=Str[i]; IDX++; CNT=1; } Else{ intp=i; //if stucked keys appears for the first time, the corresponding map is 0 and then marked as 2 . if(maps[str[i]]!=2) {STUCKED[IDX2]=Str[i]; IDX2++; Maps[str[i]]=2; } for(; str[i]==str[p+1];p + +); CNT=p-i+1; for(intj=0; j<cnt/k;j++) {Ans[idx]=Str[i]; IDX++; }}} Ans[idx]=' /'; STUCKED[IDX2]=' /'; printf ("%s\n", stucked); printf ("%s", ans); return 0;}
View Code
Pat a problem-1112. stucked Keyboard-(map app)