codeforces1351 C. Skier(结构体作键值 重载运算符)

C. Skier

Skier rides on a snowy field. Its movements can be described by a string of characters 'S', 'N', 'W', 'E' (which correspond to 11 meter movement in the south, north, west or east direction respectively).

It is known that if he moves along a previously unvisited segment of a path (i.e. this segment of the path is visited the first time), then the time of such movement is 55 seconds. If he rolls along previously visited segment of a path (i.e., this segment of the path has been covered by his path before), then it takes 11 second.

Find the skier's time to roll all the path.

Input

The first line contains an integer tt (1≤t≤1041≤t≤104) — the number of test cases in the input. Then tt test cases follow.

Each set is given by one nonempty string of the characters 'S', 'N', 'W', 'E'. The length of the string does not exceed 105105 characters.

The sum of the lengths of tt given lines over all test cases in the input does not exceed 105105.

Output

For each test case, print the desired path time in seconds.

Example

input

Copy

5
NNN
NS
WWEN
WWEE
NWNWS

output

Copy

15
6
16
12
25

结构体作为map、set等容器的键值时,需重载 < 号,重载时注意使用 if 、else if 、else 重载

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll inf = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-12;
const double PI = acos(-1.0);
const int N = 2e5 + 5;

struct node
{
    int x1, y1, x2, y2;
    node(){};
    node(int xx1, int yy1, int xx2, int yy2):x1(xx1), y1(yy1), x2(xx2), y2(yy2){}
    bool operator < (const node &a)const
    {
        if(x1 != a.x1)
            return x1 > a.x1;
        else if(y1 != a.y1)
            return y1 > a.y1;
        else if(x2 != a.x2)
            return x2 > a.x2;
        else
            return y2 > a.y2;
    }
};

int main()
{
    int t;
    string s;
    scanf("%d", &t);
    while(t--)
    {
        cin >> s;
        int len = s.size();
        map<node, int>mp;
        int prex = 0;
        int prey = 0;
        int x = 0;
        int y = 0;
        int cnt = 0;
        for(int i = 0; i < len; ++i)
        {
            switch(s[i])
            {
            case 'N':
                y++;
                break;
            case 'S':
                y--;
                break;
            case 'E':
                x++;
                break;
            case 'W':
                x--;
                break;
            }
            if(mp.count(node(prex, prey, x, y)))
            {
                cnt++;
            }
            else
            {
                mp[node(prex, prey, x, y)]++;
                mp[node(x, y, prex, prey)]++;
            }
            prex = x;
            prey = y;
        }
        cout<<cnt + (len - cnt) * 5<<'\n';
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值