Va 586 Instant Complexity

Source: Internet
Author: User

Idea: recursive Simulation

Analysis:
1. The question is the ball time complexity of a given piece of program code.
2 According to the question, we can use the stack and recursive methods, but the stack method is not easy to write, so we use recursive ideas to write
3. When we encounter LOOP, we will go recursive. When we encounter OP, we will calculate the complexity of the current layer and retuen the END time.
4. Pay attention to the final output.


Code:

 

#include<vector>#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;const int MAXN = 15;int ans[MAXN];char str[MAXN];void getAns(vector<string>v , int x){    int size = v.size();    int num_n = 0;    int num_xishu = x;    for(int i = 0 ; i < size ; i++){        int len = v[i].size();        if(v[i][0] == 'n')            num_n++;        else{            int sum = 0;            for(int j = 0 ; j < len ; j++)                 sum = sum*10 + v[i][j]-'0';            num_xishu *= sum;        }    }    ans[num_n] += num_xishu;}void solve(vector<string>v){    int x;    while(1){        scanf("%s" , str);        if(str[0] == 'L'){            scanf("%s" , str);             v.push_back(str);             solve(v);            v.pop_back();        }        else if(str[0] == 'O'){            scanf("%d" , &x);             getAns(v , x);        }        else if(str[0] == 'E'){            return;        }    }}void output(){    printf("Runtime = ");    bool isFirst = true;    bool isTime = false;    for(int i = MAXN-1 ; i >= 1 ; i--){        if(ans[i]){            isTime = true;             if(isFirst)                isFirst = false;            else                printf("+");            if(ans[i] > 1)                printf("%d*" , ans[i]);             printf("n");            if(i > 1)                printf("^%d" , i);        }    }    if(ans[0]){        isTime = true;         if(!isFirst)            printf("+");        printf("%d" , ans[0]);    }    if(!isTime)        printf("0");    printf("\n\n");}int main(){    int Case , cas = 1;    vector<string>v;    scanf("%d" , &Case);    while(Case--){        scanf("%s" , str);         memset(ans , 0 , sizeof(ans));        printf("Program #%d\n" , cas++);        v.clear();        solve(v);        output();    }     return 0;}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.