码迷,mamicode.com
首页 > 其他好文 > 详细

蓝桥杯 ALGO-110 字符串的展开(字符串)

时间:2015-03-27 22:20:17      阅读:626      评论:0      收藏:0      [点我收藏+]

标签:acm   蓝桥杯   

【思路】:按照题意来就行。注意当p1=2时,字母串和数字串的区别。推荐使用string,采用replace函数替换就行,char[]移动大量数据。

【AC代码】:

#include <iostream>
#include <algorithm>
#include <string>
#include <cstdio>
#include <cstring>
using namespace std;

#define MAX 10000+5

int isLower(char c)
{
	if (c >= 'a' && c <= 'z')
		return 1;
	return 0;
}

int isNum(char c)
{
	if (c >= '0' && c <= '9')
		return 1;
	return 0;
}

void reverse(char repstr[])
{
	int i = 0;
	for (i = 0; i < strlen(repstr)/2; i++)
	{
		char temp;
		temp = repstr[i];
		repstr[i] = repstr[strlen(repstr)-1-i];
		repstr[strlen(repstr)-1-i] = temp;
	}
}

int main()
{
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
	int p1 = 0, p2 = 0, p3 = 0, i = 0, j = 0, k = 0;
	string str;
	
	//input
	cin >> p1 >> p2 >> p3;
	cin >> str;
	
	//handle
	for (i = 1; i < str.length()-1; i++) //注意范围 
	{
		if ('-' == str[i])
		{
			int flag = 0;
			char repstr[MAX];
			if (isLower(str[i-1]) && isLower(str[i+1]) && str[i+1]>str[i-1])
				flag = 1;
			else if (isNum(str[i-1]) && isNum(str[i+1]) && str[i+1]>str[i-1])
				flag = 2;
			 if (0==flag)
			 	continue;
			
			//replace p1 p2 
			int cnt = 0;
			for (j = str[i-1]+1; j < str[i+1]; j++)
			{
				for (k = 0; k < p2; k++)
				{
					if (1 == p1)
					{
						repstr[cnt++] = j;
					}
					else if (2 == p1)
					{
						if (1 == flag)
							repstr[cnt++] = j+'A'-'a';
						else if (2 == flag)
							repstr[cnt++] = j;
					}
					else if (3 == p1)
					{
						repstr[cnt++] = '*';
					}
				}
			}
			repstr[cnt] = '\0';
			
			//p3
			if (2 == p3)
				reverse(repstr);
				
			//replace
			string rep = repstr;
			str.replace(i, 1, rep);
		}	
	}
	
	//output
	cout << str;
	return 0;
}

备注:

1.C++string类常用函数

2. string 与char* char[]之间的转换


蓝桥杯 ALGO-110 字符串的展开(字符串)

标签:acm   蓝桥杯   

原文地址:http://blog.csdn.net/weijj6608/article/details/44681631

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