【递归】Vijos P1114 FBI树(NOIP2004普及组第三题)
2016-04-18 17:39
363 查看
[b]题目链接:[/b]
https://vijos.org/p/1114
[b]题目大意:[/b]
把01串一分为二,左半边描述当前节点左子树,右半边描述右子树,子树全为1则为I节点,全为0则为B节点,混合则为F节点,直到当前串长度为1停止。
给定01串,求FBI树后序。
[b]题目思路:[/b]
【递归】
每次操作先操作左子树,再操作右子树,之后统计左右子树01状态,按照要求得到当前节点是 F B I中的哪一个。
由于输出后序,所以可以每次操作完左右子树后直接输出该节点,当前串长度为1则输出完返回。
View Code
https://vijos.org/p/1114
[b]题目大意:[/b]
把01串一分为二,左半边描述当前节点左子树,右半边描述右子树,子树全为1则为I节点,全为0则为B节点,混合则为F节点,直到当前串长度为1停止。
给定01串,求FBI树后序。
[b]题目思路:[/b]
【递归】
每次操作先操作左子树,再操作右子树,之后统计左右子树01状态,按照要求得到当前节点是 F B I中的哪一个。
由于输出后序,所以可以每次操作完左右子树后直接输出该节点,当前串长度为1则输出完返回。
// //by coolxxx // #include<iostream> #include<algorithm> #include<string> #include<iomanip> #include<memory.h> #include<time.h> #include<stdio.h> #include<stdlib.h> #include<string.h> #include<stdbool.h> #include<math.h> #define min(a,b) ((a)<(b)?(a):(b)) #define max(a,b) ((a)>(b)?(a):(b)) #define abs(a) ((a)>0?(a):(-(a))) #define lowbit(a) (a&(-a)) #define sqr(a) ((a)*(a)) #define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b)) #define eps 1e-8 #define J 10000 #define MAX 0x7f7f7f7f #define PI 3.1415926535897 #define N 1504 using namespace std; int n,m,lll,ans,cas; int e[]={1,2,4,8,16,32,64,128,256,512,1024}; char s ; char p[]={'B','I','F'}; int work(int l,int r) { int ll,rr,mid=(l+r)>>1; if(l==r) { printf("%c",p[s[l]=='1']); return (s[l]=='1'); } ll=work(l,mid); rr=work(mid+1,r); if(ll==rr) { printf("%c",p[ll]); return ll; } else { printf("%c",p[2]); return 2; } } int main() { #ifndef ONLINE_JUDGE // freopen("1.txt","r",stdin); // freopen("2.txt","w",stdout); #endif int i,j,k; // while(~scanf("%s",s1)) while(~scanf("%d",&n)) { scanf("%s",s); work(0,e -1); puts(""); } return 0; } /* // // */
View Code
相关文章推荐
- SharedPreference
- 防止app闪白屏或闪黑屏
- JSON数据的组装与解析
- 剑指offer 学习网站
- 防止测试人员疯狂点击按钮之后找你谈话
- iOS之Cookie
- “10倍效率”->好程序员
- [ERROR] No compiler is provided in this environment. Perhaps you are running on a JRE rather than a
- Ubuntu 12.04 安装谷歌拼音输入法
- 调用notifyDataSetChanged之后listview不刷新
- 数组排序
- 干货集合
- ICTCLAS中文分词库的使用
- UltraISO制作启动盘及提取U盘为ISO镜像
- Linux_查看linux并发连接数
- 【招聘】人才通缉令~~
- 【资源】科技公司钟爱的50款开源工具
- 【干货】PHP 代码规范简洁之道
- C语言编写获取Linux本地目录及本机信息的小程序实例
- 判断点是否在三角形内