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;
}

 

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
正在热议
更多
# 春招至今,你的战绩如何? #
8028次浏览 74人参与
# 你的实习产出是真实的还是包装的? #
1491次浏览 37人参与
# 米连集团26产品管培生项目 #
5294次浏览 213人参与
# 军工所铁饭碗 vs 互联网高薪资,你会选谁 #
7248次浏览 39人参与
# 简历第一个项目做什么 #
31424次浏览 318人参与
# 当下环境,你会继续卷互联网,还是看其他行业机会 #
186682次浏览 1117人参与
# MiniMax求职进展汇总 #
23488次浏览 305人参与
# 研究所笔面经互助 #
118827次浏览 577人参与
# 重来一次,我还会选择这个专业吗 #
433189次浏览 3924人参与
# 简历中的项目经历要怎么写? #
309795次浏览 4174人参与
# 面试紧张时你会有什么表现? #
30448次浏览 188人参与
# AI时代,哪些岗位最容易被淘汰 #
63087次浏览 770人参与
# 正在春招的你,也参与了去年秋招吗? #
362963次浏览 2635人参与
# 你怎么看待AI面试 #
179634次浏览 1202人参与
# 职能管理面试记录 #
10765次浏览 59人参与
# 网易游戏笔试 #
6418次浏览 83人参与
# 腾讯音乐求职进展汇总 #
160504次浏览 1107人参与
# 校招笔试 #
468957次浏览 2960人参与
# 把自己当AI,现在最消耗你token的问题是什么? #
7113次浏览 156人参与
# 你觉得通信/硬件有必要实习吗? #
155421次浏览 1065人参与
# 小红书求职进展汇总 #
227003次浏览 1357人参与
# 从哪些方向判断这个offer值不值得去? #
56719次浏览 357人参与
牛客网
牛客网在线编程
牛客网题解
牛客企业服务