leetcode 判断字符串是否可以划分为字典序的单词(动态规划)

博客提供了牛客网的一个题目链接,链接为https://www.nowcoder.com/questionTerminal/5f3b7bf611764c8ba7868f3ed40d6b2c ,可通过该链接访问相关题目。

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

#include<unordered_set>
#include<string>
#include<iostream>
bool Judge(string s, unordered_set<string> &dict)
{
	int len = s.length();
	vector<bool>res(len + 1, false);//res[i]表示从0~i-1在字典中是否存在
	res[0] = true;
	for (int pos = 0; pos < len; pos++){//从pos位置开始进行字符串截取并判断
		for (int i = pos; res[pos] && i < len; i++){//res[pos]也要进行判断,i-pos+1表示后面截取多少个字符(由1开始递增)
			if (dict.find(s.substr(pos, i - pos + 1)) != dict.end())//
				res[i + 1] = true;
		}
	}
	return res[len];
}
int main()  
{
	string s = "leetcode";
	unordered_set<string> dict = { "leet", "cod" };
	bool f = Judge(s, dict);
	getchar();
	return 0;
}

https://www.nowcoder.com/questionTerminal/5f3b7bf611764c8ba7868f3ed40d6b2c

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值