码迷,mamicode.com
首页 > 编程语言 > 详细

uva 11988 这题可以看出c++中string效率的底下

时间:2014-04-29 13:47:20      阅读:358      评论:0      收藏:0      [点我收藏+]

标签:style   blog   os   io   2014   for   

用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;
}

mamicode.com,码迷


用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

uva 11988 这题可以看出c++中string效率的底下,码迷,mamicode.com

uva 11988 这题可以看出c++中string效率的底下

标签:style   blog   os   io   2014   for   

原文地址:http://blog.csdn.net/zengchen__acmer/article/details/24670931

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!