Complexity of Time

Source: Internet
Author: User

Time Complexity time limit: +Ms | Memory Limit:65535KB Difficulty:3
Describe

in the ACM inside, computational complexity is a very important thing, and common complexity formats are three kinds:

  1. O (N)
    O (LG (N))
    O (sqrt (n))

An algorithm often has a number of solutions, the complexity of each solution has the common complexity of the above combinations, such as two algorithms for sequencing:

  1. Quick Sort: time complexity is O (N*LG (n))

  2. Bubble Sort: time complexity is O (n*n)

now given you aN,mThe complexity of the algorithm, determine if the complexity will time out. If the complexity calculation result is greater than100000000, it is timed out(TLE), otherwise the output calculates the complexity of the output and retains two decimal places for the result.

(LG (N)represents the2as the base,Nis the value of the true number)

Input
The
first line is input n (1≤n≤10000), M (1≤m≤100), where n is the number of the topic description, and M is the number of algorithm complexity.
Next m line, each behavior a string, each string contains O () the data inside any parentheses is guaranteed to be composed and valid only by N,LG (), sqrt (), * *. As shown in sample input.
Output
for each string, if the calculated complexity is greater than 100000000, the output tle, otherwise the calculation of the complexity of the output number
Sample input
10000 6O (n*n) O (n*n*n) o (sqrt (n)) O (LG (n)) O (N*LG (n)) O (N*lg (N*LG (n))
Sample output
100000000.00tle100.0013.29132877.12170197.33
Tips
the C language code for LG (n) can be written like this

Log (n)/log (2)


Code:

 #include <stdio.h> #include <string.h> #include <math.h> #define N 100000000char stack[1005];int k,n; Double eval () {if (k<0) return 0;if (stack[k]== ' n ') {K--;return n;} if (stack[k]== ' * ') {K--;return eval () *eval ();} if (stack[k]== ' G ') {K--;return log (eval ())/log (2);} if (stack[k]== ' t ') {K--;return sqrt (eval ());}} int main () {int m,i,j,top,len,s;double Result;char str[1005],ch[1005];while (~scanf ("%d%d", &n,&m)) {for (i=1;i <=m;i++) {scanf ("%s", str), Top=-1;s=-1;len=strlen (str), for (j=len-1;j>=1;j--) {if (str[j]== ' n ') {top++;stack[ TOP]=STR[J];} if (j<1) break;if (str[j]== ') ' | | str[j]== ' * ') {s++;ch[s]=str[j];} else if (str[j]== ' (') {while (s>=0&&ch[s]!= ') ') {top++;stack[top]=ch[s];s--;} s--;} else if (str[j]== ' t ') {top++;stack[top]=str[j];j=j-3;} else if (str[j]== ' G ') {top++;stack[top]=str[j];j=j-1;}} while (s>=0) {if (ch[s]!= ') ') {top++;stack[top]=ch[s];s--;}} top++;stack[top]= ' + '; K=strlen (Stack) -1;result=eval (); if (result>n) printf ("tle\n"); elseprintf ("%.2f\n", result);}}       return 0;}  



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Complexity of Time

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.