A - 签到题
东东有一个字符串X,该串包含偶数个字符,一半是 S 字符,一半是 T 字符
东东可以对该字符串执行 1010000 次操作:如果存在 ST 是该串的子串,则删除掉最左边的 ST。
即 TSTTSS⇒TTSS、SSSTTT⇒SSTT⇒ST⇒空
Input
(2 ≦ |X| ≦ 200,000)
Output
输出最终串的长度
Sample Input 1
TSTTSS
Sample Output 1
4
做法
首先考虑借助string类型的各种函数,比如find(),erase(),size()等等。考虑到时间复杂度,这种做法并不行。
于是考虑char型数组逐个读入并同时判断,当前读入为T并且前一个字符为S时,就删除这一对儿,在这个过程中用i跟随数组的下标并同时起到记录数组大小的作用。
代码
#include<iostream>
#include<string.h>
using namespace std;
char c[200099];
int main()
{
int i=0;
while(scanf("%c",&c[i])!=EOF)
{
if(i>0)
{
if(c[i-1]=='S'&&c[i]=='T')
i=i-2;
}
i++;
}
cout<<i-1<<endl;
return 0;
}
B - 东东转魔方
东东有一个二阶魔方,即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 Input
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
Sample Output
YES
YES
YES
NO
做法
首先用一个int数组a按序保存魔方6个面24个数,如果每个面数字相同就输出Yes,如果仅有两个面相同就可能通过一次旋转恢复,其他情况都不能恢复,这时可以记录一下是哪两个面相同,然后决定旋转哪个面。
六个面每个面两个方向也就是十二种旋转方法,每个面对应一个函数,经旋转可以恢复就返回true,否则返回false。旋转过程中每个面数字的改变用类似穷举的方式实现。
代码
#include<iostream>
using namespace std;
bool jdge(int *aa)
{
bool bb=true;
for(int i=0;i<6;i++)
{
if(aa[i*4+1]==aa[i*4+2]&&aa[i*4+2]==aa[i*4+3]&&aa[i*4+3]==aa[i*4+4])
continue;
else
{ bb=false;break;}
}
return bb;
}
void restoree(int *b,int *a)//还原
{
for(int i=1;i<=24;i++)
b[i]=a[i];
}
bool topp(int *b,int *a)//旋转顶部
{
int t[10];
t[1]=b[5];t[2]=b[6];t[3]=b[23];t[4]=b[21];
t[5]=b[16];t[6]=b[15];t[7]=b[18];t[8]=b[20];
//顺时针
b[18]=t[1];b[20]=t[2];b[5]=t[3];b[6]=t[4];
b[23]=t[5];b[21]=t[6];b[16]=t[7];b[15]=t[8];
if(jdge(b)) return true;
else
{//逆时针
b[23]=t[1];b[21]=t[2];b[16]=t[3];b[15]=t[4];
b[18]=t[5];b[20]=t[6];b[5]=t[7];b[6]=t[8];
if(jdge(b)) return true;
else
{
//还原
restoree(b,a);
return false;
}
}
}
bool bottomm(int *b,int *a)
{
int t[10];
t[1]=b[7];t[2]=b[8];t[3]=b[24];t[4]=b[22];
t[5]=b[14];t[6]=b[13];t[7]=b[17];t[8]=b[19];
//顺时针
b[17]=t[1];b[19]=t[2];b[7]=t[3];b[8]=t[4];
b[24]=t[5];b[22]=t[6];b[14]=t[7];b[13]=t[8];
if(jdge(b)) return true;
else
{
//逆时针
b[24]=t[1];b[22]=t[2];b[14]=t[3];b[13]=t[4];
b[17]=t[5];b[19]=t[6];b[7]=t[7];b[8]=t[8];
if(jdge(b)) return true;
else
{
restoree(b,a);
return false;
}
}
}
bool frontt(int *b,int *a)
{
int t[10];
t[1]=b[9];t[2]=b[10];t[3]=b[24];t[4]=b[23];
t[5]=b[4];t[6]=b[3];t[7]=b[20];t[8]=b[19];
//顺时针
b[20]=t[1];b[19]=t[2];b[9]=t[3];b[10]=t[4];
b[24]=t[5];b[23]=t[6];b[4]=t[7];b[3]=t[8];
if(jdge(b)) return true;
else
{
//逆时针
b[24]=t[1];b[23]=t[2];b[4]=t[3];b[3]=t[4];
b[20]=t[5];b[19]=t[6];b[9]=t[7];b[10]=t[8];
if(jdge(b)) return true;
else
{
restoree(b,a);
return false;
}
}
}
bool backk(int *b,int *a)
{
int t[10];
t[1]=b[1];t[2]=b[2];t[3]=b[21];t[4]=b[22];
t[5]=b[12];t[6]=b[11];t[7]=b[17];t[8]=b[18];
//顺时针
b[17]=t[1];b[18]=t[2];b[1]=t[3];b[2]=t[4];
b[21]=t[5];b[22]=t[6];b[12]=t[7];b[11]=t[8];
if(jdge(b)) return true;
else
{
//逆时针
b[21]=t[1];b[22]=t[2];b[12]=t[3];b[11]=t[4];
b[17]=t[5];b[18]=t[6];b[1]=t[7];b[2]=t[8];
if(jdge(b)) return true;
else
{
restoree(b,a);
return false;
}
}
}
bool leftt(int *b,int *a)
{
int t[10];
t[1]=b[3];t[2]=b[1];t[3]=b[15];t[4]=b[13];
t[5]=b[11];t[6]=b[9];t[7]=b[7];t[8]=b[5];
//顺时针
b[7]=t[1];b[5]=t[2];b[3]=t[3];b[1]=t[4];
b[15]=t[5];b[13]=t[6];b[11]=t[7];b[9]=t[8];
if(jdge(b)) return true;
else
{
//逆时针
b[15]=t[1];b[13]=t[2];b[11]=t[3];b[9]=t[4];
b[7]=t[5];b[5]=t[6];b[3]=t[7];b[1]=t[8];
if(jdge(b)) return true;
else
{
restoree(b,a);
return false;
}
}
}
bool rightt(int *b,int *a)
{
int t[10];
t[1]=b[2];t[2]=b[4];t[3]=b[6];t[4]=b[8];
t[5]=b[10];t[6]=b[12];t[7]=b[14];t[8]=b[16];
//顺时针
b[14]=t[1];b[16]=t[2];b[2]=t[3];b[4]=t[4];
b[6]=t[5];b[8]=t[6];b[10]=t[7];b[12]=t[8];
if(jdge(b)) return true;
else
{
//逆时针
b[6]=t[1];b[8]=t[2];b[10]=t[3];b[12]=t[4];
b[14]=t[5];b[16]=t[6];b[2]=t[7];b[4]=t[8];
if(jdge(b)) return true;
else
{
restoree(b,a);
return false;
}
}
}
int main()
{
int n;cin>>n;
while(n--)
{
int a[30],b[30];
for(int i=1;i<=24;i++)
{
cin>>a[i];
b[i]=a[i];
}
int x=0;//记录相同面编号
int k=0;//记录相同面的个数
for(int i=0;i<6;i++)
{
if(a[i*4+1]==a[i*4+2]&&a[i*4+2]==a[i*4+3]&&a[i*4+3]==a[i*4+4])
{
k++;
x=i;
}
}
if(k==6)
cout<<"YES"<<endl;
else if(k==2)
{
if(x==0||x==2)//转顶面或底面
{
if(topp(b,a))
cout<<"YES"<<endl;
else if(bottomm(b,a))
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
else if(x==1||x==3)//前面或后面
{
if(frontt(b,a))
cout<<"YES"<<endl;
else if(backk(b,a))
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
else if(x==4||x==5)
{
if(leftt(b,a))
cout<<"YES"<<endl;
else if(rightt(b,a))
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
}
else
cout<<"NO"<<endl;
}
return 0;
}