FBI树【题目链接】

感觉我超废。


MY SOLUTION:
     我的想法其实也是很简单的,递归的去做,因为最后要求输出FBI的后序遍历,也就是左右头,我的方法是递归存字符数组,(按照与后序遍历完全相反的顺序存的),然后倒序输出。非常遗憾的是,因为开始时写递归写炸了(微笑),于是我修改递归变成了main函数里先进行判整个串,当n=0时,就输出了两位。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; int n, cnt;
char a[];
char c[]; int pd(int x, int y) {//写了一个复杂的判断函数
if (x == y) {
if (a[x] == '') return ;
else return ;
} for (int i = x + ; i <= y; i++)
if (a[i] != a[i - ]) return ; if (a[x] == '') return ; if (a[x] == '') return ;
} int solve(int f, int len) {
int d = (len + f) >> ;
int num = len;
if (f > len)
return ;
if (pd(f, len) == )
c[++cnt] = 'F';
if (pd(f, len) == )
c[++cnt] = 'I';
if (pd(f, len) == )
c[++cnt] = 'B'; if (f == len)
return ;
len >>= ;
solve(d + , num);
solve(f, d);
return ;
} int main() {
scanf("%d", &n);
scanf("%s", a + );//输入 from a[1];
int len = strlen(a + );//求a[1] to a[len] 的长度;
if (pd(, len) == )
c[] = 'F';
if (pd(, len) == )
c[] = 'I';
if (pd(, len) == )
c[] = 'B';
cnt++;
int g = len >> ;
solve(g + , len);//因为后序遍历,故先递归右子树
solve(, g);
if (n == ) {//被坑的地方,因为n==0时,如果按我的递归方式cnt=2;
cout << c[] << endl;
return ;
}
for (int i = cnt; i >= ; i--) cout << c[i];
return ;
}

我想改一改我的这个代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; int n, cnt;
char a[];
char c[]; int pd(int x, int y) {
for (int i = x + ; i <= y; i++)
if (a[i] != a[i - ])
return ;
if (a[x] == '')
return ;
if (a[x] == '')
return ;
} int solve(int f, int len) {
if(f==len) {if(a[f]=='') c[++cnt]='I';
else c[++cnt]='B';
return ;}
int d = (len + f) >> ;
if(f>len) return ;
if (pd(f, len) == )
c[++cnt] = 'F';
if (pd(f, len) == )
c[++cnt] = 'I';
if (pd(f, len) == )
c[++cnt] = 'B';
solve(d+,len); solve(f,d);
return ;
} int main() {
scanf("%d", &n);
scanf("%s", a + );
int len = strlen(a + ); solve(, len); for (int i = cnt; i >= ; i--) cout << c[i];
return ;
}

WATER_LIFT'S SOLUTION:(是我手打的但思路是water_lift的)

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; int n, cnt;
char a[];
char c[]; int pd(int x, int y) {
if (x == y) {
if (a[x] == '') return ;
else return ;
} for (int i = x + ; i <= y; i++)
if (a[i] != a[i - ]) return ; if (a[x] == '') return ;
if (a[x] == '') return ;
} int solve(int f, int len) {
if(f==len) {
if(a[f]=='') cout<<"I";
if(a[f]=='') cout<<"B";
return ;
}
int d = (len + f) >> ;
solve(f,d);
solve(d+,len);
if (pd(f, len) == )
cout<<"F";
if (pd(f, len) == )
cout<<"I";
if (pd(f, len) == )
cout<<"B";
return ;
} int main() {
scanf("%d", &n);
scanf("%s", a + );
int len = strlen(a + ); solve(,len);
return ;
}

至于water_lift的非暴力算法,大家看看就好:

#include <iostream>
#include <string>
using namespace std;
int n;
string s;
char dfs(int l, int r)
{
if (l == r)
{
if (s[l] == '')
{
cout << 'B';
return 'B';
}
else if (s[l] == '')
{
cout << 'I';
return 'I';
}
}
int mid = (l + r) / ;
char le = dfs(l, mid);
char ri = dfs(mid + , r);
if (le == 'B' && ri == 'B')
{
cout << 'B';
return 'B';
}
if (le == 'I' && ri == 'I')
{
cout << 'I';
return 'I';
}
cout << 'F';
return 'F';
}
int main()
{
freopen("fbi.in", "r", stdin);
freopen("fbi.out", "w", stdout);
cin >> n >> s;
dfs(, ( << n) - );
cout << endl;
}

end-

