liuchuo / PAT

🍭 浙江大学PAT题解(C/C++/Java/Python) - 努力成为萌萌的程序媛~

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

1112 更好的解法

Ruth-Seven opened this issue · comments

commented

我觉得还有更好的解法
额 不会插入代码。。。
#include<iostream> #include<string> #include<cstdio> using namespace std; // 1A int mapping[400], flag[400];//flag 0 意味着 这个键位有问题 mapping 是否已经显示 int main(){ int k; string s; cin >> k >> s; for(int i = 0; i < s.size() ;i++){ int j = i; while(j < i + k){ if(s[i] != s[j]) break; j++; } if(j != i + k) mapping[ s[i]] = 1; // 只要一个字母没问题 这个键就没问题 else i = i + k - 1; } string ans = ""; for(int i = 0; i < s.size(); i++){ ans.push_back(s[i]); if(mapping[ s[i] ] == 0){ if(!flag[s[i]]) printf("%c",s[i]); flag[s[i]] = 1; i += k - 1; } } cout << endl << ans; }