A STring
东东有一个字符串X,该串包含偶数个字符,一半是 S 字符,一半是 T 字符
东东可以对该字符串执行 1010000 次操作:如果存在 ST 是该串的子串,则删除掉最左边的 ST。即 TSTTSS⇒TTSS、SSSTTT⇒SSTT⇒ST⇒空
Input
(2 ≦ |X| ≦ 200,000)
output
输出最终串的长度
Sample
TSTTSS
4
思路分析
- 利用find()和erase()会超时
- 模拟栈来实现。如果目前栈顶元素是S而且当前元素是T,那么出栈
代码
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<string>
#include<stack>
using namespace std;
stack<char> s;
string x;
int ans;
int main(){
cin>>x;
ans=x.length();
for(int i=0;i<x.length();i++){
if(s.size()&&s.top()=='S'&&x[i]=='T'){
ans-=2;
s.pop();
continue;
}
s.push(x[i]);
}
cout<<ans<<endl;
return 0;
}
B Pocket Cube
东东有一个二阶魔方,即2×2×2的一个立方体组。立方体由八个角组成。
魔方的每一块都用三维坐标(h, k, l)标记,其中h, k, l∈{0,1}。六个面的每一个都有四个小面,每个小面都有一个正整数。
对于每一步,东东可以选择一个特定的面,并把此面顺时针或逆时针转90度。
请你判断,是否东东可以在一个步骤还原这个魔方(每个面没有异色)。
Input
输入的第一行包含一个整数N(N≤30),这是测试用例的数量。
对于每个测试用例, 第 1~4 个数描述魔方的顶面,这是常见的2×2面,由(0,0,1),(0,1,1),(1,0,1),(1,1,1)标记。四个整数对应于上述部分。
第 5~8 个数描述前面,即(1,0,1),(1,1,1),(1,0,0),(1,1,0)的公共面。四个整数 与上述各部分相对应。
第 9~12 个数描述底面,即(1,0,0),(1,1,0),(0,0,0),(0,1,0)的公共面。四个整数与上述各部分相对应。
第 13~16 个数描述背面,即(0,0,0),(0,1,0),(0,0,1),(0,1),(0,1,1)的公共面。四个整数与上述各部分相对应。
第 17~20 个数描述左面,即(0,0,0),(0,0,1),(1,0,0),(1,0,1)的公共面。给出四个整数与上述各部分相对应。
第 21~24 个数描述了右面,即(0,1,1),(0,1,0),(1,1,1),(1,1,0)的公共面。给出四个整数与上述各部分相对应。
换句话说,每个测试用例包含24个整数a、b、c到x。你可以展开表面以获得平面图
如下所示。
+ - + - + - + - + - + - +
| q | r | a | b | u | v |
+ - + - + - + - + - + - +
| s | t | c | d | w | x |
+ - + - + - + - + - + - +
| e | f |
+ - + - +
| g | h |
+ - + - +
| i | j |
+ - + - +
| k | l |
+ - + - +
| m | n |
+ - + - +
| o | p |
+ - + - +
Output
对于每个测试用例,魔方如果可以至多 “只转一步” 恢复,输出YES,则输出NO。
Sample
4
1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4 5 5 5 5 6 6 6 6
6 6 6 6 1 1 1 1 2 2 2 2 3 3 3 3 5 5 5 5 4 4 4 4
1 4 1 4 2 1 2 1 3 2 3 2 4 3 4 3 5 5 5 5 6 6 6 6
1 3 1 3 2 4 2 4 3 1 3 1 4 2 4 2 5 5 5 5 6 6 6 6
YES
YES
YES
NO
Analyse
总共有12种转发,但是前面顺时针旋转和后面逆时针旋转等效,所以实际只有6种转发。
因题目要求至多转一次,所以A面顺时针旋转时A面的4个格的方格不用改变。
Code
#include<iostream>
#include<cstdio>
using namespace std;
int top[5],front[5],bottle[5],back[5],zuo[5],you[5];
int k;
void topshun(){
int temp1=top[4],temp2;
temp1=front[1],temp2=front[2];
front[1]=you[3];front[2]=you[1];
you[3]=back[4];you[1]=back[3];
back[4]=zuo[2];back[3]=zuo[4];
zuo[2]=temp1;zuo[4]=temp2;
}
void topni(){
int temp1=top[4],temp2;
temp1=front[1],temp2=front[2];
front[1]=zuo[2];front[2]=zuo[4];
zuo[2]=back[4];zuo[4]=back[3];
back[3]=you[1];back[4]=you[3];
you[1]=temp2;you[3]=temp1;
}
void frontshun(){
int temp1=front[4],temp2;
temp1=top[3],temp2=top[4];
top[3]=zuo[3];top[4]=zuo[4];
zuo[3]=bottle[2];zuo[4]=bottle[1];
bottle[2]=you[3];bottle[1]=you[4];
you[3]=temp1;you[4]=temp2;
}
void frontni(){
int temp1=front[4],temp2;
temp1=top[3],temp2=top[4];
top[3]=you[3];top[4]=you[4];
you[3]=bottle[2];you[4]=bottle[1];
bottle[1]=zuo[4];bottle[2]=zuo[3];
zuo[3]=temp1;zuo[4]=temp2;
}
void zuoshun(){
int temp1=zuo[4],temp2;
temp1=top[1],temp2=top[3];
top[3]=back[3];top[1]=back[1];
back[1]=bottle[1];back[3]=bottle[3];
bottle[1]=front[1];bottle[3]=front[3];
front[1]=temp1;front[3]=temp2;
}
void zuoni(){
int temp1=zuo[4],temp2;
temp1=top[1],temp2=top[3];
top[3]=front[3];top[1]=front[1];
front[1]=bottle[1];front[3]=bottle[3];
bottle[1]=back[1];bottle[3]=back[3];
back[1]=temp1;back[3]=temp2;
}
bool judge(){
bool ans=true;
for(int i=1;i<=3;i++){
if(top[i]!=top[i+1]){
ans=false;
break;
}
}
for(int i=1;i<=3;i++){
if(front[i]!=front[i+1]){
ans=false;
break;
}
}
for(int i=1;i<=3;i++){
if(bottle[i]!=bottle[i+1]){
ans=false;
break;
}
}
for(int i=1;i<=3;i++){
if(back[i]!=back[i+1]){
ans=false;
break;
}
}
for(int i=1;i<=3;i++){
if(zuo[i]!=zuo[i+1]){
ans=false;
break;
}
}
for(int i=1;i<=3;i++){
if(you[i]!=you[i+1]){
ans=false;
break;
}
}
return ans;
}
int main(){
cin>>k;
for(int i=0;i<k;i++){
for(int i=1;i<=4;i++){cin>>top[i];}
for(int i=1;i<=4;i++){cin>>front[i];}
for(int i=1;i<=4;i++){cin>>bottle[i];}
for(int i=1;i<=4;i++){cin>>back[i];}
for(int i=1;i<=4;i++){cin>>zuo[i];}
for(int i=1;i<=4;i++){cin>>you[i];}
bool can=false;
if(judge()){
can=true;
}
topshun();
if(judge()){
can=true;
}
topni();
topni();
if(judge()){
can=true;
}
topshun();
frontshun();
if(judge()){
can=true;
}
frontni();
frontni();
if(judge()){
can=true;
}
frontshun();
zuoshun();
if(judge()){
can=true;
}
zuoni();
zuoni();
if(judge()){
can=true;
}
if(can){
cout<<"YES"<<endl;
}
else cout<<"NO"<<endl;
}
return 0;
}