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

时间:2023-03-09 01:52:00
HDU 1020(连续同字符统计 **)

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

代码如下:

 #include <bits/stdc++.h>
using namespace std;
int main()
{
std::ios::sync_with_stdio(false);
int t,num,len;
char c;
bool f;
string s;
cin >> t;
while(t--)
{
cin >> s;
len = s.length();
c = s[];
num = ;
f = true;
for(int i = ; i < len; i++)
{
if(s[i] == c)
{
f = false;
num++;
}
else
{
if(f) cout << c;
else cout << num << c;
num = ;
c = s[i];
f = true;
}
}
if(!f) cout << num << c;
else cout << c;
cout << endl;
}
return ;
}