https://codeforces.com/contest/888/problem/B
题目大意
给出机器人 $n \ (n \le 100)$ 长的操作序列。操作序列包含 UDLR 四种字母,分别表示其向四个方向移动一个单位,机器人可能只执行了部分指令,已知它在初始及结束都在 $(0, 0)$ 位置,问其最多执行了多少指令。
简要题解
这是一道诈骗题,$n$ 很小可能直接就往 dp 想了,确实也能做,但是太麻烦了。上下的步数和左右的步数是各自独立的,因为回到了原点,所以上下步数应该一样,左右一样,所以统计一下,取尽可能多的就好了。
复杂度
$T$:$O(n)$
$S$:$O(n)$
代码实现
#include <bits/stdc++.h>
using namespace std;
int io_=[](){ ios::sync_with_stdio(false); cin.tie(nullptr); return 0; }();
using LL = long long;
using ULL = unsigned long long;
using LD = long double;
using PII = pair<int, int>;
using VI = vector<int>;
using MII = map<int, int>;
template<typename T> void cmin(T &x,const T &y) { if(y<x) x=y; }
template<typename T> void cmax(T &x,const T &y) { if(x<y) x=y; }
template<typename T> bool ckmin(T &x,const T &y) {
return y<x ? (x=y, true) : false; }
template<typename T> bool ckmax(T &x,const T &y) {
return x<y ? (x=y, true) : false; }
template<typename T> void cmin(T &x,T &y,const T &z) {// x<=y<=z
if(z<x) { y=x; x=z; } else if(z<y) y=z; }
template<typename T> void cmax(T &x,T &y,const T &z) {// x>=y>=z
if(x<z) { y=x; x=z; } else if(y<z) y=z; }
// mt19937 rnd(chrono::system_clock::now().time_since_epoch().count());
// mt19937_64 rnd_64(chrono::system_clock::now().time_since_epoch().count());
/*
---------1---------2---------3---------4---------5---------6---------7---------
1234567890123456789012345678901234567890123456789012345678901234567890123456789
*/
void solve() {
int n;
string s;
cin >> n >> s;
map<char, int> cnt;
for (char c : s) cnt[c]++;
int ans = 0;
ans += min(cnt['L'], cnt['R']);
ans += min(cnt['U'], cnt['D']);
cout << (ans * 2) << '\n';
}
int main() {
int t = 1;
// cin >> t;
while (t--) {
solve();
}
return 0;
}
Next: [CF] C. K-Dominant Character - Educational Codeforces Round 32