首页 > 代码库 > uva 11988 这题可以看出c++中string效率的底下
uva 11988 这题可以看出c++中string效率的底下
用c语言实现
#include <iostream> #include <string.h> #include <stdio.h> using namespace std; typedef struct node { char x; struct node *next; }chan; chan *root = new chan; char a[100010]; int main() { while(scanf("%s" , a) != EOF) { int i ; root->next = NULL; int n = strlen(a); chan *r = root , *q = root; for(i = 0; i < n; i++) { if(a[i] == ‘[‘) { r = root; } else if(a[i] == ‘]‘) { r = q; } else { chan *t = new chan; t->x = a[i]; t->next = r->next; r->next = t; r = t; //cout<<root->next->x<<endl; if(r->next == NULL) q = t; } } r = root->next; while(r != NULL) { q = r; printf("%c" , r->x); r =r->next; delete q; } cout<<endl; } return 0; }
用c++的string实现
#include <iostream> #include <string.h> #include <string> using namespace std; int main() { string a; while(cin>>a) { string b; int n = a.size(); int i; string::iterator x , y; x = b.begin(); y = b.end(); for(i = 0; i < n; i++) { if(a[i] == ‘[‘) { if(b.size == 0) continue; x = b.begin(); } else if(a[i] == ‘]‘) { if(b.size == 0) continue; x = b.end(); } else { x = b.insert(x , a[i]); x++; y = b.end(); } } cout<<b<<endl; } return 0; }
结果TEL
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。