CF888B题解

发布时间 2023-10-26 10:20:37作者: Kazdale
  • 分析

    题意为选出最多的操作使机器人执行完仍停留在原地。
    分为左右和上下两类,则每一类的可执行操作数都是操作次数最少的一种操作的二倍(因为正反操作都要执行才能抵消)。
    直接统计每种操作的操作次数计算答案即可。

  • 代码

#include <iostream>
using namespace std;
constexpr int MAXN(1000007);
inline void read(int &temp) { cin >> temp; }
string s;
int n, lcnt, rcnt, ucnt, dcnt;
int main() {
	ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
	read(n);
	cin >> s;
	for (int i(0); i < (int)s.length(); ++i)  lcnt += (s[i] == 'L'), rcnt += (s[i] == 'R'), ucnt += (s[i] == 'U'), dcnt += (s[i] == 'D');
	cout << min(lcnt, rcnt) * 2 + min(ucnt, dcnt) * 2 << endl; 
	return 0;
}