NBUT [1369] A Breaking Computer
A Breaking Computer
题目: http://cdn.ac.nbutoj.com/Problem/view.xhtml?id=1369
参考了网上的代码才ac了,尝试了用string来做,但是TLE了,做了我n久的,桑心死了,基础很重要啊。
1 include <iostream> 2 #include <vector> 3 #include <stdio.h> 4 using namespace std; 5 #define MAXN 200000 6 7 char ht[MAXN+10]; 8 vector <char> ans[MAXN+10]; 9 10 int main() 11 { 12 while(~scanf("%s",ht)) 13 { 14 int i,r,kar; 15 for(i=0;i<MAXN;i++) 16 ans[i].clear(); 17 r = 0; 18 kar = 0; 19 for(i=0;ht[i]!='\0';i++) 20 { 21 if(ht[i]=='[') 22 { 23 r++; 24 kar = r; 25 } 26 else if(ht[i]==']') 27 { 28 kar = 0; 29 } 30 else 31 { 32 ans[kar].push_back(ht[i]); 33 } 34 } 35 for(i=r;i>=0;i--) 36 { 37 for(int j=0;j<ans[i].size();j++) 38 { 39 printf("%c",ans[i][j]); 40 } 41 } 42 printf("\n"); 43 } 44 return 0; 45 }
1 #include <iostream> 2 #include <vector> 3 #include <string> 4 #include <stdio.h> 5 using namespace std; 6 #define MAXN 200000 7 8 char ht[MAXN+10]; 9 10 int main() 11 { 12 string str1,str2,str3; 13 while(~scanf("%s",ht)) 14 { 15 str1 = ht; 16 int a=0,b=0,c=0; 17 a = str1.find('[',b); 18 if(a==string::npos) 19 { 20 printf("%s\n",str1.c_str()); 21 continue; 22 } 23 while(a!=string::npos) 24 { 25 str2.append(str1,b,a-b); 26 c = str1.find(']',b); 27 if(c==string::npos) c = str1.length(); 28 str3.assign(str1,a+1,c-a-1); 29 str2 = str3 + str2 ; 30 b = c+1; 31 a = str1.find('[',b); 32 } 33 if(c!=str1.length()) 34 { 35 c = str1.length(); 36 str2.append(str1,b,c); 37 } 38 printf("%s\n",str2.c_str()); 39 str1.clear(); 40 str2.clear(); 41 } 42 return 0; 43 }

浙公网安备 33010602011771号