咽帕蒂

在这里插入图片描述
.
.
.
.
.
分析
对于一个字符串
如果首尾的字符相同,则删去,答案加1
如果相邻的字符相同,则删去,答案加1
最后,所剩的字符若不为1
则答案加上 所剩字符个数/2-1
.
.
.
.
.
程序:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
string s;
int main()
{
	int ans=0,l,r,bz=1;
	cin>>s;
	int len=s.length()-1;
	while (bz!=0)
	{
	  	bz=0;
	  	for (int i=0;i<=len;i++)
	  	{
	  		if (s[i]!=' ')
	  		{
	  	  		l=i-1;
				r=i+1;
	  	  		if (l==-1) l=len;
	  	  		if (r>len) r=0;
	  	  		while (s[l]==' ')
	  	  		{
	  	  			l--;
	  				if (l<0) l=len;
		  		}
	  	  		while (s[r]==' ')
	  	  		{
	  	  			r++;	
	  	  			if (r>len) r=0;
		  		}
		  		if (l==i||r==i) break;
		  		if (s[i]==s[l])
		  		{
		  			ans++;
		  			bz=1;
		  			s[l]=s[i]=' ';
		  		} else 
				if (s[i]==s[r])
		  		{
		  			ans++;
		  			bz=1;
		  			s[r]=s[i]=' ';
		  		}
			}
	  	}
	}
	int tj=0;
	for (int i=0;i<=len;i++)
	 	if (s[i]!=' ') tj++;
	if (tj>1) ans+=tj/2-1;
	printf("%d",ans);
	return 0;
}
原文地址:https://www.cnblogs.com/YYC-0304/p/10292819.html