题目链接 : C-被遗忘的书籍_牛客小白月赛82 (nowcoder.com)
题意:T组测试样例,每组给你一个n,问多少种字符串的方案包含”txt“;这里并没有说总的n的范围,考虑预处理,这样包含关系的方案数一般考虑dp
代码
#include<bits/stdc++.h> using namespace std; using ull = unsigned long long; using ll = long long; using PII = pair<int,int>; #define IOS ios::sync_with_stdio(false),cin.tie(0) #define endl "\n" #define pb push_back const int N=2e5+10; const int INF=0x3f3f3f3f; const int mod=998244353; ll dp[N][4]; /* dp[i][j] 代表前i个字符的情况 1. j [0~2]分别以"","t","tx"结尾的方案 2. j [3] 时代表包含了"txt"的方案 */ void init() { dp[0][0] = 1; for(int i = 1; i <= N - 1; i ++) { dp[i][3] = dp[i - 1][3] * 26 + dp[i - 1][2]; dp[i][2] = dp[i - 1][1]; dp[i][1] = dp[i - 1][1] + dp[i - 1][0]; dp[i][0] = dp[i - 1][2] * 25 + dp[i - 1][1] * 24 + dp[i - 1][0] * 25; dp[i][0] %= mod, dp[i][1] %= mod, dp[i][2] %= mod, dp[i][3] %= mod; } } ll ksm(ll a,ll b) { ll res = 1; while(b){ if(b & 1) res = (ll)res*a % mod; a = (ll)a * a % mod; b >>= 1; } return res; } void solve() { int n; cin >> n; cout << dp[n][3] << endl; } int main() { IOS; int T; cin>>T; init(); while(T--) { solve(); } return 0; }
标签:const,int,long,using,define,书籍,dp,遗忘 From: https://www.cnblogs.com/ZouYua/p/17880446.html