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

本文深入探讨了使用C语言和C++实现的实用编程技巧,包括字符串操作、数组处理和数据结构应用。通过实例展示了如何在实际项目中高效解决问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

用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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值