P3025 [USACO11OPEN] Forgotten Password S
比较显然的 DP 是很好思考的。
令
然而我的实现并不太优美,复杂度甚至已经达到了
这个复杂度其实并不很优,但是题目数据比较弱,所以就可以过了。不过优化也比较容易思考。发现单词长度不超过
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string>
#include <cassert>
using namespace std;
const int N = 1005;
string p[N], s;
string dp[N];
int n, m;
inline string check(string& g)
{
string res = "-1";
for (int i = 1; i <= m; i++)
{
if (p[i].size() != g.size()) continue;
bool f = 1;
for (int j = 0; j < g.size(); j++)
{
if (g[j] != '?' && p[i][j] != g[j])
{
f = 0;
break;
}
}
if (f)
{
if (res == "-1") res = p[i];
else res = min(res, p[i]);
}
}
return res;
}
auto main() -> int
{
ios::sync_with_stdio(0), cin.tie(nullptr), cout.tie(nullptr);
cin >> n >> m >> s;
for (int i = 1; i <= m; i++) cin >> p[i];
for (int i = 0; i < n; i++)
{
for (int j = i; j >= 0; j--)
{
if (j != 0 && dp[j - 1].empty()) continue;
string g = s.substr(j, i - j + 1);
string l = check(g);
if (l != "-1")
{
if (dp[i].empty()) dp[i] = (j == 0 ? "" : dp[j - 1]) + l;
else dp[i] = min(dp[i], (j == 0 ? "" : dp[j - 1]) + l);
}
}
}
cout << dp[n - 1] << "\n";
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现