Remove Two Letters (思维)
思路: 看样例不难发现,对于aba来说,删掉ab和ba只会剩下a,对于aaa来说删掉aa也是一样的,剩下的那个字母都相同。所以其实判断条件就是遍历一遍,如果当前字母=往前两个的那个字母的话,贡献度不会增加,反之则增。
Code:
#include<iostream>
using namespace std;
int main()
{
int t;cin >> t;
while (t--)
{
int ans = 1;
int n;cin >> n;
string str;cin >> str;
for (int i = 2;i < n;i++)
if (str[i] != str[i - 2])ans++;
cout << ans << endl;
}
}