UVA1401 Remember the Word 题解
考虑一个动态规划,
但直接转移是
#pragma GCC optimize("-Ofast,fast-math,-inline")
#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdio>
#include <string>
#include <algorithm>
#include <unordered_set>
using namespace std;
using ull = unsigned long long;
const int N = 3e5 + 5, M = 4005, K = 105, MOD = 20071027;
long long f[N];
string s;
int n;
string t[M];
ull hashing[N], hash2[M][K];
ull powe[N];
unordered_set<ull> st;
int main()
{
ios::sync_with_stdio(0), cin.tie(nullptr), cout.tie(nullptr);
int c = 0;
powe[0] = 1;
for (int i = 1; i < N; i++)
{
powe[i] = powe[i - 1] * 841;
}
while (cin >> s)
{
st.clear();
int p = s.size();
for (int i = 1; i <= p; i++)
{
hashing[i] = hashing[i - 1] * 841 + s[i - 1];
}
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> t[i];
int pp = t[i].size();
for (int j = 1; j <= pp; j++)
{
hash2[i][j] = hash2[i][j - 1] * 841 + t[i][j - 1];
}
st.insert(hash2[i][pp]);
}
f[0] = 1LL;
for (int i = 1; i <= p; i++)
{
f[i] = 0LL;
int maxn = min(i, 100);
for (int j = 1; j <= maxn; j++)
{
int l = i - j + 1, r = i;
ull subhash = hashing[r] - hashing[l - 1] * powe[r - l + 1];
if (st.count(subhash)) f[i] = (f[i] + f[l - 1]) % MOD;
}
}
cout << "Case " << ++c << ": " << f[p] << "\n";
}
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现