HDU 1020(连续同字符统计 **)

题意是要统计在一段字符串中连续相同的字符,不用再排序,相等但不连续的字符要分开输出,不用合在一起,之前用了桶排序的方法一直 wa,想复杂了。

代码如下:

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 int main()
 4 {
 5     std::ios::sync_with_stdio(false);
 6     int t,num,len;
 7     char c;
 8     bool f;
 9     string s;
10     cin >> t;
11     while(t--)
12     {
13         cin >> s;
14         len = s.length();
15         c = s[0];
16         num = 1;
17         f = true;
18         for(int i = 1; i < len; i++)
19         {
20             if(s[i] == c)
21             {
22                 f = false;
23                 num++;
24             }
25             else
26             {
27                 if(f) cout << c;
28                 else cout << num << c;
29                 num = 1;
30                 c = s[i];
31                 f = true;
32             }
33         }
34         if(!f) cout << num << c;
35         else cout << c;
36         cout << endl;
37     }
38     return 0;
39 }
View Code
日后若能有更好的想法,再来完善。 希望看到的大神不吝赐教 orz
原文地址:https://www.cnblogs.com/Taskr212/p/9529106.html