zoukankan      html  css  js  c++  java
  • 展开字符串

    展开字符串

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 1959    Accepted Submission(s): 930


    Problem Description
    在纺织CAD系统开发过程中,经常会遇到纱线排列的问题。
    该 问题的描述是这样的:常用纱线的品种一般不会超过25种,所以分别可以用小写字母表示不同的纱线,例如:abc表示三根纱线的排列;重复可以用数字和括号 表示,例如:2(abc)表示abcabc;1(a)=1a表示a;2ab表示aab;如果括号前面没有表示重复的数字出现,则就可认为是1被省略了, 如:cd(abc)=cd1(abc)=cdabc;这种表示方法非常简单紧凑,也易于理解;但是计算机却不能理解。为了使计算机接受,就必须将简单紧凑 的表达方式展开。某ACM队接受了此项任务。现在你就是该ACM队的一员,请你把这个程序编写完成。
    已知条件:输入的简单紧凑表达方式的长度不超过250个字符;括号前表示重复的数不超过1000;不会出现除了数字、括号、小写字母以外的任何其他字符;不会出现括号不配对等错误的情况(错误处理已由ACM其他队员完成了)。
     
    Input
    本题有多个测试数据组,第一行输入的就是数据组数N,接着就是N行表达式,表达式是按照前面介绍的意义书写的。
     
    Output
    输出时含有N行,每行对应一个输入的表达式。
     
    Sample Input
    2
    1(1a2b1(ab)1c)
    3(ab2(4ab))
     
    Sample Output
    abbabc
    abaaaabaaaababaaaabaaaababaaaabaaaab
     
    Author
    Cai Minglun
     
    Source
     
    Recommend
    lcy
    比赛的时候队友说你能做出来,我也觉着我能做出来~~结果,我没做出来,因为我不知道是用dfs还是纯字符串处理,用df我又不知道s怎么写,纯字符串当时又不在状态。。。。。
     
    /*
    递归求解,适时返回结束位置的地址。
    当然也可以用栈来解决,一般这种括号问题都用栈来解决。*/
    #include <cmath>
    #include <string>
    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    #include <queue>
    #include <vector>
    #include <ctype.h>
    
    using namespace std;
    #define maxn 1200
    #define INF 0xfffffff
    
    char *dfs(char str[])
    {
    
        char *p;
    
        while(1)
        {
            int k = 0;
    
            while(isdigit(*str))
            {
                k = k * 10 + (*str-'0');
                str++;
            }
    
            if(k == 0)
                k++;
    
            if(*str == '(')
            {
                for(int i = 0; i < k; i++)
                    p = dfs(str+1);
    
                str = p;
            }
            else if(isalpha(*str))
            {
                for(int i = 0; i < k; i++)
                    printf("%c", *str);
                str++;
            }
            else
                return str+1;
        }
    }
    
    int main()
    {
        int n;
        char str[maxn];
    
        scanf("%d", &n);
    
        while(n--)
        {
            scanf("%s", str);
    
            dfs(str);
    
            puts("");
        }
        return 0;
    }
     纯字符串处理
    #include<stdio.h>
    #include<string.h>
    #define N 10100
    int main ()
    {
        int T, i, j, k, x, y, num, n, m;
        char str[N], s1[N], s2[N];
        scanf("%d", &T);
        while (T--)
        {
            k = 0;
            scanf("%s", str);
            for (i = 0; str[i] != ''; i++)
                if (str[i] == '(') k++;
            while (k--)
            {
                num = 0;
                n = 0;
                j = 0;
                for (i = 0; str[i] != ''; i++)
                {
                    if (str[i] == '(') x = i;
                    else if (str[i] == ')')
                    {
                        y = i;
                        break;
                    }
                    s1[j++] = str[i];
                }
                m = x-1;
                while (str[m] >= '0' && str[m] <= '9') m--;
                m++;
                j = m;
                s1[j] = '';
                for (i = x+1; i < y; i++) s2[n++] = str[i];
                s2[n] = '';
                for (i = m; i < x; i++) num = num * 10 + (str[i]-'0');
                if (num == 0) num = 1;
                j += (y-x-1)*num;
                while (num--) strcat(s1, s2);
                for (i = y+1; str[i] != ''; i++) s1[j++] = str[i];
                s1[j] = '';
                strcpy(str, s1);
            }
            for (i = 0; str[i] != ''; i++)
            {
                num = 0;
                if (str[i] >= '0' && str[i] <= '9')
                {
                    while (str[i] >= '0' && str[i] <= '9')
                    {
                        num = num * 10 + (str[i]-'0');
                        i++;
                    }
                    while (num--) printf("%c", str[i]);
                }
                else printf("%c", str[i]);
            }
            printf("
    ");
        }
        return 0;
    }
    让未来到来 让过去过去
  • 相关阅读:
    mongodb 条件查询
    node 创建静态web服务器(下)(处理异步获取数据的两种方式)
    node 创建静态web服务器(上)
    node.js 从文件流中读写数据及管道流
    node.js 中的 fs (文件)模块
    vue-router 嵌套路由
    (转载)js调用打印机 打印整体或部分
    web react面试题
    vue 面试题
    vue 父页面中含子页面滑动,滑动结束,底部组件进行滑动
  • 原文地址:https://www.cnblogs.com/Tinamei/p/4708614.html
Copyright © 2011-2022 走看看