試題 基礎練習 FJ的字符串
資源限制
時間限制:1.0s 內存限制:512.0MB
問題描述
FJ在沙盤上寫了這樣一些字符串:
A1 = “A”
A2 = “ABA”
A3 = “ABACABA”
A4 = “ABACABADABACABA”
… …
你能找出其中的規律並寫所有的數列AN嗎?
輸入格式
僅有一個數:N ≤ 26。
輸出格式
請輸出相應的字符串AN,以一個換行符結束。輸出中不得含有多餘的空格或換行、回車符。
樣例輸入
3
樣例輸出
ABACABA
實現代碼
#include<iostream>
#include<string>
#include<cstring>
using namespace std;
int n;
char a[26];
string ans[26];
string dfs(int n) {
if (ans[n].length()) return ans[n];
if (n == 0) return ans[0] = a[0];
else return ans[n] = dfs(n - 1) + a[n] + dfs(n - 1);
}
int main() {
for (int i = 0; i < 26; i++) a[i] = 'A' + i;
cin >> n;
cout << dfs(n - 1) << endl;
return 0;
}