HDU 7108 Command Sequence

题目链接:HDU 7108 Command Sequence

题目大意:

题解:
记录所有经过的点,对重复的点计算组合数。

#include <cstdio>
#include <iostream>
#include <map>
using namespace std;
#define LL long long

map<pair<LL, LL>, LL> mat;
LL t, n, x, y, ans;
char ch[100010];

int main() {
    scanf("%lld", &t);
    while (t--) {
        scanf("%lld", &n);
        scanf("%s", ch);
        x = y = ans = 0;
        mat.clear();
        mat[make_pair(0ll, 0ll)]++;
        for (int i = 0; i < n; ++i) {
            if (ch[i] == 'U') y++;
            else if (ch[i] == 'D') y--;
            else if (ch[i] == 'L') x--;
            else x++;
            if (mat[make_pair(x, y)]) ans += mat[make_pair(x, y)];
            mat[make_pair(x, y)]++;
        }
        printf("%lld
", ans);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/IzumiSagiri/p/15203259.html