P8185 [USACO22FEB] Blocks B

文章讲述了母牛Bessie通过四块木块上的字母学习拼写,需要确定她能否用这些字母成功拼写出给定的单词列表。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目描述

In an effort to improve her vocabulary, Bessie the cow has obtained a set of four wooden blocks, each one a cube with a letter of the alphabet written on each of its six sides. She is learning how to spell by arranging the blocks in a row so the letters on top of the blocks spell words.

Given the letters on each of Bessie's four blocks, and a list of words she would like to spell, please determine which of words on her list she will be able to spell successfully using the blocks.

输入格式

The first line of input contains N (1≤N≤10), the number of words that Bessie would like to spell. The next four lines each contain a string with six uppercase letters, representing the letters on the six sides of one of Bessie's blocks. The next N lines contain the N words Bessie would like to spell. Each of these is between 1 and 4 uppercase letters long.

输出格式

For each word on Bessie's list, output YES if she is able to spell it using the blocks and NO otherwise.

题意翻译

题目描述

为了提高词汇量,母牛贝西得到了一套四块木块,其中每块都是一个立方体,六面各写着一个字母。她正在通过将木块排成一排使得木块顶部的字母拼出单词来学习拼写。

给定 Bessie 的四个木块上的字母,以及她想拼写的单词列表,请确定列表中哪些单词可被她使用木块成功拼写。

输入格式

输入的第一行包含 N(1≤N≤10),为 Bessie 想要拼写的单词数。接下来的四行每行包含一个带有六个大写字母的字符串,表示 Bessie 的一个块的六个侧面上的字母。接下来的 N 行包含 Bessie 想要拼写的 N 个单词。其中每一个的长度在 1 到 4 个大写字母之间。

输出格式

对于 Bessie 列表中的每个单词,如果她能够使用木块拼写,则输出 YES,否则输出 NO。

样例解释

在本例中,Bessie 可以拼写 COW、ZOO 和 MOVE。 不幸的是,她不能拼写 MOO,因为唯一带有 M 的木块也不能用于 O。她不能拼写 FARM,因为没有字母 R 的木块。她不能拼写 CODE,因为 C、D 和 E 都属于同一个木块。

输入输出样例

输入 #1

6
MOOOOO
OOOOOO
ABCDEF
UVWXYZ
COW
MOO
ZOO
MOVE
CODE
FARM

输出 #1

YES
NO
YES
YES
NO
NO

说明/提示

【样例解释】

In this example, Bessie can spell COW, ZOO, and MOVE. Sadly, she cannot spell MOO, since the only block with an M cannot also be used for an O. She cannot spell FARM since there is no block with a letter R. She cannot spell CODE since the C, D, and E all belong to the same block.

在本例中,Bessie可以拼写COW、ZOO和MOVE。遗憾的是,她不能拼写MOO,因为唯一带有M的块也不能用于O。她不能拼写FARM,因为没有带有字母R的块。她不能拼CODE,因为C、D和E都属于同一个块。

题目难度

普及-

参考思路

对于每次询问,我们用一个桶 a 记录下 s 中 26 个出现的次数。然后 dfs 枚举从每个字符串中选出哪个字母,也用一个桶 b 记录下出现的次数。若1 ,则输出 YES;否则输出 NO

参考代码

 #include<bits/stdc++.h>
using namespace std;

string a[15],s;
bool f[15],ans;

void dfs(int dep,int x)
{
	if(dep==s.size())
	{
		
		ans|=(s.size()==x);//处理结果
		return;
	}
	for(int i=1;i<=4;i++)
	{
		if(f[i]) continue;
		if(a[i].find(s[x])!=string::npos)//能在 a[i] 里找到 s[x]
		{
			f[i]=1;
			dfs(dep+1,x+1);//往下搜
			f[i]=0;//回溯
		}
		dfs(dep+1,x);//不取
	}
}

int main()
{
	int n;
	cin>>n>>a[1]>>a[2]>>a[3]>>a[4];
	while(n--)
	{
		cin>>s;
		dfs(0,0);
		puts(ans?"YES":"NO");
		ans=0;
		memset(f,0,sizeof(f));//初始化
	}
 	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值