字符序列
today’s 回溯2333
問題描述
從三個元素的集合 [A,B,C][A,B,C] 中選取元素生成一個 NN 個字符組成的序列,使得沒有兩個相鄰字的子序列(子序列長度=2)相同。例:N=5N=5時 ABCBA 是合格的,而序列 ABCBC 與 ABABC 是不合格的,因為其中子序列BC,AB是相同的。
對于由鍵盤輸入的 N(1≤N≤12)N(1≤N≤12),求出滿足條件的 NN 個字符的所有序列和其總數。
輸入格式
一行,一個整數 NN。
輸出格式
一個整數,即滿足條件的序列總數。
樣例一
input
4output
72數據范圍與約定
時間限制:??????????1s1s
內存限制: 256MB256MB
?
?
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n; 4 int ans; 5 int d[20]; 6 void search (int n,int k=0){ 7 if(n==k) { 8 ans++; 9 return ; 10 } 11 for(char ss='A';ss<='C';ss++){ 12 d[k]=ss; 13 bool aaa=true; 14 if(k>2 && d[k]==d[k-2] && d[k-1]==d[k-3]) aaa=false; 15 if(aaa) search (n,k+1); 16 } 17 } 18 int main(){ 19 cin>>n; 20 search(n); 21 cout<<ans<<endl; 22 return 0; 23 }?
轉載于:https://www.cnblogs.com/luv-letters/p/8568603.html
總結
- 上一篇: SpringBoot使用Thymelea
- 下一篇: tomcat启动时,报java.io.E