当前位置:网站首页>P1087 [noip2004 popularity group question 3] FBI tree
P1087 [noip2004 popularity group question 3] FBI tree
2022-07-20 12:29:00 【hnjzsyjyj】
Title source
https://www.luogu.com.cn/problem/P1087
https://www.acwing.com/problem/content/description/421/
Title Description
We can turn the “ 0 0 0” and “ 1 1 1” There are three types of strings : whole “ 0 0 0” The string is called B B B strand , whole “ 1 1 1” The string is called I I I strand , Both contain “ 0 0 0” It also includes “ 1 1 1” The string of is called F F F strand .
F B I FBI FBI A tree is a binary tree , Its node types also include F F F node , B B B Node sum I I I There are three kinds of nodes . By a length of 2 N 2^N 2N Of “ 01 01 01” strand S You can build a tree F B I FBI FBI Trees T T T, The construction of recursion is as follows :
- T T T The root node of is R R R, Its type and string S S S Same type of ;
- Ruoshan S S S Is longer than 1 1 1, String S S S Separate from the middle , It is divided into equal length left and right substrings S 1 S_1 S1 and S 2 S_2 S2; From left sub string S 1 S_1 S1 structure R The left subtree T 1 T_1 T1, From the right sub string S 2 S_2 S2 structure R R R The right subtree T 2 T_2 T2.
Now let's give a length of 2 N 2^N 2N Of “ 01 01 01” strand , Please construct a F B I FBI FBI Trees , And output its post order traversal sequence .
Input format
The first line is an integer N ( 0 ≤ N ≤ 10 ) N(0 \le N \le 10) N(0≤N≤10),
The second line is a length of 2 N 2^N 2N Of “ 01 01 01” strand .
Output format
A string , namely F B I FBI FBI The subsequent traversal sequence of the tree .
The sample input #1
3
10001011
Sample output #1
IBFBBBFIBFIIIFF
Tips
about 40% The data of , N ≤ 2 N \le 2 N≤2;
For all the data , N ≤ 10 N \le 10 N≤10.
Algorithm code 1
Algorithm code 1 comes from :https://www.acwing.com/solution/content/5497/
#include <bits/stdc++.h>
using namespace std;
void dfs(string str) {
if(str.size()>1) {
dfs(str.substr(0,str.size()/2));
dfs(str.substr(str.size()/2));
}
int one=0, zero=0;
for(int i=0; i<str.size(); i++)
if(str[i]=='0') zero++;
else one++;
if(one && zero) cout<<'F';
else if(one) cout<<'I';
else cout<<'B';
}
int main() {
int n;
string str;
cin>>n>>str;
dfs(str);
return 0;
}
/* in: 3 10001011 out: IBFBBBFIBFIIIFF */
In order to understand this code , Need to understand functions in depth substr() Usage of . As shown below .
#include <bits/stdc++.h>
using namespace std;
int main() {
string s="abcdef";
string a=s.substr(0,s.size()/2); // From the subscript 0 Start to output s.size()/2 Characters
string b=s.substr(s.size()/2); // From the subscript s.size()/2 Start outputting all characters
string c=s.substr(2,s.size()/2); // From the subscript 2 Start to output s.size()/2 Characters
cout<<s.size()/2<<endl; //3
cout<<a<<" "<<b<<endl; //abc def
cout<<c<<" "<<b<<endl; //cde def
return 0;
}
Of course , If this question is changed to require the output of medium order F B I FBI FBI Trees , Then the code is slightly adjusted as follows :
#include <bits/stdc++.h>
using namespace std;
void dfs(string str) {
if(str.size()>1) {
dfs(str.substr(0,str.size()/2));
}
int one=0, zero=0;
for(int i=0; i<str.size(); i++)
if(str[i]=='0') zero++;
else one++;
if(one && zero) cout<<'F';
else if(one) cout<<'I';
else cout<<'B';
if(str.size()>1) {
dfs(str.substr(str.size()/2));
}
}
int main() {
int n;
string str;
cin>>n>>str;
dfs(str);
return 0;
}
/* in: 3 10001011 out: IFBFBBBFIFBFIII */
Accordingly , If this question is changed to require the output of the first order F B I FBI FBI Trees , Then the code is slightly adjusted as follows :
#include <bits/stdc++.h>
using namespace std;
void dfs(string str) {
int one=0, zero=0;
for(int i=0; i<str.size(); i++)
if(str[i]=='0') zero++;
else one++;
if(one && zero) cout<<'F';
else if(one) cout<<'I';
else cout<<'B';
if(str.size()>1) {
dfs(str.substr(0,str.size()/2));
dfs(str.substr(str.size()/2));
}
}
int main() {
int n;
string str;
cin>>n>>str;
dfs(str);
return 0;
}
/* in: 3 10001011 out: FFFIBBBBFFIBIII */
Algorithm code 2
#include <bits/stdc++.h>
using namespace std;
const int maxn=1050;
char s[maxn];
int n;
void create(int x,int y) {
if(y>x) {
create(x,(x+y)/2);
create((x+y+1)/2,y);
}
int B=1,I=1;
for(int i=0; i<=y-x; i++) {
if(s[x+i]=='1')
B=0;
else if(s[x+i]=='0')
I=0;
}
if(B)
cout<<'B';
else if(I)
cout<<'I';
else
cout<<'F';
}
int main() {
cin>>n>>s;
create(0,(1<<n)-1);
return 0;
}
/* in: 3 10001011 out: IBFBBBFIBFIIIFF */
reference
https://blog.csdn.net/weixin_44170305/article/details/109568533
https://www.acwing.com/solution/content/5497/
边栏推荐
- DeFi 2.0的LaaS协议Elephant,重振DeFi赛道发展的关键
- 【延期至12月】2022年网络安全国际研讨会(CSW2022)
- kvm部署及应用
- 高斯数学——看动画学奥数
- 论文阅读:Rethinking Atrous Convolution for Semantic Image Segmentation
- Xiaobai learns MySQL - generated columns function
- Opencv (12): cv:: rectangle learning and code demonstration, using OpenCV to draw rectangles / rectangular boxes
- 广汽埃安新一轮引战增资正式预挂牌,资金将重点用于新产品开发、新一代电池等方面
- Rancher安装部署及基本使用
- What is the gold content of PMP certificate? Is it worth taking the exam?
猜你喜欢
Solution to the first game of 2022 Niuke multi school league
Xiaobai learns MySQL - generated columns function
NFT 游戏互操作性:技术不是拦路虎
What network models does neural network include? Basic principles of neural network model
【微信小程序】断点调试一
Data analysis children's shoes can't be missed | operational risk control business analysis report
CAN的过滤器设置
[nightsafe AI] create the NFT digital art collection you want in one minute
出于对员工人身安全的担忧 星巴克未来可能关闭更多美国门店
【无标题】
随机推荐
第十五篇,STM32的SPI串行通信协议
Ipv6-icmpv6 protocol
Chrome基础
Yunna FSU dynamic loop monitoring unit, what is FSU dynamic loop monitoring unit
数据库优化方式和问题汇总篇
flink读取的mysql数据,出现重复消费,需要怎么处理
广州证券开户安不安全?
新闻速递 | 恭喜肖晓容工程师获得Domo专业认证!
A good product needs "emotional value"
Waiting for cache lock: Could not get lock /var/lib/dpkg/lock-frontend. It is held by process xxxx
Software of synonymous sentence conversion online translator
美女直播首用LDR6028无线麦克风音质传输OTG充电持续输出
23. Network principle - key protocols in tcp/ip four layer model (2)
微服务--熔断和限流
The Permutation Results by backtracking method (dfs)
PyQt5学习资源准备与环境配置
Okaleido或杀出NFT重围,你看好它吗?
【虹科新闻】虹科电子与WEKA正式建立合作伙伴关系
Style attribute operation of DOM series
Spec206 detailed parameters and summary of common problems in the test process (with example operation)