SaMer is building a simple robot that can move in the four directions: up (^), down (v), left (<), and right (>). The robot receives the commands to move as a string and executes them sequentially. The robot skips the commands that make it go outside the table.

SaMer has an R×C table and a string of instructions. He wants to place the robot on some cell of the table without rotating it, such that the robot will skip the minimum number of instructions. Can you help him find the number of instructions that will be skipped by the robot if it was placed optimally?

Input

The first line of input contains a single integer T, the number of test cases.

Each test case contains two space-separated integers R and C (1 ≤ R, C ≤ 105), the number of rows and the number of columns in the table, followed by a non-empty string of no more than 200000 characters representing the initial instructions. The instructions are executed in the given order from left to right.

Output

For each test case, print the minimum number of instructions which will be skipped, on a single line.

Example

Input
2
2 2 >^<v>^^<v
3 4 >^<^vv<>v>>v
Output
1
2 题意:
给一个n*m的格子,以及一串移动序列,如果移动超出边界,则会跳过这个操作,问最少会跳过多少个序列(起点未定)
思路:
没必要确定起点,如果在同一方向上走出的距离长度超出了棋盘长度的,那就是要跳过的操作。
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<ctime>
#define fuck(x) cout<<#x<<" = "<<x<<endl;
#define debug(a,i) cout<<#a<<"["<<i<<"] = "<<a[i]<<endl;
#define ls (t<<1)
#define rs ((t<<1)+1)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = ;
const int maxm = ;
const int inf = 2.1e9;
const ll Inf = ;
const int mod = ;
const double eps = 1e-;
const double pi = acos(-); char s[maxn]; int main()
{
// ios::sync_with_stdio(false);
// freopen("in.txt","r",stdin); int T;
scanf("%d",&T);
while(T--){
int n,m;
scanf("%d%d",&n,&m);
scanf("%s",s);
int len=strlen(s);
int x,y;
x=y=;
int x1,x2,y1,y2;
x1=x2=y1=y2=;
int ans=;
for(int i=;i<len;i++){
if(s[i]=='^'){y++;if(y-y1>=n){ans++;y--;continue;}}
else if(s[i]=='v'){y--;if(y2-y>=n){ans++;y++;continue;}}
else if(s[i]=='>'){x++;if(x-x1>=m){ans++;x--;continue;}}
else{
x--;if(x2-x>=m){ans++;x++;continue;}
}
// cout<<i<<" "<<x<<" "<<x1<<endl;
x1=min(x1,x);
x2=max(x2,x);
y1=min(y1,y);
y2=max(y2,y);
}
printf("%d\n",ans);
} return ;
}
/*
5
1 5
>>>>><<<<<
*/

最新文章

  1. from表单提交数据之后,后台对象接受不到值
  2. CSS之过渡简单应用—日落西山
  3. Yii;CodeIgniter;thinkphp学习
  4. AgularJS中Unknown provider: $routeProvider解决方案
  5. HD1394 Minimum Inversion Number
  6. SqlServer2008根据现有表,获取该表的分区创建脚本
  7. UIKit&#160;框架之UICollectionViewController
  8. HW4.42
  9. net-ldap for ruby openNebula ldap
  10. java.lang.system 类源码解读
  11. Spring 下 MyBatis 的基本使用
  12. Python学习 Part4:模块
  13. Python--day09(内存管理、垃圾回收机制)
  14. 第24月第30天 scrapy《TensorFlow机器学习项目实战》项目记录
  15. 函数和对象 及 prototype和__proto__
  16. NodeJs中类定义及类使用
  17. vue 中this指向遇到的坑
  18. Resource interpreted as Stylesheet but transferred with MIME type text/plain
  19. (贪心 or DP)Woodcutters -- Codefor 545C
  20. eclipse svn新增文件不显示在文件列表,只有修改文件可以提交!

热门文章

  1. qbao
  2. Path Sum 深度搜索
  3. hdu1848 sg打表
  4. KiCad 5.1.0 镜像圆弧后错位问题
  5. JQ取消hover事件
  6. Libevent:3创建event_base
  7. 【机器学习】Iris Data Set(鸢尾花数据集)
  8. 通过 PHP OPcache 提升 Laravel 应用运行速度
  9. Vue组件跨层级通信
  10. H5页面IOS中键盘弹出导致点击错位的问题