【6.10校内test】T1 FBI树的更多相关文章

  1. Vijos 1114 FBI树

    描述 我们可以把由"0"和"1"组成的字符串分为三类:全"0"串称为B串,全"1"串称为I串,既含"0&quo ...

  2. 蓝桥杯之FBI树问题

    问题描述 我们可以把由"0"和"1"组成的字符串分为三类:全"0"串称为B串,全"1"串称为I串,既含"0&q ...

  3. noip普及组2004 FBI树

    FBI树 描述 我们可以把由"0"和"1"组成的字符串分为三类:全"0"串称为B串,全"1"串称为I串,既含" ...

  4. Vijos P1114 FBI树【DFS模拟,二叉树入门】

    描述 我们可以把由“0”和“1”组成的字符串分为三类:全“0”串称为B串,全“1”串称为I串,既含“0”又含“1”的串则称为F串. FBI树是一种二叉树1,它的结点类型也包括F结点,B结点和I结点三种 ...

  5. [题解]ybt1365:FBI树(fbi)

    ybt1365:FBI树(fbi) [题目描述] 我们可以把由“0”和“1”组成的字符串分为三类:全“0”串称为B串,全“1”串称为I串,既含“0”又含“1”的串则称为F串. FBI树是一种二叉树,它 ...

  6. FBI树-数据结构(二叉树)

    问题 B: [2004_p4]FBI树-数据结构 时间限制: 1 Sec  内存限制: 125 MB提交: 57  解决: 46 题目描述 我们可以把由“0”和“1”组成的字符串分为三类:全“0”串称 ...

  7. C语言 · FBI树

    算法训练 FBI树   时间限制:1.0s   内存限制:256.0MB        锦囊1 二叉树. 问题描述 我们可以把由“0”和“1”组成的字符串分为三类:全“0”串称为B串,全“1”串称为I ...

  8. FBI树(第一次做建树题)

    试题来源 NOIP2004 普及组 问题描述 我们可以把由“0”和“1”组成的字符串分为三类:全“0”串称为B串,全“1”串称为I串,既含“0”又含“1”的串则称为F串. FBI树是一种二叉树,它的结 ...

  9. code vs 1094 FBI树 2004年NOIP全国联赛普及组

    题目描述 Description 我们可以把由“0”和“1”组成的字符串分为三类:全“0”串称为B串,全“1”串称为I串,既含“0”又含“1”的串则称为F串. FBI树是一种二叉树[1],它的结点类型 ...

随机推荐

  1. vue3之组件

    目录 组件 根组件 局部组件 全局组件 组件的注册 组件名 组件名大小写 全局注册 局部注册 模块系统 组件注册实例: 组件化 组件间数据的传递 父组件传递数据给子组件 父组件传递数据给子组件例子 子 ...

  2. Quantitative Strategies for Achieving Alpha (三)

    chapter 4: Profitability Profitability measures we tested include return on invested capital, return ...

  3. postman—创建collection,执行collection和批量执行

    接口测试中,可以在 Postman 逐个创建请求.但当请求逐渐增多时,如果我们不采取任何措施管理,散乱的请求维护起来就比较麻烦了.这个时候我们可以创建测试集 Collection 来对这些请求进行管理 ...

  4. 更改pip源地址为阿里云

    1.在用户名目录创建pip目录,在pip目录下创建pip.ini. 2.pip.ini中输入: [global] index-url = http://mirrors.aliyun.com/pypi/ ...

  5. POI操作Excel(批量导出数据/下载excel)

    目录 1.第一个demo:创建工作簿,创建sheet页,创建单元格    2.创建一个时间格式的单元格    3.遍历工作簿的行和列并获取单元格内容    4.文本提取    5.单元格对齐方式    ...

  6. ASP.NET MVC Ajax下载文件(使用NPOI向现有的excel模板文件里面添加数据)

    View Html.DevExpress().Button(DevExpressButtonHelper.AddButton(ViewBag.Form, "Export", &qu ...

  7. 使用ros_driver运行velodyne16线激光雷达

    一.使用ros_driver运行VLP16 推荐网址: http://blog.csdn.net/littlethunder/article/details/51920681 https://www. ...

  8. 大数据笔记(二十六)——Scala语言的高级特性

    ===================== Scala语言的高级特性 ========================一.Scala的集合 1.可变集合mutable 不可变集合immutable / ...

  9. eigen 四元数进行坐标旋转

    (<视觉SLAM十四讲>第三讲习题7)设有小萝卜一号和二号在世界坐标系中.一号位姿q1 = [0.35, 0.2, 0.3, 0.1],t1=[0.3, 0.1, 0.1].二号位姿q2= ...

  10. 在aspx页面的checkbox取值验证

    在做项目的时候遇到了一个选择性的问题,之前都可以用$("#id").checked,但是不知道为什么现在不可以了,只能if($(this).is(":checked&qu ...