数据结构——编程珠玑
来源:互联网 发布:无所事事 知乎 编辑:程序博客网 时间:2024/06/06 17:29
先上程序:
#include "stdio.h"#include "stdlib.h"typedef struct BEAD{ char color; struct BEAD* prior; struct BEAD* next;}BEAD;int main(void){ int n,i,num,num_b=0,i_p,i_n,flag=1; char color_p='b'; BEAD *first,*goon,*before,*point,*check1,*check2; scanf("%d",&n); getchar(); first=(BEAD*) malloc(sizeof(BEAD)); first->color=getchar(); first->next=first; first->prior=first; point=before=first; for (i=2; i<=n; i++,before=before->next) { goon=(BEAD*)malloc(sizeof(BEAD)); goon->color=getchar(); goon->next=first; goon->prior=before; before->next=goon; first->prior=goon; } for (i=0;i<=n; point=point->next,i++) { if (point->color!='w') color_p=point->color; if ((point->next->color=='w')?0:(point->next->color!=color_p)) { flag=0; check2=check1=point; for (i_p=0; (check1->color=='w')?1:check1->color==color_p; check1=check1->prior) { i_p++; } for (i_n=0; (check2->next->color=='w')?1:check2->next->color!=color_p; check2=check2->next) { i_n++; } num=i_p+i_n; num_b=(num_b>num)?num_b:num; } } free(first); if ((flag==1)||(num_b>=n)) { printf("%d\n",n); } else printf("%d\n",num_b); return 0;}这个题目不同算法写了总共有3次,第一次,是只在r和b相连时开始计数,这样明显不行,网教里的一个非隐藏案例提醒了我
第二次,我没多想,干脆碰见不同的就判断计数,毫无疑问的TLE了
第三次,问了下大神,采用了上面的方法,程序比较紧凑,运行消耗时间也不大
不过,我在程序里多次使用了 ?: 运算符,应该要注意改进
- 数据结构——编程珠玑
- 《编程珠玑》学习笔记——第一章 位图数据结构
- 《编程珠玑》阅读小记(3) — 数据决定数据结构
- 编程珠玑——第一章
- 编程珠玑—代码优化
- 《编程珠玑》—习题提示
- 编程珠玑——排序算法
- 珠玑编程读书笔记——<一>
- 珠玑编程读书笔记——<二>
- 编程珠玑——变位词
- 位图排序——编程珠玑
- 抽样问题——《编程珠玑》读书笔记
- 蓄水池抽样——《编程珠玑》读书笔记
- 位图排序——《编程珠玑》笔记
- 编程珠玑——AVL树
- 编程珠玑——位图法
- 蓄水池抽样——《编程珠玑》读书笔记
- 编程珠玑——初体验:排序
- ODBC in Win7
- 你的脸在我的记忆里变得模糊不清:伤感日志
- 有关static的问题
- JDBC: Statement、PreparedStatement和CallableStatement
- Oracle与MySql函数
- 数据结构——编程珠玑
- opengl中的纹理映射
- Java多态性详解——父类引用子类对象
- 内存泄露检测工具
- 理解LVM《二》lvm 组件
- 常用的正则表达式 使用RegularExpressionValidator验证
- 【我害怕我心碎时没人帮我擦眼泪】
- 问题解决 The hierarchy of the type is inconsistent
- 一、Statement,PreparedStatement,CallableStatement,Batch,Transaction,ScrollResultSet,UpdateResultSet 例