• Eliminate Witches!【2011年北京赛区正赛赛题2】


    /*
    sample input
    3
    walpurgis(charlotte(patricia,gertrud),elly,gisela)
    wuzetian
    nanoha(fate(hayate))
    */
    /*
    DFS
    把树的字符串
    1
    a(b(c,d),e,f)
    进行深度优先搜索
    当前房间编号i
    遇到'('则i-》i+1
    遇到','则i-》pre_i   pre_i-》i+1
    遇到')'则i-》pre_i   如果pre_i!=1 pre_i-》pre_i' pre_i'-》pre_i+1
     */
    #include <stdio.h>
    #include <string.h>
    
    int T;//T<=20
    char str[1000002];//Madoka's log <=1000000chars
    char Witches[50001][11];//at most 50000 rooms
    int sum_witches,str_i,str_len,name_i,cur_i;//当前结点编号
    int pre_i[50001];//父结点编号
    int pass[100002][2];//路线
    int pass_i;
    int main()
    {
        char ch;
        int t,i;
        scanf("%d",&T);
        while (T--)
        {
            memset(pass,0,sizeof(pass));    
            pass_i=0;
            memset(Witches,0,sizeof(Witches));
            sum_witches=1;
            name_i=0;
            memset(pre_i,0,sizeof(pre_i));
            cur_i=1;
            pre_i[cur_i]=-1;
            memset(str,0,sizeof(str));        
            str_i=0;
            scanf("%s",str);
            str_len=strlen(str);        
            while (str_i<str_len)
            {
                ch=str[str_i++];
                switch(ch)
                {
                case '(':
                    sum_witches++;
                    name_i=0;
                    pre_i[sum_witches]=cur_i;
                    pass[pass_i][0]=cur_i;
                    pass[pass_i][1]=sum_witches;
                    pass_i++;
                    cur_i=sum_witches;
                    break;
                case ',':
                    t = pre_i[cur_i];
                    pass[pass_i][0]=cur_i;
                    pass[pass_i][1]=t;
                    pass_i++;
                    cur_i=t;
                    sum_witches++;
                    name_i=0;
                    pre_i[sum_witches]=cur_i;
                    pass[pass_i][0]=cur_i;
                    pass[pass_i][1]=sum_witches;
                    pass_i++;
                    cur_i=sum_witches;
                    break;
                case ')':
                    t=pre_i[cur_i];
                    pass[pass_i][0]=cur_i;
                    pass[pass_i][1]=t;
                    pass_i++;
                    cur_i=t;
                    break;
                default:
                        Witches[sum_witches][name_i++]=ch;
                        if(str[str_i]>'z' || str[str_i]<'a')
                            Witches[sum_witches][name_i]='\0';
                    break;
                }
            }
            printf("%d\n",sum_witches);
            for (i=1;i<sum_witches+1;i++)    printf("%s\n",Witches[i]);
            for (i=0;i<pass_i;i++)    printf("%d %d\n",pass[i][0],pass[i][1]);
            printf("\n");
        }
        return 1;
    }
    字节跳动内推

    找我内推: 字节跳动各种岗位
    作者: ZH奶酪(张贺)
    邮箱: cheesezh@qq.com
    出处: http://www.cnblogs.com/CheeseZH/
    * 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

  • 相关阅读:
    浅谈IIS处理Asp.net请求和 Asp.net页面生命周期
    Windows Phone 7与Android和iPhone的比较
    ExtJs开发需要引用的Js库文件
    Windows phone开发(1)一些基本概念
    synthesize
    Objective C 基础教程
    Objective C基础教程笔记
    ObjectiveC语法与Cocoa框架
    Handing basic Interactio IOS notes
    Learning ObjectiveC and Xcode
  • 原文地址:https://www.cnblogs.com/CheeseZH/p/2442144.html
Copyright © 2020-2023  润新知