hdu 5311 Hidden String(水)

本文提供了一种解决HDU5311 HiddenString问题的方法,通过深度优先搜索来判断字符串是否可以匹配预设的anniversary字符串。代码中详细展示了如何递归地检查每个字符,确保匹配过程正确且高效。

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

题目链接:hdu 5311 Hidden String


#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;
const int maxn = 105;
//const int N = 11;
const char S[maxn] = "anniversary";

bool dfs (char* t, int l, int r, int d) {
	if (d >= 3)
		return false;

	for (; t[l]; l++) {
		int v = 0;
		while (t[l+v] && S[r+v] && t[l+v] == S[r+v])
			v++;
		if (S[r+v] == '\0')
			return true;
		if (dfs(t, l+v, r+v, d+1))
			return true;
	}
	return false;
}

int main () {
	int cas;
	char s[maxn];
	scanf("%d", &cas);
	while (cas--) {
		scanf("%s", s);
		printf("%s\n", dfs(s, 0, 0, 0) ? "YES" : "NO");
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值