week10模拟

A

题意

某个包含偶数个字符的字符串X,一半是 S 字符,一半是 T 字符。如果存在 ST 是该串的子串,则删除掉最左边的 ST。即 TSTTSS⇒TTSS、SSSTTT⇒SSTT⇒ST⇒空。

  • 输入
TSTTSS
  • 输出
4

思路

利用<string>类型操作erase

  1. 同时对当前位置的字母和紧挨着的下一个字母判断,若为ST,则删除。
  2. 删除后,若当前位置下标为0,则不变;否则,当前位置下标-1作为下一次的下标。
  3. 若没有删除,位置向前移动1位。
  4. 最后返回string的长度。

总结

最开始用C风格的字符串char*,惨遭滑铁卢…
后果断更换string类型,使用erase方法简化,或许处理字符串类型的题目,string类型更方便一些。

代码

#include<iostream>
#include<string>
using namespace std;


int main(void){
	string str;
	cin>>str;
	int len=str.size();
	for(int i=0;i<len-1;){
		if(str[i]=='S'&&str[i+1]=='T'){
			int temp=i;
			str.erase(i,2);
			if(temp>0)
				i=temp-1;
			else
				i=temp;
			len=str.size();
		}
		else i++;
	}
	cout<<str.size()<<endl;

	return 0;
}

B

题意

二阶魔方,判断是否能在最多转动一步的情况下将魔方复原。

  • 输入对应的面序号:
+ - + - + - + - + - + - +
| q | r | a | b | u | v |
+ - + - + - + - + - + - +
| s | t | c | d | w | x |
+ - + - + - + - + - + - +
        | e | f |
        + - + - +
        | g | h |
        + - + - +
        | i | j |
        + - + - +
        | k | l |
        + - + - +
        | m | n |
        + - + - +
        | o | p |
        + - + - +
  • 输入
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

思路

二阶魔方最多只转一次,显然只有3种转法,影响不同的4个面。对应题目输入,为魔方各面、各色块编号如图。

  1. 统计不同面的数量,若不为4则一定不行,若为0直接输出YES
  2. 判断转动的类别(即对应不同的几个面)
  3. 在转动类别下,分别使用顺时针、逆时针判断转动后是否相同,若能相同则返回true
    在这里插入图片描述

总结

check2()函数中某个面的字母写错了,检查了很久很久很久,刚刚好错过提交时间AC…

  • 此类题目最好还是用纸叠一个,标上字母,不容易犯错

代码

#include<cstdio>
#include<map>
#include<set>
using namespace std;

/*
	a b 
	c d
*/

struct Faces{
	int a,b,c,d;

	Faces() {}
	Faces(int w,int x,int y,int z){
		a=w,b=x,c=y,d=z;
	}
};
// 1 top,2 front,3 bottom,4 behind,5 left,6 right;
map<int,Faces> Cube;
set<int> diff;

//相等返回true
bool checkFace(int a,int b,int c,int d){
	if(a!=b ||a!=c || a!=d
				 || b!=c || b!=d || c!=d)
		return false;
	return true;
}


//front,behind,left,right
//2,4,5,6
set<int> c1={2,4,5,6};
bool check1(){
	//
	if(checkFace(Cube[2].c,Cube[2].d,Cube[6].a,Cube[6].c)
		&& checkFace(Cube[5].a,Cube[5].c,Cube[2].a,Cube[2].b)
		&& checkFace(Cube[4].a,Cube[4].b,Cube[5].b,Cube[5].d)
		&& checkFace(Cube[6].b,Cube[6].d,Cube[4].c,Cube[4].d))
		return true;
	
	//
	if(checkFace(Cube[2].c,Cube[2].d,Cube[5].b,Cube[5].d)
		&& checkFace(Cube[5].a,Cube[5].c,Cube[4].c,Cube[4].d)
		&& checkFace(Cube[4].a,Cube[4].b,Cube[6].a,Cube[6].c)
		&& checkFace(Cube[6].b,Cube[6].d,Cube[2].a,Cube[2].b))
		return true;
	return false;
}

//top,bottom,left,right
//1,3,5,6
set<int> c2={1,3,5,6};
bool check2(){
	//
	if(checkFace(Cube[1].c,Cube[1].d,Cube[6].a,Cube[6].b)
		&& checkFace(Cube[5].c,Cube[5].d,Cube[1].a,Cube[1].b)
		&& checkFace(Cube[3].a,Cube[3].b,Cube[5].a,Cube[5].b)
		&& checkFace(Cube[6].c,Cube[6].d,Cube[3].c,Cube[3].d))
		return true;
	
	//
	if(checkFace(Cube[1].c,Cube[1].d,Cube[5].a,Cube[5].b)
		&& checkFace(Cube[5].c,Cube[5].d,Cube[3].c,Cube[3].d)
		&& checkFace(Cube[3].a,Cube[3].b,Cube[6].a,Cube[6].b)
		&& checkFace(Cube[6].c,Cube[6].d,Cube[1].a,Cube[1].b))
		return true;
	return false;
}

//top,front,bottom,behind
//1,2,3,4
set<int> c3={1,2,3,4};
bool check3(){
	//
	if(checkFace(Cube[1].a,Cube[1].c,Cube[4].b,Cube[4].d)
		&& checkFace(Cube[2].a,Cube[2].c,Cube[1].b,Cube[1].d)
		&& checkFace(Cube[3].a,Cube[3].c,Cube[2].b,Cube[2].d)
		&& checkFace(Cube[4].a,Cube[4].c,Cube[3].b,Cube[3].d))
		return true;
	
	//
	if(checkFace(Cube[1].a,Cube[1].c,Cube[2].b,Cube[2].d)
		&& checkFace(Cube[2].a,Cube[2].c,Cube[3].b,Cube[3].d)
		&& checkFace(Cube[3].a,Cube[3].c,Cube[4].b,Cube[4].d)
		&& checkFace(Cube[4].a,Cube[4].c,Cube[1].b,Cube[1].d))
		return true;
	return false;
}

void initall(){
	diff.clear();
	Cube.clear();
}



int main(void){
	int group;
	scanf("%d",&group);
	while(group--){
		initall();

		for(int i=1;i<=6;i++){
			int tempa,tempb,tempc,tempd;
			scanf("%d%d%d%d",&tempa,&tempb,&tempc,&tempd);
			Cube[i]=Faces(tempa,tempb,tempc,tempd);

			if(!checkFace(tempa,tempb,tempc,tempd))
				diff.insert(i);
		}
		if(diff.size()==0){
			printf("YES\n");
			continue;
		}
		if(diff.size()!=4){
			printf("NO\n");
			continue;
		}

		bool bc1=true,bc2=true,bc3=true;
		for(auto iter=diff.begin(), iter1=c1.begin(), iter2=c2.begin(), iter3=c3.begin();
			iter!=diff.end() && iter1!=c1.end() && iter2!=c2.end() && iter3!=c3.end();
			++iter, ++iter1, ++iter2, ++iter3){
			if(*iter!=*iter1)
				bc1=false;
			if(*iter!=*iter2)
				bc2=false;
			if(*iter!=*iter3)
				bc3=false;
		}

		bool res;
		if(bc1) res=check1();
		else if(bc2) res=check2();
		else if(bc3) res=check3();
		else res=false;

		if(res)
			printf("YES\n");
		else
			printf("NO\n");
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值