/ Vijos / 题库 / FBI树 /

题解

157 条题解

  • 0
    @ 2014-08-10 11:50:40

    var
    s:ansistring;
    p,t,i,j,k,m,n:longint;
    a:array[1..3000] of char;
    ch:char;
    procedure f(t:longint);
    begin
    if t>=p then
    begin
    write(a[t]);
    exit;
    end;
    f(t*2);
    f(t*2+1);
    write(a[t]);
    end;
    begin
    readln(n);
    p:=2;
    if n=0 then
    begin
    read(ch);
    if ch='1' then write('I') else write('B');
    halt;
    end;
    for i:=1 to n-1 do
    p:=p*2;
    for i:=1 to p do
    begin
    read(ch);
    if ch='1' then
    a[i+p-1]:='I' else
    a[i+p-1]:='B';
    end;
    a[1]:=' ';
    k:=p;
    while a[1]=' ' do
    begin
    i:=k;
    while i<2*k do
    begin
    if (a[i]='I') and (a[i+1]='I') then
    a[i div 2]:='I' else
    if (a[i]='B') and (a[i+1]='B') then
    a[i div 2]:='B' else
    a[i div 2]:='F';
    i:=i+2;
    end;
    k:=k div 2;
    end;
    f(1);
    end.
    15ms...刚开始数组开小了。。。。。。。。唉、。。。。我的AC率。。。

  • 0
    @ 2014-05-18 00:12:23

    #include<cstdio>
    bool S[1025]={false};
    char tmp;
    int n,tbl[11]={1,2,4,8,16,32,64,128,256,512,1024};
    char dfs(int s,int e){
    if(s==e){
    if(S[s]){printf("I");return 'I';}
    else{printf("B");return 'B';}
    }else{
    char s1=dfs(s,(s+e)/2),s2=dfs((s+e)/2+1,e);
    if(s1=='F'||s2=='F'){printf("F");return 'F';}
    else if(s1=='B'&&s2=='B'){printf("B");return 'B';}
    else if(s1=='I'&&s2=='I'){printf("I");return 'I';}
    else{printf("F");return 'F';}
    }
    }
    int main(){
    //freopen("P1114.in","r",stdin);
    //freopen("P1114.out","w",stdout);
    scanf("%d\n",&n);
    n=tbl[n];
    for(int i=0;i<n;i++){
    scanf("%c",&tmp);
    if(tmp=='0')S[i]=false;
    else S[i]=true;
    }
    dfs(0,n-1);
    printf("\n");
    }

  • 0
    @ 2014-03-08 20:04:02

    #构造一棵二叉树(确切地说是满二叉树),后序遍历它

    C:

    #include <stdio.h>
    #include <string.h>
    #define MAXL 0x400/**最多1024位**/

    void main(){
    char s[MAXL];
    int n;
    void work(char b[]);

    scanf("%d", &n);
    scanf("%s", s);
    work(s);/**分解字符串 s **/
    }

    void work(char base[])
    {
    int l;
    char left[MAXL], right[MAXL];
    char call(char a[]);/**判断输出‘F’ or 'B' or 'I' /
    void stringcopy(char a[], char b[], int i, int j);/
    把字符串b的i~j位复制到a上**/

    l = strlen(base);
    if(l > 1){
    stringcopy(base, left, 0, l/2);/**左孩子**/
    stringcopy(base, right, l/2, l);/**右孩子**/
    work(left);/**递归左孩子**/
    work(right);/**递归右孩子**/
    }
    printf("%c", call(base));
    }

    char call(char base[])
    {
    int i, l;
    l = strlen(base);
    for(i = 0; i < l-1; i++)
    if(base[i] != base[i+1]) return 'F';
    if(base[i] == '0') return 'B';
    if(base[i] == '1') return 'I';
    }

    void stringcopy(char base[], char get[], int st, int ed)
    {
    int i = st;
    for(;i < ed; i++)
    get[i-st] = base[i];
    get[ed-st] = '\0';/**插入空字符,表示字符串的结束**/
    }

  • 0
    @ 2013-11-02 19:49:28

    var a:array[1..100000] of boolean;
    n,m,i:longint;
    b:char;
    function aa(x,y:longint):longint;
    var i,j,k,l,q,w,e:longint;
    begin
    if x=y then
    begin
    if a[x] then k:=1 else k:=0;
    if k=1 then write('I') else write('B');
    exit(k);
    end;
    k:=aa(x,(x+y) div 2);
    l:=aa((x+y) div 2+1,y);
    if (k=l)and(k=1) then begin e:=1;write('I');end else
    if (k=l)and(k=0) then begin e:=0;write('B');end else
    begin e:=2;write('F');end;
    exit(e);
    end;
    begin
    readln(n);m:=1;
    for i:=1 to n do m:=m*2;n:=m;
    for i:=1 to n do begin read(b);if b='1' then a[i]:=true else a[i]:=false;end;
    i:=aa(1,n);
    writeln;
    end.

    15ms
    随随便便后序遍历

  • 0
    @ 2013-07-27 15:18:02

    测试数据 #0: Accepted, time = 0 ms, mem = 440 KiB, score = 10

    测试数据 #1: Accepted, time = 0 ms, mem = 440 KiB, score = 10

    测试数据 #2: Accepted, time = 0 ms, mem = 440 KiB, score = 10

    测试数据 #3: Accepted, time = 0 ms, mem = 436 KiB, score = 10

    测试数据 #4: Accepted, time = 0 ms, mem = 436 KiB, score = 10

    测试数据 #5: Accepted, time = 0 ms, mem = 436 KiB, score = 10

    测试数据 #6: Accepted, time = 0 ms, mem = 436 KiB, score = 10

    测试数据 #7: Accepted, time = 0 ms, mem = 436 KiB, score = 10

    测试数据 #8: Accepted, time = 0 ms, mem = 432 KiB, score = 10

    测试数据 #9: Accepted, time = 0 ms, mem = 440 KiB, score = 10

    #include <stdio.h>
    char fbi[3000];
    int tep=0;
    void set(char *s,int l,int r)
    {
    int i,flag=0;
    int mid=(l+r)>>1;
    if(l==r)
    {
    if(s[l]=='0')fbi[tep++]='B';
    if(s[l]=='1')fbi[tep++]='I';
    return ;
    }
    set(s,l,mid);
    set(s,mid+1,r);
    if(s[l]=='0')fbi[tep]='B';
    if(s[l]=='1')fbi[tep]='I';
    for(i=l+1;i<=r;i++)
    {
    if(s[l]!=s[i])flag=1;
    }
    if(flag)fbi[tep]='F';
    tep++;
    }
    int main()
    {
    char s[2000];
    int a,n,m,tep;
    scanf("%d",&a);
    n=1<<a;
    scanf("%s",s);
    set(s,0,n-1);
    m=n*2-1;
    fbi[m]='\0';
    printf("%s",fbi);
    return 0;
    }

  • 0
    @ 2013-05-27 14:02:02

    15MS呢。。。那些人怎么做到100多ms的。。
    测试数据 #0: Accepted, time = 15 ms, mem = 548 KiB, score = 10
    测试数据 #1: Accepted, time = 0 ms, mem = 468 KiB, score = 10
    测试数据 #2: Accepted, time = 0 ms, mem = 464 KiB, score = 10
    测试数据 #3: Accepted, time = 0 ms, mem = 464 KiB, score = 10
    测试数据 #4: Accepted, time = 0 ms, mem = 464 KiB, score = 10
    测试数据 #5: Accepted, time = 0 ms, mem = 464 KiB, score = 10
    测试数据 #6: Accepted, time = 0 ms, mem = 468 KiB, score = 10
    测试数据 #7: Accepted, time = 0 ms, mem = 468 KiB, score = 10
    测试数据 #8: Accepted, time = 0 ms, mem = 472 KiB, score = 10
    测试数据 #9: Accepted, time = 0 ms, mem = 508 KiB, score = 10
    Accepted, time = 15 ms, mem = 548 KiB, score = 100

    #include <iostream>
    #include <cmath>
    #include <string>
    using namespace std;

    struct TreeNode {
    char type;
    TreeNode *l, *r;
    } *root;

    char FBI[1030];

    int n;

    string substring(const string &str, int l, int r) {
    return str.substr(l, r-l+1);
    }

    void create(TreeNode *&root, int l, int r) {
    root = new TreeNode;
    if(r-l+1==1) {
    root->l = root->r = NULL;
    if(FBI[l]=='1') {
    root->type = 'I';
    } else root->type = 'B';
    }
    else {
    int m = (l+r)/2;
    create(root->l, l, m);
    create(root->r, m+1, r);
    if(root->l->type==root->r->type&&root->l->type!='F') {
    root->type = root->l->type;
    } else root->type = 'F';
    }
    }

    void last_order(TreeNode *r) {
    if(!r) return;
    last_order(r->l);
    last_order(r->r);
    cout<<r->type;
    }

    int main() {
    cin>>n;
    cin>>FBI + 1;
    n = pow(2, n);
    create(root, 1, n);
    last_order(root);
    return 0;
    }

  • 0
    @ 2013-03-02 17:08:50

    辛苦写完的程序第一次测评时竟被测评机忽略了………………
    包子,你的代码的变量名称也太…………有内涵了,表示正常人看不懂啊
    给个改良版
    测试数据 #0: Accepted, time = 78 ms, mem = 780 KiB, score = 10

    测试数据 #1: Accepted, time = 62 ms, mem = 520 KiB, score = 10

    测试数据 #2: Accepted, time = 46 ms, mem = 520 KiB, score = 10

    测试数据 #3: Accepted, time = 62 ms, mem = 520 KiB, score = 10

    测试数据 #4: Accepted, time = 62 ms, mem = 516 KiB, score = 10

    测试数据 #5: Accepted, time = 15 ms, mem = 520 KiB, score = 10

    测试数据 #6: Accepted, time = 15 ms, mem = 520 KiB, score = 10

    测试数据 #7: Accepted, time = 15 ms, mem = 520 KiB, score = 10

    测试数据 #8: Accepted, time = 31 ms, mem = 520 KiB, score = 10

    测试数据 #9: Accepted, time = 46 ms, mem = 784 KiB, score = 10

    Summary: Accepted, time = 432 ms, mem = 784 KiB, score = 100
    代码

    program FBI;
    type
    rec=record
    data:char;
    lch,rch,p:integer;
    end;
    var
    n,sum,i,j:integer;
    f:array[1..5000]of rec;
    s:ansistring;
    a:array[1..5000]of integer;
    function word(x,y:integer):char;
    begin
    word:='F';
    if (f[x].data='B')and(f[y].data='B') then
    word:='B'
    else if (f[x].data='I')and(f[y].data='I') then
    word:='I';
    end;
    procedure build1(i:integer);
    begin
    if (i>sum) then
    exit
    else begin
    f[i].p:=i div 2;
    f[i].lch:=2*i;
    f[i].rch:=2*i+1;
    inc(i);
    build1(i);
    end;
    end;
    procedure behind(i:integer);
    begin
    if f[i].data='#' then
    exit
    else begin
    if (f[i].lch>0)and(f[i].lch<2*sum) then
    behind(f[i].lch);
    if (f[i].rch>0)and(f[i].rch<2*sum) then
    behind(f[i].rch);
    write(f[i].data);
    end;
    end;
    begin
    readln(n);
    sum:=1;
    for i:=1 to n do
    sum:=sum*2;
    read(s);
    build1(1);
    for i:=1 to sum do
    a[i]:=ord(s[i])-ord('0');
    for i:=1 to 2*sum+1 do
    f[i].data:='#';
    for i:=sum to 2*sum-1 do
    begin
    f[i].data:='F';
    if a[i-sum+1]=0 then
    f[i].data:='B'
    else if a[i-sum+1]=1 then
    f[i].data:='I';
    end;
    for i:=sum-1 downto 1 do
    f[i].data:=word(f[i].lch,f[i].rch);
    behind(1);
    end.

  • 0
    @ 2013-03-02 14:54:19

    106ms无压力秒杀~
    type nn=record
    f,l,r:longint;
    n:char;
    end;

    var
    i,n,t,l,top:longint;
    s:ansistring;
    a:array[1..10000]of longint;
    f:array[1..10000]of nn;

    procedure lala(i:longint);
    begin
    f[i].f:=i div 2;
    f[i].l:=i*2;
    f[i].r:=i*2+1;
    i:=i+1;
    if i>l then exit;
    lala(i);
    end;

    procedure ee(i:longint);
    begin
    if (f[i].l<>0)and(f[i].l<l*2) then ee(f[i].l);
    if (f[i].r<>0)and(f[i].r<l*2) then ee(f[i].r);
    write(f[i].n);
    end;

    Begin
    readln(n);
    l:=1;
    for i:=1 to n do l:=l*2;
    readln(s);
    for i:=1 to l do a[i]:=ord(s[i])-ord('0');
    top:=2*l-1;
    lala(1);
    for i:=l to top do if a[i-l+1]=0 then f[i].n:='B' else f[i].n:='I';
    for i:=l-1 downto 1 do begin
    f[i].n:='F';
    if (f[f[i].l].n='I') and (f[f[i].r].n='I') then f[i].n:='I';
    if (f[f[i].l].n='B') and (f[f[i].r].n='B') then f[i].n:='B';
    end;
    ee(1);
    End.

  • 0
    @ 2012-10-18 21:01:49

    ├ 测试数据 01:答案正确... (0ms, 1032KB)

    ├ 测试数据 02:答案正确... (0ms, 664KB)

    ├ 测试数据 03:答案正确... (0ms, 628KB)

    ├ 测试数据 04:答案正确... (0ms, 664KB)

    ├ 测试数据 05:答案正确... (0ms, 664KB)

    ├ 测试数据 06:答案正确... (0ms, 664KB)

    ├ 测试数据 07:答案正确... (0ms, 664KB)

    ├ 测试数据 08:答案正确... (0ms, 664KB)

    ├ 测试数据 09:答案正确... (0ms, 664KB)

    ├ 测试数据 10:答案正确... (0ms, 1032KB)

    ---|---|---|---|---|---|---|---|-

    Accepted / 100 / 0ms / 1032KB

    思路:按题目要求建造满二叉树tree,再后序遍历输出

    {

    ID:darkgod-z

    PROG:vijos P1114

    HANG:PASCAL

    }

    var

    s:ansistring;

    n:integer;

    tree:array[1..2047] of record

    data:char;

    l,r:integer;

    end;

    procedure lrd(x:integer);

    begin

    if x0 then begin

    lrd(tree[x].l);

    lrd(tree[x].r);

    write(tree[x].data);

    end;

    end;

    procedure gettree(s:ansistring; i:integer);

    var

    s1,s2:ansistring;

    t,j:integer;

    begin

    if (pos('0',s)>0)and(pos('1',s)>0) then tree[i].data:='F'

    else

    if pos('0',s)>0 then tree[i].data:='B'

    else tree[i].data:='I';

    if length(s)=1 then exit;

    t:=length(s) div 2;

    for j:=1 to t do s1:=s1+s[j];

    for j:=t+1 to length(s) do s2:=s2+s[j];

    if i*2

  • 0
    @ 2012-09-28 14:07:10

    Ansistring

    Ansistring

    Ansistring

    Ansistring

    Ansistring

    Ansistring

    Ansistring

    Ansistring

    Ansistring

    Ansistring

  • 0
    @ 2012-08-19 10:45:13

    第一次做到了一次AC,祝贺一下,用的递归

    #include

    int cifang[12]={1,2,4,8,16,32,64,128,256,512,1024,2048};

    int panduan(char* s,int n)

    {

    int i;

    char c=s[0];

    int flag=1;

    for(i=1;i

  • 0
    @ 2012-08-14 17:03:48

    ├ 测试数据 01:运行时错误... (83ms, 1032KB)

    读取访问违规, 地址: 0x0000d7aa

    ├ 测试数据 02:答案正确... (64ms, 700KB)

    ├ 测试数据 03:答案正确... (40ms, 628KB)

    ├ 测试数据 04:答案正确... (118ms, 664KB)

    ├ 测试数据 05:答案正确... (142ms, 664KB)

    ├ 测试数据 06:答案正确... (103ms, 664KB)

    ├ 测试数据 07:答案正确... (173ms, 700KB)

    ├ 测试数据 08:答案正确... (169ms, 700KB)

    ├ 测试数据 09:答案正确... (173ms, 700KB)

    ├ 测试数据 10:答案正确... (185ms, 996KB)

    ————————————

    谁能告诉我第一个点是怎么个思想?

    ————已经解决了 是数组开小了 2的10次方是1024 我开1025 挂了, 开3000 ac!

  • 0
    @ 2012-08-12 16:10:50

    求后序遍历符合递归的规律(都是树的结构),

    十分同意楼下所说的,关键在于理解题意

    做法:

    将题目所给的树不断按中间将树切开两半,拼命切,

    直到只有一个结点为止,然后返回到自己的父亲,拼命返回,直到根结点为止

    用递归实现

  • 0
    @ 2010-07-24 22:03:02

    关键在于理解题意。。

  • 0
    @ 2010-07-17 12:26:07

    var

    f:array[1..2000] of char;

    n,i,j,k,tt:integer;

    procedure search(s,t:integer);

    var

    tot1,tot0:integer;

    p:char;

    begin

    if t-s+11 then

    begin

    tot1:=0; tot0:=0;

    for i:=s to t do

    if f[i]='1' then inc(tot1) else inc(tot0);

    if tot1=0 then p:='B' else

    if tot0=0 then p:='I' else

    p:='F';

    search(s,(t-s+1) div 2+s-1);

    search((t-s+1) div 2+s,t);

    write(p);

    end;

    end;

    begin

    readln(n);

    tt:=1;

    for i:=1 to n do tt:=tt*2;

    for i:=1 to tt do read(f[i]);

    search(1,tt);

    writeln;

    end.

  • 0
    @ 2010-07-01 19:45:03

    我的代码应该比较简洁,清一色的分治。

    #include

    using namespace std;

    int n;

    char a[1025];

    void init(void)

    {

    cin>>n>>a;

    return;

    }

    int fbi(int i,int j)

    {

    if(i0)cout

  • 0
    @ 2009-11-04 19:45:23

    晕 建造树居然忘了 New(p)!!!

  • 0
    @ 2009-10-31 14:57:18

    program fbi;

    const

    fbii:array[0..2]of char=('B','I','F');

    var

    n,i,t:longint;

    shu:ansistring;

    shui:array[1..1024]of longint;

    function tree(l,r:longint):longint;

    var x,y:longint;

    begin

    if l=r then

    begin

    if shui[l]=1 then begin tree:=1;write(fbii[1]);end;

    if shui[l]=0 then begin tree:=0;write(fbii[0]);end;

    end else

    begin

    x:=tree(l,(l+r) div 2);

    y:=tree((l+r) div 2+1,r);

    if x+y=0 then begin tree:=0;write(fbii[0]);end

    else

    if x*y=1 then begin tree:=1;write(fbii[1]);end

    else begin tree:=2;write(fbii[2]);end

    end;

    end;

    begin

    readln(n);

    readln(shu);

    t:=length(shu);

    for i:=1 to t do

    shui[i]:=ord(shu[i])-48;

    tree(1,t);

    end.

  • 0
    @ 2009-10-30 21:36:12

    好水好喝!

  • 0
    @ 2009-10-27 15:58:58

    WATER...

    题目让我终于看懂了。。。

    编译通过...

    ├ 测试数据 01:答案正确... 0ms

    ├ 测试数据 02:答案正确... 0ms

    ├ 测试数据 03:答案正确... 0ms

    ├ 测试数据 04:答案正确... 0ms

    ├ 测试数据 05:答案正确... 0ms

    ├ 测试数据 06:答案正确... 0ms

    ├ 测试数据 07:答案正确... 0ms

    ├ 测试数据 08:答案正确... 0ms

    ├ 测试数据 09:答案正确... 0ms

    ├ 测试数据 10:答案正确... 0ms

    ---|---|---|---|---|---|---|---|-

    Accepted 有效得分:100 有效耗时:0ms

    program ex;

    var i,j,n:longint;

    st:ansistring;

    procedure get(l,r:longint);

    var i,j:longint;

    begin

    if r-l>=1 then

    begin

    get(l,(l+r)div 2);

    get((l+r)div 2+1,r);

    end;

    if pos('0',copy(st,l,r-l+1))=0 then

    write('I')

    else

    if pos('1',copy(st,l,r-l+1))=0 then

    write('B')

    else

    write('F');

    end;

    begin

    readln(n);

    readln(st);

    get(1,length(st));

    writeln;

    end.

信息

ID
1114
难度
3
分类
数据结构 | 点击显示
标签
递交数
4162
已通过
2223
通过率
53%
被复制
26
上传者