A. Box is Pull
题解:如果两个点不在一条直线上就加上老鼠多走的2步。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<time.h>
#include<vector>
#define SF(n) scanf("%d",&n)
#define ll long long
#define For(i,n) for(int i=0; i<n; ++i)
using namespace std;
int main()
{
//ios::sync_with_stdio(false);
//cin.tie(0);
//cout.tie(0);
int n;
cin>>n;
while(n--){
int x1,x2,y1,y2;
cin>>x1>>y1>>x2>>y2;
if(x1==x2 || y1==y2){
cout<<abs(x1-x2)+abs(y1-y2)<<endl;
}
else{
cout<<abs(x1-x2)+abs(y1-y2)+2<<endl;
}
}
return 0;
}
B - Belted Rooms
翻译:
在蛇展中,有n个房间(编号为0到n-1)排列成一个圆圈,每个房间里都有一条蛇。房间由n条传送带连接,第i条传送带连接房间i和(i+1)modn。换句话说,房间0和1,1和2,…,n−2和n−1,n−1和0与传送带相连。
第i条传送带处于以下三种状态之一:
如果是顺时针方向,蛇只能从房间i到(i+1)modn。
如果是逆时针方向,蛇只能从房间(i+1)移动到i。
如果它关闭,蛇可以向任何一个方向移动。
以上是一个4个房间的示例,其中皮带0和3关闭,1顺时针,2逆时针。
每一条蛇都想离开自己的房间,待会再来找它。如果蛇可以离开房间,然后使用传送带返回房间,则可以返回房间。有多少这样的可退换房?
输入
每个测试包含多个测试用例。第一行包含单个整数t(1≤t≤1000):测试用例的数量。测试用例的描述如下。
每个测试用例描述的第一行包含单个整数n(2≤n≤300000):房间数。
每个测试用例描述的下一行包含一个长度为n的字符串s,仅由“<”、“>”和“-”组成。
如果si=’>’,第i条传送带顺时针移动。
如果si=“<”,则第i条传送带将逆时针移动。
如果si=’-’,第i条传送带关闭。
保证所有测试用例中n的总和不超过300000。
解题思路:
一种情况是蛇离开房间后到临近的一个传送带关闭的双向通道,然后回来。
一种是蛇周围没有传送带关闭的通道,就只能绕环一圈,这个过程中,传送带移动只能是单向的,如果出现反向就不能回到原来的房间,即必须是回路。
综上,如果存在回路,即顺时针">"和逆时针“<”符号只能出现一种,则所有的蛇通过绕圈一周都可以回到原始房间,输出n
如果,不存在回路,则计算周围存在传送带关闭的通道的蛇的个数,然后输出答案。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<time.h>
#include<vector>
#define SF(n) scanf("%d",&n)
#define ll long long
#define For(i,n) for(int i=0; i<n; ++i)
using namespace std;
int main()
{
//ios::sync_with_stdio(false);
//cin.tie(0);
//cout.tie(0);
int t;
cin>>t;
while(t--){
int n;
cin>>n;
string s;
int ans = 0;
cin>>s;
int flag1 = 0,flag2 = 0;
for(int i=0; i<n; ++i){
if(s[i]=='>') flag1 = 1;
if(s[i]=='<') flag2 = 1;
}
if(flag1 && flag2){
for(int i=0; i<n; ++i){
if(s[i]=='-'||s[(i+1)%n]=='-'){
//如果这个点前后出现 “-” ,就计入答案
ans++;
}
}
cout<<ans<<endl;
}
else{
cout<<n<<endl;
}
}
return 0;
}
C. ABBB
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<time.h>
#include<vector>
#define SF(n) scanf("%d",&n)
#define ll long long
#define For(i,n) for(int i=0; i<n; ++i)
using namespace std;
stack<char> s;
int main()
{
//ios::sync_with_stdio(false);
//cin.tie(0);
//cout.tie(0);
int t;
cin>>t;
while(t--){
while(!s.empty())
s.pop();
string str;
cin>>str;
int n = str.size();
s.push(str[0]); //先push一个,保证不为空
for(int i=1; i<n; ++i){
if(!s.empty() && str[i]=='B')
//只要是AB或BB都可以,s出栈
s.pop();
else{
s.push(str[i]);
}
}
cout<<s.size()<<endl;
}
return 0;
}