关于字符串处理的递归题,当然有的大神用栈做也能ac
2 1(1a2b1(ab)1c) 3(ab2(4ab))
abbabc abaaaabaaaababaaaabaaaababaaaabaaaab
思路其实也很好想,无非两个问题,1、get数字;2、去括号输出
我是这么想的,例如输入一个字符串str ,定义两个函数分别是Output(int n, int left, int right),和get_num(int index)分别用来输出(left,right)(开区间)里的内容和get要输出的次数。因为每个括号都是一个同样的问题只是括号的位置不同,那么我一开始可以调用Output(1,-1,strlen(str))意思就是把(-1,len)这个区间里的字符串输出一次。然后遍历输出或者处理括号就可以ac,不过千万记得括号的匹配要考虑各种情况不然很容易出问题!对 我就这么wa了一发,懵逼了好半天不知道是为什么,最后实在搞不定了就去网上找题解但是也懒得看,所幸第一篇题解就给了你测试数据,正好测出了我括号匹配有问题。下面贴代码(代码一是错误代码,也就是括号匹配有问题的代码,代码二是ac的)。
/* * test.cpp * * Created on: 2016年3月12日 * Author: Triose */ #include<stdio.h> #include<iostream> #include<string> #include<string.h> #include<algorithm> #include<vector> #include<queue> #include<stack> #include<iterator> #include<math.h> #include<stdlib.h> #include<map> #include<set> using namespace std; //#define ONLINE_JUDGE #define eps 1e-8 #define INF 0x7fffffff #define inf 0x3f3f3f3f #define rep(i,a) for((i)=0; i<(a);(i)++) #define mem(a,b) (memset((a),b,sizeof(a))) #define sf(a) scanf("%d",&a) #define sfI(a) scanf("%I64d",&a) #define sfd(a,b) scanf("%d%d",&a,&b) #define sft(a,b,c) scanf("%d%d%d",&a,&b,&c) #define sfs(a) scanf("%s",a) #define pf(a) printf("%d\n",a) #define pfs(a) printf("%s\n",a) #define pfI(a) printf("%I64d\n",a) #define LL __int64 const double PI = acos(-1.0); template<class T> T gcd(T a, T b) { return b ? gcd(b, a%b) : a; } template<class T> T lcm(T a, T b) { return a / gcd(a, b)*b; } template<class T> inline T Min(T a, T b) { return a<b ? a : b; } template<class T> inline T Max(T a, T b) { return a>b ? a : b; } int n, m; #define N 260 char str[N]; int s,e; int get_num(int& index) { int num = 0; while(str[index] <= '9' && str[index] >= '0') { num = num * 10 + (str[index++] - '0'); } return num; } void Output(int times, int left, int right) { int next_times = 0; for(int t = 0; t < times; t++) { s = left; e = right; for(int i = left + 1; i < right; i++) { if(str[i] <= '9' && str[i] >= '0') { next_times = get_num(i); if(str[i] != '(') { for(int j = 0; j < next_times ; j++) { putchar(str[i]); } } else { s = i;<span style="white-space:pre"> </span>//这里的括号匹配有问题,并不是所有的括号都是镶嵌着的,可能有平级的 e--; while(str[e] != ')') { e--; } i = e; Output(next_times, s, e); } } else if(str[i] <= 'z' && str[i] >= 'a') { putchar(str[i]); } else { continue; } } } } int main() { int t; sf(t); while(t--) { sfs(str); s = -1; e = strlen(str); Output(1,s,e); putchar(10); } return 0; }
/* * test.cpp * * Created on: 2016年3月12日 * Author: Triose */ #include<stdio.h> #include<iostream> #include<string> #include<string.h> #include<algorithm> #include<vector> #include<queue> #include<stack> #include<iterator> #include<math.h> #include<stdlib.h> #include<map> #include<set> using namespace std; //#define ONLINE_JUDGE #define eps 1e-8 #define INF 0x7fffffff #define inf 0x3f3f3f3f #define rep(i,a) for((i)=0; i<(a);(i)++) #define mem(a,b) (memset((a),b,sizeof(a))) #define sf(a) scanf("%d",&a) #define sfI(a) scanf("%I64d",&a) #define sfd(a,b) scanf("%d%d",&a,&b) #define sft(a,b,c) scanf("%d%d%d",&a,&b,&c) #define sfs(a) scanf("%s",a) #define pf(a) printf("%d\n",a) #define pfs(a) printf("%s\n",a) #define pfI(a) printf("%I64d\n",a) #define LL __int64 const double PI = acos(-1.0); template<class T> T gcd(T a, T b) { return b ? gcd(b, a%b) : a; } template<class T> T lcm(T a, T b) { return a / gcd(a, b)*b; } template<class T> inline T Min(T a, T b) { return a<b ? a : b; } template<class T> inline T Max(T a, T b) { return a>b ? a : b; } int n, m; #define N 260 char str[N]; int s,e; int get_num(int& index) { int num = 0; while(str[index] <= '9' && str[index] >= '0') { num = num * 10 + (str[index++] - '0'); } return num; } void Output(int times, int left, int right) { int next_times = 0; for(int t = 0; t < times; t++) { s = left; e = right; for(int i = left + 1; i < right; i++) { if(str[i] <= '9' && str[i] >= '0') { next_times = get_num(i); if(str[i] != '(') { for(int j = 0; j < next_times ; j++) { putchar(str[i]); } } else { s = i; // e--; // while(str[e] != ')') { // e--; // } int flag = 1;<span style="white-space:pre"> </span>//对 ,我就这样想了个蠢办法搞定了 for(e = s + 1; ; e++) { if(str[e] == '(') flag++; if(str[e] == ')') flag--; if(str[e] == ')' && flag == 0) break; } i = e; Output(next_times, s, e); } } else if(str[i] <= 'z' && str[i] >= 'a') { putchar(str[i]); } else { continue; } } } } int main() { #ifndef ONLINE_JUDGE freopen("in.txt","r",stdin); // freopen("Out.txt", "w", stdout); #endif int t; sf(t); while(t--) { sfs(str); s = -1; e = strlen(str); Output(1,s,e); putchar(10); } return 0; }