[编程题] 丑陋的字符串

牛牛喜欢字符串,但是他讨厌丑陋的字符串。对于牛牛来说,一个字符串的丑陋值是字符串中相同连续字符对的个数。比如字符串“ABABAABBB”的丑陋值是3,因为有一对"AA"和两对重叠的"BB"。现在给出一个字符串,字符串中包含字符'A'、'B'和'?'。牛牛现在可以把字符串中的问号改为'A'或者'B'。牛牛现在想让字符串的丑陋值最小,希望你能帮帮他。

输入描述:
输入包括一个字符串s,字符串长度length(1 ≤ length ≤ 50),字符串只包含'A','B','?'三种字符。


输出描述:
输出一个整数,表示最小的丑陋值

输入例子1:
A?A

输出例子1:
0

#include <iostream>
#include <string>

using namespace std;

int main()
{
	string str;
	while (cin >> str)
	{
		if (str[0] == 'q')
			break;
		int len = str.length();
		//去掉前后?,不影响结果
		if (len < 2)
		{
			cout << 0 << endl;
			return 0;
		}
		int count = 0;

		int Lstr = 0;
		int Rstr = len - 1;
		while (str[Lstr] == '?')
			Lstr++;
		while (str[Rstr] == '?')
			Rstr--;

		for (int i = Lstr; i < Rstr; i++)
		{
			if (str[i] == '?')
				str[i] = str[i - 1] == 'A' ? 'B' : 'A';

			if (str[i] == str[i + 1])
				count++;
		}
		cout << count << endl;
	}
	system("pause");
	return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值