题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=171
算法思想: 贪心
Sample Input
1
5
UU D
UU
(1):先将UU放在一边,遇到D,将UU翻转,放到原来的位置上,成为DDDUU;
(2):将DDD放在一边,遇到U,将DDD翻转,放到原来的位置上,成为UUUUU。
代码实现:
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<iostream>
using namespace std;
int main()
{
int T;
scanf("%d", &T);
while (T--)
{
int n;
scanf("%d", &n);
int sum = 0;
char c;
char pre;
int i;
for (i = 0; i < n; )
{
scanf("%c", &c);
if (c == 'U' || c == 'D')
{
if (i == 0)
{
pre = c;
}
else
{
if (c != pre)
{
sum++;
pre = c;
}
}
i++;
//cout << i << endl;
}
}
//if (sum != 0)
printf("%d\n", sum);
if (T)
cout << endl;
}
return 0;
}