time limit per test : 2 seconds
memory limit per test : 256 megabytes
分数:2100
You have a string s— a sequence of commands for your toy robot. The robot is placed in some cell of a rectangular grid. He can perform four commands:
'W' — move one cell up;
'S' — move one cell down;
'A' — move one cell left;
'D' — move one cell right.
Let Grid(s) be the grid of minimum possible area such that there is a position in the grid where you can place the robot in such a way that it will not fall from the grid while running the sequence of commands s. For example, if s=DSAWWAW then Grid(s) is the 4×3 grid:
you can place the robot in the cell (3,2);
the robot performs the command ′D′ and moves to (3,3);
the robot performs the command ′S′ and moves to (4,3);
the robot performs the command ′A′ and moves to (4,2);
the robot performs the command ′W′ and moves to (3,2);
the robot performs the command ′W′ and moves to (2,2);
the robot performs the command ′A′ and moves to (2,1);
the robot performs the command ′W′ and moves to (1,1);
.
You have 4 extra letters: one ′W′, one ′A′, one ′S′, one ′D′. You’d like to insert at most one of these letters in any position of sequence s to minimize the area of Grid(s).What is the minimum area of Grid(s) you can achieve?
Input
The first line contains one integer T(1≤T≤1000) — the number of queries.
Next T lines contain queries: one per line. This line contains single string s(1≤∣s∣≤2⋅105,si∈W,A,S,D) — the sequence of commands.
It’s guaranteed that the total length of s over all queries doesn’t exceed 2⋅105.
Output
Print T integers: one per query. For each query print the minimum area of Grid(s) you can achieve.
Example
Input
3
DSAWWAW
D
WA
Output
8
2
4
Note
In the first query you have to get string DSAWW{D}AW.
In second and third queries you can not decrease the area of Grid(s).
题意:
给定一个字符串,四种字符分别表示上下左右
你可以在任意位置,最多插入1个字符,使得所经过的点,用一个最小的矩形包住,使得这个矩形的面积最小。
求最小面积。
题解:
枚举插入位置,然后枚举插入的字符。
我们可以知道,影响答案的只跟maxx,maxy,minx,miny有关系
那么我们维护字符串前缀和后缀的这四个值,然后很显然插入字符之后等于后面所有坐标向这个位置平移。
然后直接计算即可。
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const ll INF=1e18;
int n;
int mx,my,gx,gy;
char s[200004];
int dx[4]={0,1,0,-1},
dy[4]={1,0,-1,0};
map<char,int>mp;
struct point{
int x,y;
point(int _x=0,int _y=0){x=_x;y=_y;}
point operator+(point B){
return point(x+B.x,y+B.y);
}
void print(){
printf("(%d,%d)",x,y);
}
}p[200004];
struct Ds{
int minx,miny,maxx,maxy;
Ds(int _a=0,int _b=0,int _c=0,int _d=0){
minx=_a;
miny=_b;
maxx=_c;
maxy=_d;
}
void Merge(Ds B){
minx=min(minx,B.minx);
miny=min(miny,B.miny);
maxx=max(maxx,B.maxx);
maxy=max(maxy,B.maxy);
}
void print(){
cout<<minx<<" "<<miny<<" "<<maxx<<" "<<maxy<<endl;
}
}tr[200004];
void update(int k){
tr[k].maxx=max(p[k].x,tr[k+1].maxx);
tr[k].minx=min(p[k].x,tr[k+1].minx);
tr[k].maxy=max(p[k].y,tr[k+1].maxy);
tr[k].miny=min(p[k].y,tr[k+1].miny);
}
void build(){
tr[n]=Ds(p[n].x,p[n].y,p[n].x,p[n].y);
for(int i=n-1;i>=0;i--){
update(i);
//tr[i].print();cout<<"--"<<endl;
}
}
Ds query(int k){
return tr[k];
}
ll calc(Ds v){
return 1LL*(v.maxx-v.minx+1)*(v.maxy-v.miny+1);
}
ll work(int st){
Ds Gc,left=Ds(gx,gy,mx,my);
//left.print();
Ds right=query(st);
ll res=-1;
for(int i=0;i<4;i++){
Gc=left;
Gc.Merge(Ds(right.minx+dx[i],right.miny+dy[i],right.maxx+dx[i],right.maxy+dy[i]));
Gc.Merge(Ds(p[st-1].x+dx[i],p[st-1].y+dy[i],p[st-1].x+dx[i],p[st-1].y+dy[i]));
//Gc.print();
ll now=calc(Gc);
if(res==-1||(res!=-1&&res>now)){
res=now;
}
}
//cout<<"qqqq"<<endl;
return res;
}
int w33ha(){
p[0]=point(0,0);
scanf("%s",s+1);
n=strlen(s+1);
for(int i=1;i<=n;i++){
p[i]=p[i-1]+point(dx[mp[s[i]]],dy[mp[s[i]]]);
//p[i].print();
}
//puts("");
ll ans=INF;
build();
mx=p[0].x;
my=p[0].y;
gx=p[0].x;
gy=p[0].y;
for(int i=1;i<=n;i++){
ans=min(ans,work(i));
mx=max(mx,p[i].x);
my=max(my,p[i].y);
gx=min(gx,p[i].x);
gy=min(gy,p[i].y);
}
printf("%lld\n",ans);
return 0;
}
int main(){
mp['W']=3;
mp['D']=0;
mp['S']=1;
mp['A']=2;
int T;scanf("%d",&T);
while(T--)w33ha();
return 0;
}