版本比较
题解:P1820 麻将 加强加强版
正文
增加 372 行删除 436 行
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551[题目](https://www.luogu.com.cn/problem/P1820)# **先说方法(DP)**# 题目大意*你有n张牌,在你手上的牌基础上加一张要凑若干对三张连着或三张一样的加上一对两张一样的。**问加上那张牌可以?*## 前置总述普通麻将胡牌 DP 的难点在于顺子会跨三种牌,不能像刻子一样直接按种类贪心处理。我们不记录所有已经成型的面子,只记录**还没收尾、正在等待后续牌补齐的顺子**。由于新开顺子最多只需要开 2 个(开3个等价于一组刻子,没有意义),等待中的顺子数量只有 $0,1,2$ 三种可能,组合起来一共只有 $3\times 3=9$ 种状态。依靠这个极小的状态集,我们预处理前后缀 DP,就可以把原本每次 $O(n)$ 的 check 优化到预处理后 $O(1)$ 查询,整体复杂度压到 $O(n)$。# 观察题目## 其实刻子很好处理难就难在顺子*因为顺子会影响三个数而刻子只影响一个数***例如**| 第一项 |第二项 | 第三项 ||:-:|:-:|:-:|| 1 | 2 |3 || 4 | 5 | 6 ||i | i+1 | i+2 |**九种情况**| 以i-2开头的顺子数 | 以i-1开头的顺子数 ||:-:|:-:|| 0 | 0 || 0 | 1 || 0 | 2 ||1 | 0 || 1 | 1 || 1 | 2 || 2 | 0 || 2 | 1 || 2 | 2 |### 为什么最多新开 2 个顺子:若新开≥3 个顺子,等价换成刻子,因此只枚举 0/1/2。### 对应代码```cppint hui(int state,int v) {//state是编码号state=以i-2开头的顺子数*3+以i-1开头的顺子数 v是有几个iint a=state/3,b=state%3;if(v<a+b)return -1;int root=v-a-b;//消耗的ifor(int cc=0;cc<=2;++cc) {if(root>=cc&&(root-cc)%3==0)return b*3+cc;}return -1;}```1. 先补之前未完成的顺子2. 剩余牌组成刻子3. 剩余牌新开顺子# 主函数## 1 *先判断个数*```cppif(k%3!=1) {cout<<"QAQ\n";return 0;}```## 2 *初始化1*```cppfor(int i=0;i<=n+2;++i)c[i]=0;```c[i]表示有多少个i## 3 *输入*```cppfor(int i=0;i<k;++i) {int x;cin>>x;c[x]++;}```## 4 *初始化2*```cppfor(int s=0;s<9;++s) {dp1[n+1][s]=(s==0);//=0时为一否则为零dp0[n+1][s]=false;}```*dp1[i][s]:处理 \(i\sim n\),**这段区间里不能出雀头,只能全部是面子****dp0[i][s]:处理 \(i\sim n\),**这段区间里面恰好可以选一处放雀头(最多一次)***## 5 *计算dp1*```cppfor(int i=n;i>=1;--i) {for(int s=0;s<9;++s) {int ns=hui(s,c[i]);dp1[i][s]=(ns!=-1&&dp1[i+1][ns]);}}```### ns = hui(s, c[i]):当前位置有 c[i] 张牌,从状态 s 出发,消掉这些牌后转移到 ns### 若 ns != -1 且 dp1[i+1][ns] 为真,则 dp1[i][s] 为真## 6*计算dp0*```cppfor(int i=n;i>=1;--i) {for(int s=0;s<9;++s) {bool ok=false;int ns1=hui(s,c[i]);if(ns1!=-1&&dp0[i+1][ns1]){ok=true;}int ns2=hui(s,c[i]-2);if(ns2!=-1&&dp1[i+1][ns2]){ok=true;}dp0[i][s]=ok;}}```### 转移当前位置 i 不使用雀头:```cppint ns1=hui(s,c[i]);if(ns1!=-1&&dp0[i+1][ns1]){ok=true;}```当前位置 i 使用雀头:```cppint ns2=hui(s,c[i]-2);if(ns2!=-1&&dp1[i+1][ns2]){ok=true;}```## 7 *初始化3*```cpps[0]=0;v[0]=true;```### *v[i] = 前 i 种牌不使用雀头能否全部消成面子*### *s[i] = 若能,则唯一对应的状态*### *补充说明 hui 是确定性转移函数,同一输入状态与牌数只会得到唯一后继;前缀 v,s 这条路径全程不使用雀头、起点固定为状态 0,因此如果前 i 张能合法消完,状态一定唯一,只用单独存一个 s[i] ,不用布尔数组。**注意:该唯一性只针对这条无前缀雀头的转移,dp0、dp1、u 数组是允许多个状态可达的。**)*## 8 *计算前缀(计算v[i],s[i])*```cppfor(int i=1;i<=n;++i) {s[i]=-1;v[i]=false;if(v[i-1]) {int ns=hui(s[i-1],c[i]);if(ns!=-1) {s[i]=ns;v[i]=true;}}}```## 9 *初始化4*```cppfor(int s=0;s<9;++s){u[0][s]=false;}```### *u[i][s]处理完前 i 张牌,恰好已经使用过一次雀头,并且处理完第 i 张后的状态为 s,这种情况是否可达。*## 10 *计算前缀“已用雀头”可达状态 u*```cppfor(int i=1;i<=n;++i) {for(int s=0;s<9;++s){u[i][s]=false;}for(int s=0;s<9;++s) {if(u[i-1][s]) {int ns=hui(s,c[i]);if(ns!=-1)u[i][ns]=true;}}if(v[i-1]) {int ns=hui(s[i-1],c[i]-2);if(ns!=-1)u[i][ns]=true;}}```- ### 拆解1. *初始化5*```cppfor(int s=0;s<9;++s){u[i][s]=false;}```2. *延续“已经用过雀头”的状态*```cppfor(int s=0;s<9;++s) {if(u[i-1][s]) {int ns=hui(s,c[i]);if(ns!=-1)u[i][ns]=true;}}```3. *在当前位置 i 第一次使用雀头*```cppif(v[i-1]) {int ns=hui(s[i-1],c[i]-2);if(ns!=-1)u[i][ns]=true;}```## 11 判断每张牌 x 是否听```cppfor(int x=1;x<=n;++x) {bool b=false;for(int s=0;s<9&&!b;++s) {if(u[x-1][s]) {int ns=hui(s,c[x]+1);if(ns!=-1&&dp1[x+1][ns])b=true;}}if(!b&&v[x-1]) {int ns2=hui(s[x-1],c[x]+1);if(ns2!=-1&&dp0[x+1][ns2])b=true;if(!b) {int ns3=hui(s[x-1],c[x]-1);if(ns3!=-1&&dp1[x+1][ns3])b=true;}}if(b)ans[cnt++]=x;}```### 我们模拟摸上来 x,\(c[x]\)暂时变成 \(c[x]+1\),整组牌需要恰好总共 1 个雀头### 1.雀头在当前左边```cppfor(int s=0;s<9&&!b;++s) {if(u[x-1][s]) {int ns=hui(s,c[x]+1);if(ns!=-1&&dp1[x+1][ns])b=true;}}```### 2.雀头在当前右边```cppif(!b&&v[x-1]) {int ns2=hui(s[x-1],c[x]+1);if(ns2!=-1&&dp0[x+1][ns2])b=true;}```### 3.雀头在当前```cppif(!b) {int ns3=hui(s[x-1],c[x]-1);if(ns3!=-1&&dp1[x+1][ns3])b=true;}```### 4.记录```cppif(b)ans[cnt++]=x;```# 代码```cpp#include<bits/stdc++.h>#define int long longusing namespace std;const int MAXN=200005;int n,k,c[MAXN],s[MAXN],ans[MAXN],cnt;bool dp1[MAXN][9],dp0[MAXN][9],v[MAXN],u[MAXN][9];int hui(int state,int v) {int a=state/3,b=state%3;if(v<a+b)return -1;int root=v-a-b;for(int cc=0;cc<=2;++cc) {if(root>=cc&&(root-cc)%3==0)return b*3+cc;}return -1;}signed main() {ios::sync_with_stdio(false);cin.tie(nullptr);cin>>n>>k;if(k%3!=1) {cout<<"QAQ\n";return 0;}for(int i=0;i<=n+2;++i)c[i]=0;for(int i=0;i<k;++i) {int x;cin>>x;c[x]++;}for(int s=0;s<9;++s) {dp1[n+1][s]=(s==0);dp0[n+1][s]=false;}for(int i=n;i>=1;--i) {for(int s=0;s<9;++s) {int ns=hui(s,c[i]);dp1[i][s]=(ns!=-1&&dp1[i+1][ns]);}}for(int i=n;i>=1;--i) {for(int s=0;s<9;++s) {bool ok=false;int ns1=hui(s,c[i]);if(ns1!=-1&&dp0[i+1][ns1]){ok=true;}int ns2=hui(s,c[i]-2);if(ns2!=-1&&dp1[i+1][ns2]){ok=true;}dp0[i][s]=ok;}}s[0]=0;v[0]=true;for(int i=1;i<=n;++i) {s[i]=-1;v[i]=false;if(v[i-1]) {int ns=hui(s[i-1],c[i]);if(ns!=-1) {s[i]=ns;v[i]=true;}}}for(int s=0;s<9;++s){u[0][s]=false;}for(int i=1;i<=n;++i) {for(int s=0;s<9;++s){u[i][s]=false;}for(int s=0;s<9;++s) {if(u[i-1][s]) {int ns=hui(s,c[i]);if(ns!=-1)u[i][ns]=true;}}if(v[i-1]) {int ns=hui(s[i-1],c[i]-2);if(ns!=-1)u[i][ns]=true;}}cnt=0;for(int x=1;x<=n;++x) {bool b=false;for(int s=0;s<9&&!b;++s) {if(u[x-1][s]) {int ns=hui(s,c[x]+1);if(ns!=-1&&dp1[x+1][ns])b=true;}}if(!b&&v[x-1]) {int ns2=hui(s[x-1],c[x]+1);if(ns2!=-1&&dp0[x+1][ns2])b=true;if(!b) {int ns3=hui(s[x-1],c[x]-1);if(ns3!=-1&&dp1[x+1][ns3])b=true;}}if(b)ans[cnt++]=x;}if(cnt==0) {cout<<"QAQ\n";} else {for(int i=0;i<cnt;++i) {if(i)cout<<' ';cout<<ans[i];}cout<<'\n';}return 0;}```# 时间复杂度## **$O(9n)$**# 空间复杂度## **$O(9n)$**# 其余分数## 80($O(n^2)$)```#include<bits/stdc++.h>#define int long longusing namespace std;const int MAXN=200005;int n,k,c[MAXN];int hui(int state,int v){int a=state/3,b=state%3;if(v<a+b)return -1;int root=v-a-b;for(int cc=0;cc<=2;++cc){if(root>=cc&&(root-cc)%3==0)return b*3+cc;}return -1;}bool check(){bool dp[9][2]={};dp[0][0]=true;for(int i=1;i<=n;++i){bool ndp[9][2]={};for(int s=0;s<9;++s){for(int used=0;used<2;++used){if(!dp[s][used])continue;int ns1=hui(s,c[i]);if(ns1!=-1)ndp[ns1][used]=true;if(!used&&c[i]>=2){int ns2=hui(s,c[i]-2);if(ns2!=-1)ndp[ns2][1]=true;}}}memcpy(dp,ndp,sizeof dp);}return dp[0][1];}signed main(){ios::sync_with_stdio(false);cin.tie(nullptr);cin>>n>>k;if(k%3!=1){cout<<"QAQ\n";return 0;}for(int i=0;i<k;++i){int x;cin>>x;c[x]++;}vector<int>ans;for(int x=1;x<=n;++x){c[x]++;if(check())ans.push_back(x);c[x]--;}if(ans.empty()){cout<<"QAQ\n";}else{for(int i=0;i<(int)ans.size();++i){if(i)cout<<' ';cout<<ans[i];}cout<<'\n';}return 0;}```## 50($O(n^3)$)[对应题目](https://www.luogu.com.cn/problem/P4050)```cpp#include<bits/stdc++.h>using namespace std;int n,m,t[4001],a[4001];bool bj;bool check(){for(int i=1;i<=n;i++){if(t[i]>=2){bool ok=1;t[i]-=2;for(int j=1;j<=n+2;j++)a[j]=t[j];for(int j=1;j<=n+2;j++){if(a[j]<0){ok=0;break;}a[j]%=3;a[j+1]-=a[j];a[j+2]-=a[j];}t[i]+=2;if(ok)return 1;}}return 0;}int main(){cin>>n>>m;m=3*m+1;for(int i=1,x;i<=m;i++)cin>>x,t[x]++;for(int i=1;i<=n;i++){t[i]++;if(check())bj=1,cout<<i<<" ";t[i]--;}if(!bj)cout<<"NO";return 0;}```# 后言## 本蒟蒻的第一篇题解求过123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487# [题目传送门](https://www.luogu.com.cn/problem/P1820)# P1820 题解:状态压缩 DP 判断听牌## 题意有若干种牌,手中有 $k$ 张牌。问再加入哪一张牌后可以胡牌;若不存在,输出 `QAQ`。胡牌要求为:- 全部牌可以分成若干个面子;- 恰好剩下一对将牌,也称雀头。其中:- 面子为三张连续牌组成的顺子,或三张相同牌组成的刻子;- 雀头为两张相同的牌。加入一张后总牌数应为 $3m+2$,因此原手牌张数必须满足$$k \equiv 1 \pmod 3$$否则一定不能胡牌。---## 主要思路麻将胡牌 DP 的难点在于顺子会跨越三种牌,不能像刻子一样只按单一牌种贪心处理。我们不记录所有已经成型的完整面子,只记录尚未完成、正在等待后续牌补齐的顺子。处理到第 $i$ 种牌时,未完成顺子只可能有两种来源:- 以 $i-2$ 开头的顺子;- 以 $i-1$ 开头的顺子。每种未完成顺子的数量最多为 $2$。如果新开 $3$ 个同起点的顺子,它们可以由三个刻子等价替换,因此没有必要枚举到 $3$。于是每种未完成顺子的数量只有 $0,1,2$ 三种可能,总状态数为$$3 \times 3 = 9$$记状态$$state = a \times 3 + b$$其中:- $a$ 表示以 $i-2$ 开头的未完成顺子数;- $b$ 表示以 $i-1$ 开头的未完成顺子数;- $a,b \in \{0,1,2\}$。---## 状态转移函数设当前第 $i$ 种牌有 $cnt$ 张,当前状态为 $state$。记$$a = state \div 3,\qquad b = state \bmod 3$$则必须先消耗 $a+b$ 张第 $i$ 种牌,用来补齐之前的未完成顺子。若$$cnt < a+b$$则当前状态非法,返回 $-1$。补齐后剩余$$rest = cnt - a - b$$接下来枚举新开顺子的数量 $c$,其中 $c \in \{0,1,2\}$。剩余牌中拿出 $c$ 张作为新顺子的开头,剩下的牌必须全部组成刻子,因此要求$$rest - c \ge 0$$且$$rest - c \equiv 0 \pmod 3$$若满足,则处理完第 $i$ 种牌后,新状态为$$state' = b \times 3 + c$$对应代码如下:```cppint nextState(int state, int cnt) {int a = state / 3;int b = state % 3;// 当前牌不足以补齐之前未完成的顺子if (cnt < a + b) return -1;int rest = cnt - a - b;// 枚举新开顺子的数量for (int c = 0; c <= 2; ++c) {if (rest >= c && (rest - c) % 3 == 0) {return b * 3 + c;}}return -1;}```---## 后缀 DP定义:- `dp1[i][s]`:处理第 $i$ 到第 $n$ 种牌,不使用雀头,从状态 $s$ 出发能否全部消完;- `dp0[i][s]`:处理第 $i$ 到第 $n$ 种牌,恰好使用一次雀头,从状态 $s$ 出发能否全部消完。初始化:```cppfor (int s = 0; s < 9; ++s) {dp1[n + 1][s] = (s == 0);dp0[n + 1][s] = false;}```转移时:### 不使用雀头```cppint ns = nextState(s, c[i]);dp1[i][s] = (ns != -1 && dp1[i + 1][ns]);```### 恰好使用一次雀头有两种可能:1. 第 $i$ 种牌不使用雀头,雀头在右侧已经使用;2. 第 $i$ 种牌使用两张作为雀头,右侧不再使用雀头。```cppbool ok = false;int ns1 = nextState(s, c[i]);if (ns1 != -1 && dp0[i + 1][ns1]) {ok = true;}int ns2 = nextState(s, c[i] - 2);if (ns2 != -1 && dp1[i + 1][ns2]) {ok = true;}dp0[i][s] = ok;```---## 前缀 DP定义:- `v[i]`:前 $i$ 种牌不使用雀头能否全部消完;- `s[i]`:若前 $i$ 种牌不使用雀头能全部消完,对应的唯一状态;- `u[i][s]`:前 $i$ 种牌恰好使用一次雀头,处理完后状态为 $s$ 是否可达。初始化:```cppv[0] = true;s[0] = 0;```计算 `v` 和 `s`:```cppfor (int i = 1; i <= n; ++i) {s[i] = -1;v[i] = false;if (v[i - 1]) {int ns = nextState(s[i - 1], c[i]);if (ns != -1) {s[i] = ns;v[i] = true;}}}```计算 `u`:```cppfor (int s = 0; s < 9; ++s) {u[0][s] = false;}for (int i = 1; i <= n; ++i) {for (int s = 0; s < 9; ++s) {u[i][s] = false;}// 雀头已经在前面使用过for (int s = 0; s < 9; ++s) {if (u[i - 1][s]) {int ns = nextState(s, c[i]);if (ns != -1) {u[i][ns] = true;}}}// 雀头在第 i 种牌处第一次使用if (v[i - 1]) {int ns = nextState(s[i - 1], c[i] - 2);if (ns != -1) {u[i][ns] = true;}}}```---## 判断每张牌是否可以胡对于每一种牌 $x$,模拟加入一张 $x$,即第 $x$ 种牌的数量暂时变为 $c[x]+1$。需要检查三种雀头位置。### 1. 雀头在左侧```cppfor (int s = 0; s < 9 && !ok; ++s) {if (u[x - 1][s]) {int ns = nextState(s, c[x] + 1);if (ns != -1 && dp1[x + 1][ns]) {ok = true;}}}```### 2. 雀头在右侧```cppif (!ok && v[x - 1]) {int ns2 = nextState(s[x - 1], c[x] + 1);if (ns2 != -1 && dp0[x + 1][ns2]) {ok = true;}}```### 3. 雀头在当前牌 $x$ 处加入一张后,第 $x$ 种牌共有 $c[x]+1$ 张。若取两张作为雀头,则用于组成面子的数量为$$c[x]+1-2=c[x]-1$$此时左右两侧都不能再使用雀头。```cppif (!ok) {int ns3 = nextState(s[x - 1], c[x] - 1);if (ns3 != -1 && dp1[x + 1][ns3]) {ok = true;}}```若三种情况中有任意一种成立,则牌 $x$ 可以作为答案记录。---## 参考代码```cpp#include <bits/stdc++.h>using namespace std;int nextState(int state, int cnt) {int a = state / 3;int b = state % 3;// 当前牌不足以补齐之前未完成的顺子if (cnt < a + b) return -1;int rest = cnt - a - b;// 枚举新开顺子的数量for (int c = 0; c <= 2; ++c) {if (rest >= c && (rest - c) % 3 == 0) {return b * 3 + c;}}return -1;}int main() {ios::sync_with_stdio(false);cin.tie(nullptr);int n, k;cin >> n >> k;vector<int> c(n + 3, 0);for (int i = 0; i < k; ++i) {int x;cin >> x;++c[x];}// 加入一张后,总牌数必须为 3m + 2if (k % 3 != 1) {cout << "QAQ\n";return 0;}vector<array<char, 9>> dp1(n + 3), dp0(n + 3);for (int s = 0; s < 9; ++s) {dp1[n + 1][s] = (s == 0);dp0[n + 1][s] = false;}// 后缀 DPfor (int i = n; i >= 1; --i) {for (int s = 0; s < 9; ++s) {int ns = nextState(s, c[i]);dp1[i][s] = (ns != -1 && dp1[i + 1][ns]);bool ok = false;int ns1 = nextState(s, c[i]);if (ns1 != -1 && dp0[i + 1][ns1]) {ok = true;}int ns2 = nextState(s, c[i] - 2);if (ns2 != -1 && dp1[i + 1][ns2]) {ok = true;}dp0[i][s] = ok;}}vector<char> v(n + 1, false);vector<int> s(n + 1, -1);v[0] = true;s[0] = 0;// 前缀不使用雀头的状态for (int i = 1; i <= n; ++i) {if (v[i - 1]) {int ns = nextState(s[i - 1], c[i]);if (ns != -1) {v[i] = true;s[i] = ns;}}}vector<array<char, 9>> u(n + 1);for (int st = 0; st < 9; ++st) {u[0][st] = false;}// 前缀恰好使用一次雀头的状态for (int i = 1; i <= n; ++i) {for (int st = 0; st < 9; ++st) {u[i][st] = false;}for (int st = 0; st < 9; ++st) {if (u[i - 1][st]) {int ns = nextState(st, c[i]);if (ns != -1) {u[i][ns] = true;}}}if (v[i - 1]) {int ns = nextState(s[i - 1], c[i] - 2);if (ns != -1) {u[i][ns] = true;}}}vector<int> ans;for (int x = 1; x <= n; ++x) {bool ok = false;// 情况 1:雀头在左侧for (int st = 0; st < 9 && !ok; ++st) {if (u[x - 1][st]) {int ns = nextState(st, c[x] + 1);if (ns != -1 && dp1[x + 1][ns]) {ok = true;}}}// 情况 2、3:左侧未使用雀头if (!ok && v[x - 1]) {// 情况 2:雀头在右侧int ns2 = nextState(s[x - 1], c[x] + 1);if (ns2 != -1 && dp0[x + 1][ns2]) {ok = true;}// 情况 3:雀头在当前牌 x 处if (!ok) {int ns3 = nextState(s[x - 1], c[x] - 1);if (ns3 != -1 && dp1[x + 1][ns3]) {ok = true;}}}if (ok) {ans.push_back(x);}}if (ans.empty()) {cout << "QAQ\n";} else {for (int i = 0; i < (int)ans.size(); ++i) {if (i > 0) cout << ' ';cout << ans[i];}cout << '\n';}return 0;}```---## 复杂度分析状态数固定为 $9$,每种牌只会被常数次枚举,因此:- 时间复杂度:$$O(9n)=O(n)$$- 空间复杂度:$$O(9n)=O(n)$$