Pat a problem-1112. stucked Keyboard-(map app)

Source: Internet
Author: User
Tags key string

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.