题解:P8194 [USACO22FEB] Phone Numbers P

· · 题解

2026.09.26 upd. 修改了错误的代码3

约定

思路

step 1 设计 dp 状态

首先发现有一个很基础的思路,就是设 dp_{i,j} 表示前 i 个字符中结尾 3 个字符为 j 的“好字符串”的方案数。

然后 dp_{i,j} 要从 dp_{i-1, a},dp_{i-2, b},dp_{i-4, c} 等转移。

但是这样会算重。

:::info[例子]

::: 我们发现问题在于 $dp_{i,j}$ 和 $dp_{i-1, k}$ 中有重叠的方案,但是我们在转移中又没有将他们“隔离”,即有转移方程同时包含 $dp_{i,j}$ 和 $dp_{i-1, k}$。 考虑如何解决,我们可以把他们强行隔离开来,即把总体转移方式变为 $dp_{i-1} \to dp_i$,提前钦定一个字符串的字符。 具体状态为 $dp_{i,b0,b1,b2,b3,c}$($b0,b1,b2,b3$ 为布尔值,$c$ 为包含 $3$ 个字符的字符串)表示前 $i$ 个字符中最后 $3$ 个字符为 $c$ 的满足以下条件的好字符串的数量: - 若 $b0 = 1$,则前 $i-3$ 个字符的字符串是“好字符串”。 - 若 $b1 = 1$,则前 $i-2$ 个字符的字符串是“好字符串”。 - 若 $b2 = 1$,则前 $i-1$ 个字符的字符串是“好字符串”。 - 若 $b3 = 1$,则前 $i$ 个字符的字符串是“好字符串”。 给个图理解一下: ![](https://cdn.luogu.com.cn/upload/image_hosting/h6j4act1.webp) ## step 2 转移,答案,边界条件 答案就是 $dp_{n, any, any, any, 1, any}$ 的方案数($any$ 表示值无所谓)。 考虑向后转移。 设给定的字符串为 $s$,当前在转移 $dp_{i,b0,b1,b2,b3,c}$。 我们枚举第 $i+1$ 个字符是什么。 假设第 $i+1$ 个字符为 $t$,我们要转移到的状态为 $dp_{i+1, d0, d1, d2, d3, e}$。 首先将 $d0 \leftarrow b1$,$d1 \leftarrow b2$ ,$d2 \leftarrow b3$, $e$ 设为 $c[1 .. 2]+t$ 。 然后进行如下操作: 如果 $t = s_{i+1}$,`d3 = d3 || b3`。 如果 $s[i \ldots i+1]$ 是合法输入,且 $s[i \ldots i+1]$ 与 $e[2 \ldots 3]$ 同构,`d3 = d3 || b2`。 如果 $s[i-2 \ldots i+1]$ 是合法输入,且 $s[i-2 \ldots i+1]$ 与 $e$ 同构,`d3 = d3 || b0`。 然后就转移完成了。 边界条件是 $dp_{0, 0, 0, 0, 1, \texttt{000}} = 1$。 时间复杂度 $O(n)$,常数 $2^4 \times 9^4$(转移常数为 $9$),需要优化,具体常数优化在实现部分。 # 实现 这题的实现有挺多可以说的。 首先我们发现如果 $b0, b1, b2, b3$ 均等于 $0$,那么这个状态就可以丢弃了。 我们拟出第一版代码: :::success[代码1(12pts)] `state` 表示一个状态。 函数 `eq` 检查两个字符串是否同构。 函数 `check` 检查一个字符串是否是可输入的。 ```cpp #include <bits/stdc++.h> using namespace std; #define fir first #define sec second #define low_b lower_bound #define upp_b upper_bound #define imod(x) ((x)-((x) >= MOD)*MOD) template <class Vec_T> using vc=vector<Vec_T>; const int MOD=1e9+7; const vc<string> chk_sz2={"12", "23", "45", "56", "78", "89", "14", "25", "36", "47", "58", "69"}; const vc<string> chk_sz4={"1245", "2356", "4578", "5689"}; bool eq(string s1, string s2){ sort(s1.begin(), s1.end()); sort(s2.begin(), s2.end()); return s1 == s2; } bool check(string s){ sort(s.begin(), s.end()); if(s.size() == 1) return 1; else if(s.size() == 2 && find(chk_sz2.begin(), chk_sz2.end(), s) != chk_sz2.end()) return 1; else if(s.size() == 4 && find(chk_sz4.begin(), chk_sz4.end(), s) != chk_sz4.end()) return 1; else return 0; } struct state{ string c="000"; vc<bool> b=vc<bool>(4); int val=0; bool operator==(const state &a) const{ return c == a.c && b == a.b && val == a.val; } bool operator<(const state &a) const{ return c < a.c || (c == a.c && b < a.b); } void trans(vc<state> &to, char s1, string s2="00", string s4="0000"){ for(char t='1';t <= '9';t++){ state res=*this; res.b.erase(res.b.begin()); res.b.push_back(0); if(s1 == t) res.b[3]=res.b[3] || b[3]; if(eq(s2, c.substr(2, 1)+t) && check(s2)) res.b[3]=res.b[3] || b[2]; if(eq(s4, c+t) && check(s4)) res.b[3]=res.b[3] || b[0]; res.c.erase(res.c.begin()); res.c+=t; to.push_back(res); } } }; int main(){ ios::sync_with_stdio(0); cin.tie(0);cout.tie(0); int T; cin >> T; while(T--){ string s; cin >> s; s=' '+s; int n=s.size()-1; vc<state> v0(1), v1; v0[0].b[3]=1; v0[0].val=1; for(int i=1;i <= n;i++){ v1.clear(); for(state x : v0){ if(i <= 1) x.trans(v1, s[i]); else if(i <= 3) x.trans(v1, s[i], s.substr(i-1, 2)); else x.trans(v1, s[i], s.substr(i-1, 2), s.substr(i-3, 4)); } sort(v1.begin(), v1.end()); state pre; v0.clear(); for(state x : v1){ if(x.b == vc<bool>(4, 0)) continue; else if(x == pre){ v0.back().val=imod(v0.back().val+x.val); } else{ v0.push_back(x); pre=x; } } } int ans=0; for(state x : v0){ if(x.b[3] == 1){ ans=imod(ans+x.val); } } cout << ans << "\n"; } } ``` ::: 首先我们发现每次排序后去重太慢了,我们实现一个 `to_int` 实现 `state` 与 `int` 的互相转换。 其次我们发现每次枚举每一个数字是多余的,我们只枚举当前位置的前后 $4$ 个字符(总共 $7$ 个字符就行了)。 :::success[代码2 56pts] ```cpp #include <bits/stdc++.h> using namespace std; #define fir first #define sec second #define low_b lower_bound #define upp_b upper_bound #define imod(x) ((x)-((x) >= MOD)*MOD) template <class Vec_T> using vc=vector<Vec_T>; const int MOD=1e9+7; const vc<string> chk_sz2={"12", "23", "45", "56", "78", "89", "14", "25", "36", "47", "58", "69"}; const vc<string> chk_sz4={"1245", "2356", "4578", "5689"}; int dp[17005]; bool eq(string s1, string s2){ sort(s1.begin(), s1.end()); sort(s2.begin(), s2.end()); return s1 == s2; } bool check(string s){ sort(s.begin(), s.end()); if(s.size() == 1) return 1; else if(s.size() == 2 && find(chk_sz2.begin(), chk_sz2.end(), s) != chk_sz2.end()) return 1; else if(s.size() == 4 && find(chk_sz4.begin(), chk_sz4.end(), s) != chk_sz4.end()) return 1; else return 0; } struct state{ string c="000"; vc<bool> b=vc<bool>(4); int val=0; bool operator==(const state &a) const{ return c == a.c && b == a.b && val == a.val; } bool operator<(const state &a) const{ return c < a.c || (c == a.c && b < a.b); } int to_int(){ return b[0]*1000+b[1]*2000+b[2]*4000+b[3]*8000+stoll(c); } void trans(vc<int> &to, vc<char> &v, char s1, string s2="", string s4=""){ for(char t : v){ state res=*this; res.b.erase(res.b.begin()); res.b.push_back(0); if(s1 == t) res.b[3]=res.b[3] || b[3]; if(eq(s2, c.substr(2, 1)+t) && check(s2)) res.b[3]=res.b[3] || b[2]; if(eq(s4, c+t) && check(s4)) res.b[3]=res.b[3] || b[0]; res.c.erase(res.c.begin()); res.c+=t; int id=res.to_int(); if(dp[id] == 0){ to.push_back(id); } dp[id]=imod(dp[id]+val); } } }; state to_state(int x){ int tmp=x/1000; state res; res.c="000"; res.c[0]+=x%1000/100; res.c[1]+=x%100/10; res.c[2]+=x%10; res.b[0]=(tmp >> 0) & 1; res.b[1]=(tmp >> 1) & 1; res.b[2]=(tmp >> 2) & 1; res.b[3]=(tmp >> 3) & 1; return res; } int main(){ ios::sync_with_stdio(0); cin.tie(0);cout.tie(0); int T; cin >> T; while(T--){ string s; cin >> s; s=' '+s; int n=s.size()-1; vc<state> v0(1); vc<int> v1; v0[0].b[3]=1; v0[0].val=1; for(int i=1;i <= n;i++){ vc<char> V; for(int j=max(1, i-3);j <= min(n, i+3);j++){ V.push_back(s[j]); } sort(V.begin(), V.end()); int m=unique(V.begin(), V.end())-V.begin(); V.assign(V.begin(), V.begin()+m); v1.clear(); for(state x : v0){ if(i <= 1) x.trans(v1, V, s[i]); else if(i <= 3) x.trans(v1, V, s[i], s.substr(i-1, 2)); else x.trans(v1, V, s[i], s.substr(i-1, 2), s.substr(i-3, 4)); } v0.clear(); for(int x : v1){ state tmp=to_state(x); tmp.val=dp[x]; dp[x]=0; if(tmp.b == vc<bool>(4, 0)) continue; v0.push_back(tmp); } } int ans=0; for(state x : v0){ if(x.b[3] == 1){ ans=imod(ans+x.val); } } cout << ans << "\n"; } } ``` ::: 这份代码有两个瓶颈: 1. 每次 `check` 和 `eq` 太慢了。 2. `state` 本身常数太大了。 先解决第二个瓶颈。我们将原来的 `vc<bool>` 和 `string` 都换成 `int`。 然后再加点小剪枝就变成了这样。 :::success[代码3 56pts] ```cpp #include <bits/stdc++.h> using namespace std; #define fir first #define sec second #define low_b lower_bound #define upp_b upper_bound #define imod(x) ((x)-((x) >= MOD)*MOD) #define to_int(a, b) ((a)*1000+(b)) template <class Vec_T> using vc=vector<Vec_T>; const int MOD=1e9+7; const vc<string> chk_sz2={"12", "23", "45", "56", "78", "89", "14", "25", "36", "47", "58", "69"}; const vc<string> chk_sz4={"1245", "2356", "4578", "5689"}; int dp[17005]; bool eq(string s1, string s2){ sort(s1.begin(), s1.end()); sort(s2.begin(), s2.end()); return s1 == s2; } bool check(string s){ sort(s.begin(), s.end()); if(s.size() == 1) return 1; else if(s.size() == 2 && find(chk_sz2.begin(), chk_sz2.end(), s) != chk_sz2.end()) return 1; else if(s.size() == 4 && find(chk_sz4.begin(), chk_sz4.end(), s) != chk_sz4.end()) return 1; else return 0; } struct state{ int b=0, c=0, val=0; bool operator==(const state &a) const{ return c == a.c && b == a.b && val == a.val; } bool operator<(const state &a) const{ return c < a.c || (c == a.c && b < a.b); } void trans(vc<int> &to, vc<char> &v, char &s1, string &s2, string &s4){ for(char t : v){ state res=*this; res.b>>=1; if(s1 == t) res.b |= (8*((b >> 3) & 1)); if(!((res.b >> 3) & 1) && eq(s2, to_string(c%10)+t) && check(s2)) res.b |= (8*((b >> 2) & 1)); if(!((res.b >> 3) & 1) && eq(s4, to_string(c)+t) && check(s4)) res.b |= 8*(((b >> 0) & 1)); res.c%=100; res.c*=10; res.c+=t-'0'; int id=to_int(res.b, res.c); if(!((res.b >> 0) & 1)) res.c%=100; if(dp[id] == 0){ to.push_back(id); } dp[id]=imod(dp[id]+val); } } }; state to_state(int x){ return {x/1000, x%1000, 0}; } int main(){ ios::sync_with_stdio(0); cin.tie(0);cout.tie(0); int T; cin >> T; while(T--){ string s; cin >> s; s=' '+s; int n=s.size()-1; vc<state> v0(1); vc<int> v1; v0[0].b=8; v0[0].val=1; for(int i=1;i <= n;i++){ vc<char> V; for(int j=max(1, i-3);j <= min(n, i+3);j++){ V.push_back(s[j]); } sort(V.begin(), V.end()); int m=unique(V.begin(), V.end())-V.begin(); V.assign(V.begin(), V.begin()+m); v1.clear(); string s2, s4; if(i > 1) s2=s.substr(i-1, 2); if(i > 3) s4=s.substr(i-3, 4); for(state x : v0){ x.trans(v1, V, s[i], s2, s4); } v0.clear(); for(int x : v1){ state tmp=to_state(x); tmp.val=dp[x]; dp[x]=0; if(tmp.b == 0) continue; v0.push_back(tmp); } } int ans=0; for(state x : v0){ if((x.b >> 3) & 1){ ans=imod(ans+x.val); } } cout << ans << "\n"; } } ``` ::: 既然都用 `int` 表示状态了,那干脆把所有可以换的 `string` 都换成 `int`,然后提前对字符串排序以预处理 `check` 和 `eq`。 :::success[代码4 100pts 最终代码] ```cpp #include <bits/stdc++.h> using namespace std; #define fir first #define sec second #define low_b lower_bound #define upp_b upper_bound #define imod(x) ((x)-((x) >= MOD)*MOD) #define to_int(a, b) ((a)*1000+(b)) template <class Vec_T> using vc=vector<Vec_T>; const int MOD=1e9+7; const vc<string> chk_all={"0001", "0002", "0003", "0004", "0005", "0006", "0007", "0008", "0009", "0012", "0023", "0045", "0056", "0078", "0089", "0014", "0025", "0036", "0047", "0058", "0069", "1245", "2356", "4578", "5689"}; int dp[17005], ssort[10005], chk[10005]; bool eq(string s1, string s2){ sort(s1.begin(), s1.end()); sort(s2.begin(), s2.end()); return s1 == s2; } bool eq(int s1, int s2){ return ssort[s1] == ssort[s2]; } bool check(string s){ sort(s.begin(), s.end()); return (find(chk_all.begin(), chk_all.end(), s) != chk_all.end()); } bool check(int x){ return chk[x]; } struct state{ int b=0, c=0, val=0; bool operator==(const state &a) const{ return c == a.c && b == a.b && val == a.val; } bool operator<(const state &a) const{ return c < a.c || (c == a.c && b < a.b); } void trans(vc<int> &to, vc<char> &v, int s1, int s2, int s4){ for(char t : v){ int num=t-'0'; state res=*this; res.b>>=1; if(s1 == num) res.b |= (8*((b >> 3) & 1)); if(!((res.b >> 3) & 1) && eq(s2, c%10*10+num) && check(s2)) res.b |= (8*((b >> 2) & 1)); if(!((res.b >> 3) & 1) && eq(s4, c*10+num) && check(s4)) res.b |= 8*(((b >> 0) & 1)); res.c%=100; res.c*=10; res.c+=t-'0'; int id=to_int(res.b, res.c); if(dp[id] == 0){ to.push_back(id); } dp[id]=imod(dp[id]+val); } } }; state to_state(int x){ return {x/1000, x%1000, 0}; } int main(){ ios::sync_with_stdio(0); cin.tie(0);cout.tie(0); if(1){ // init string blank="00000"; for(int i=0;i <= 10000;i++){ string s=to_string(i); while(s.size() < 4) s='0'+s; chk[i]=check(s); sort(s.begin(), s.end()); ssort[i]=stoll(s); } } int T; cin >> T; while(T--){ string s; cin >> s; s=' '+s; int n=s.size()-1; vc<state> v0(1); vc<int> v1; v0[0].b=8; v0[0].val=1; for(int i=1;i <= n;i++){ vc<char> V; for(int j=max(1, i-3);j <= min(n, i+3);j++){ V.push_back(s[j]); } sort(V.begin(), V.end()); int m=unique(V.begin(), V.end())-V.begin(); V.assign(V.begin(), V.begin()+m); v1.clear(); int s2=0, s4=0; if(i > 1) s2=stoll(s.substr(i-1, 2)); if(i > 3) s4=stoll(s.substr(i-3, 4)); for(state x : v0){ x.trans(v1, V, s[i]-'0', s2, s4); } v0.clear(); for(int x : v1){ state tmp=to_state(x); tmp.val=dp[x]; dp[x]=0; if(tmp.b == 0) continue; v0.push_back(tmp); } } int ans=0; for(state x : v0){ if((x.b >> 3) & 1){ ans=imod(ans+x.val); } } cout << ans << "\n"; } } ``` :::