PAT A 1112. Stucked Keyboard (20)

来源:互联网 发布:淘宝哪些鞋店是正品 编辑:程序博客网 时间:2024/06/08 18:44

字符串的题,不难,但粗心就容易错。要注意题目的意思,其中有一段“The stucked key will always repeat output for a fixed k times whenever it is pressed”,意思就是说卡住的键是每被敲一次是100%会出现k个对应字符的,例如k=3时,对于”eeeeee"来讲,e就是被卡住了,原串为“ee",但对于”eeeee"来讲,e就是没被卡住。这个题代码我写得很麻烦,不喜欢做这种题。

#include <string>#include <iostream>#include <map>#include <vector>#include <set>using namespace std;map<char,bool> alp;set<char> res;int main(void){//freopen("in.log","r",stdin);int k;string st;cin>>k>>st;for(int i=0;i<st.size();++i){if((!alp.count(st[i]))||alp[st[i]]){//仅检查当前被标记为卡住的键,和未层出现的字符int j=i+1;bool flag=true;//字符初始为被卡住while(j-i<k&&j<st.size()){if(st[i]!=st[j]){flag=false;break;}++j;}if(j==st.size()&&j-i<k){//这是特殊情况,即当前字符后面剩下的字符个数已经不够k-1个了,那必然不是卡住的键flag=false;}alp[st[i]]=flag;if(flag)i=j-1;}}for(int i=0;i<st.size();++i){if(alp[st[i]]&&!res.count(st[i])){cout<<st[i];res.insert(st[i]); }}cout<<endl;for(int i=0;i<st.size();++i){if(alp[st[i]])i+=k-1;cout<<st[i];}cout<<endl; return 0;} 

原创粉丝点击