ZOJ-2571 Big String Outspread 模拟
生活随笔
收集整理的這篇文章主要介紹了
ZOJ-2571 Big String Outspread 模拟
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題意:給定一個字符串,按照要求輸出來。
解法:每次進行一次首字母判定,然后根據不同的情況進行遞歸。
代碼如下:
#include <cstdlib> #include <cstdio> #include <cstring> #include <string> #include <iostream> #include <algorithm> using namespace std;char str[300];string display(int ti, int sta, int fuck) {string ret;if (sta > fuck) return ret;if (sta == fuck && !isalpha(str[sta])) return ret;string tmp;if (!isalpha(str[sta])) { // 如果不是以字符開始if (isdigit(str[sta])) {int t = 0, i, j;for (i = sta; isdigit(str[i]); ++i) { // 如果是數字的話 t = t * 10 + (str[i] - '0'); // 得出循環的次數數字 }if (str[i] != '(') { // 如果后面不是括號,那么只有一位 tmp = display(t, i, i) + display(1, i+1, fuck);} else {int cnt = 0;for (j = sta; ; ++j) { // 找到括號在的位置 if (str[j] == ')') {--cnt;if (!cnt) break;}else if (str[j] == '(') ++cnt;}tmp = display(t, i+1, j-1) + display(1, j+1, fuck);}}else { // 如果是括號進入 int cnt = 0, j;for (j = sta; ; ++j) {if (str[j] == ')') {--cnt;if (!cnt) break; }else if (str[j] == '(') ++cnt;}tmp = display(1, sta, j-1) + display(1, j+1, fuck);}while (ti--) {ret += tmp;}} else {tmp = str[sta] + display(1, sta+1, fuck);while (ti--)ret += tmp; // 把后面的這一個字母進行重復 }return ret; }int main() {int T, len;scanf("%d", &T);while (T--) {scanf("%s", str);len = strlen(str);cout << display(1, 0, len-1) << endl;}return 0; }?
總結
以上是生活随笔為你收集整理的ZOJ-2571 Big String Outspread 模拟的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 搜索Maven的仓库
- 下一篇: C语言程序设计试题