字符串压缩
字符串压缩
http://www.nowcoder.com/questionTerminal/44da6966beb449d383f62b12e8df6317
#include <iostream> #include <unordered_map> #include <map> using namespace std; int main() { string s; cin >> s; unordered_map<char, int> umap; //非空判断 if(!s.size()){ return 0; } //hash映射一下 for (int i = 0; i < s.size(); i++) { umap[s[i]]++; } bool flag = true; //已经输出标志位 char temp = s[0]; //首字母先输出一次,前提是字符串不为空 cout << umap[s[0]] << s[0]; for (int i = 1; i < s.size(); i++) { if (flag && temp != s[i]) { cout << umap[s[i]] << s[i]; temp = s[i]; flag = false; } if (temp != s[i]) { cout << umap[s[i]] << s[i]; temp = s[i]; flag = true; } } cout << endl; return 0; }可能相对大佬们的答案会显得有点傻,@豆豆瓣,来解答一下