Broken Necklace 坏掉的项链

#include <iostream>
#include <string>
#include <stdlib.h>
#include <stdio.h>
using namespace std;

struct node
{
	char c;
	struct node * next;
	struct node * prev;
};

int main(int argc, char const *argv[])
{
	cout<<"Please input ?"<<endl;
	char c;
	c = getchar();
	struct node *head,*first,*p,*tail,*q,*t;
	head = (struct node*)malloc(sizeof(struct node));
	first = head;
	head->c = c;
	head->next = NULL;
	head->prev = NULL;
	c = getchar();
	while(c!= '!')
	{
		p = (struct node*)malloc(sizeof(struct node));
		head->next = p;
		p->next = NULL;
		p->prev = head;
		p->c = c;
		head = p;
		c = getchar();
	}
	tail = head;
	head->next = first;
	first->prev = head;
	// head = first->next;
	// cout<<first->c;
	// while(head!=first)
	// {
	// 	cout<<head->c;
	// 	head = head->next;
	// }

	// head = first->prev;
	// while(head != first)
	// {
	// 	cout<<head->c;
	// 	head = head->prev;
	// }
	// cout<<first->c;
	int max = 0;
	for(p=first;p!=tail;p=p->next)
	{
		int left = 1;
		if(p->c == 'b')
		{
			q = p->prev;
			while(q->c == 'b' or q->c =='w')
			{
				left++;
				q = q->prev;
			}
		}
		else if(p->c == 'r')
		{
			q = p->prev;
			while(q->c == 'r' or q->c == 'w')
			{
				left++;
				q = q->prev;
			}
		}
		else
		{
			int b = 1;
			int r = 1;
			q = p -> prev;
			t = p -> prev;
			while(q->c == 'b' or q->c == 'w')
			{
				b++;
				q = q->prev;
			}
			while(t->c == 'r' or t->c == 'w')
			{
				r++;
				t = t->prev;
			}
			if(b-r >= 0)
				left = b;
			else
				left = r;
		}
		int right = 1;
		if(p->next->c == 'b')
		{
			q = p->next->next;
			while(q->c == 'b' or q->c == 'w')
			{
				right++;
				q = q->next;
			}
		}
		else if(p->next->c == 'r')
		{
			q = p->next->next;
			while(q->c == 'r' or q->c == 'w')
			{
				right++;
				q = q->next;
			}
		}
		else
		{
			int b = 1;
			int r = 1;
			q = p->next->next;
			t = p->next->next;
			while(q->c == 'b' or q->c == 'w')
			{
				b++;
				q = q->next;
			}
			while(t->c == 'r' or t->c == 'w')
			{
				r++;
				t = t->next;
			}
			if(b-r >= 0)
				right = b;
			else
				right = r;
		}
		int temp;
		temp = left + right;
		cout<<temp<<endl;
		if(temp >= max)
			max = temp;
	}
	cout<<max<<endl;
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值