Broken Necklace 坏掉的项链

来源:互联网 发布:苹果手机如何抹除数据 编辑:程序博客网 时间:2024/04/29 07:49
#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;elseleft = 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;elseright = r;}int temp;temp = left + right;cout<<temp<<endl;if(temp >= max)max = temp;}cout<<max<<endl;return 0;}

0 0
原创粉丝点击