360笔试题——处理字符串

题目要求:
先输入要处理的字符串个数n,然后输入n个字符串,经过处理输出字符串。

处理过程:'#','@'为控制符,不应出现在字符中。如果遇到'#',再删除前一个字符和'#',如果遇到'@',则应删除'@'前面所有的字符和'@'。

e.g.

输入:

2

abcc#de

kj@abcde

输出:

abcde

abcde

方式1:低级的方法,用链表

#include <iostream>
#include <string>
using namespace std;
struct node
{
	char data;
	struct node *next;
};// 注:这里仅仅是声名,可以在后面进行定义。
struct node* creat(string str)
{
	struct node *head, *p1, *p2;
	head = p1 = p2 = (struct node *)malloc(sizeof(struct node));
	string::size_type i = 0;
	while (str[i] == '@' || str[i] == '#' || str[i+1] == '@' || str[i+1] == '#')
		i++;
	p1->data = str[i];
	i++;
	for (; i < str.size(); i++)
	{
		p2->next = p1;
		p2 = p1;
		p1 = (struct node *)malloc(sizeof(struct node));
		while (i + 1 < str.size() && (str[i] == '#' || str[i] == '@' || str[i + 1] == '#' || str[i + 1] == '@'))
		{
			if (str[i + 1] == '@' || str[i + 1] == '#')
			{
				if (str[i + 1] == '@')
				{
					head = p2 = p1;
				}
				i += 2;
			}
			else
			{
				if (str[i] == '@')
					head = p2 = p1;
				else;
				i++;
			}
		}
		if (i < str.size() )
			p1->data = str[i];
	}
	p2->next = p1;
	p1->next = nullptr;
	return head;
}
void print(node *head)
{
	node *p;
	p = head;
	if (nullptr != head)
	{
		while (nullptr != p)
		{
			cout << p->data;
			p = p->next;
		}
		cout << endl;
	}
	else
		cout << "链表无效!" << endl;
}
void main()
{
	string a[100] = {""};
	int cnt;
	cin >> cnt;
	for (int i = 0; i < cnt; i++)
	{
		cin >> a[i];
	}
	cout << "原链表:" << endl;
	for (int i = 0; i < cnt; i++)
	{
		cout << a[i] << endl;
	}

	//struct node *head;
	struct node *b[100];
	for (int i = 0; i < cnt; i++)
	{
		b[i] = creat(a[i]);
	}
	
	cout << "校验后:" << endl;
	for (int i = 0; i < cnt; i++)
	{
		print(b[i]);
	}
}
 

方式2:用容器vector,string,迭代器iterator;

#include <vector>
#include <iostream>
#include <string>
using namespace std;
void main()
{
	vector<string> str;
	int cnt;
	string t;
	cin >> cnt;
	for (int i = 0; i < cnt; i++)
	{
		cin >> t;
		str.push_back(t);
	}
	vector<string>::iterator it_vec;
	string::iterator it_str;
	for (it_vec = str.begin(); it_vec != str.end(); ++it_vec)
	{
		for (it_str = (*it_vec).begin(); it_str != (*it_vec).end();)
		{
			if (*it_str == '#')
			{
				if (it_str == (*it_vec).begin())
					(*it_vec).erase(it_str);
				else
				{
					(*it_vec).erase(it_str - 1);
					it_str--;
					(*it_vec).erase(it_str);
					it_str--;
					++it_str;
				}
			}
			else if (*it_str == '@')
			{
				it_str = (*it_vec).erase((*it_vec).begin(), it_str + 1);
			}
			else
				++it_str;
		}
	}
	for (it_vec = str.begin(); it_vec != str.end(); ++it_vec)
	{
		cout << *it_vec << endl;
	}	
}

方式3:精简的方法

#include <iostream>
#include <string>
using namespace std;
void main()
{
	string str;
	cin >> str;
	for (int i = 0; str[i] != '\0'; i++)
	{
		if (str[i] == '#')
		{
			if (i != 0)
				str[i - 1] = '#';
		}
		if (str[i] == '@')
		{
			for (int j = 0; j <= i; j++)
				str[j] = '@';
		}
	}
	for (int i = 0; str[i] != '\0'; i++)
	{
		if (str[i] != '#' && str[i] != '@')
			cout << str[i];
	}
	cout << endl;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值