POJ1472-Instant complexity

Source: Internet
Author: User
Tags string to number

 

Reprinted please indicate the source: Thank you

Http://user.qzone.qq.com/289065406/blog/1308998858

 

General question:

A pascial program is provided to calculate the time complexity (computation is performed for items that can be computed, and conversion from computation to the simplest expression about n o (n ), and arrange items according to the N index from high to low). When outputting, items with a coefficient of 0 are not output, and items with a coefficient of 1 are not output coefficient, items with an index of 1 do not output an index.

A program has only one in, which indicates the beginning of the program. The end corresponds to the end of the program.

A program can have at most 10 layers of loop nesting. The loop entry is loop, and one loop corresponds to one end, which indicates the end of the loop of the layer.

The number of OPS in a program is unlimited.

 

Loop is the entry of the loop. The data following it may be a constant (non-negative integer) or a variable N, which indicates the number of times the loop body is executed.

OP is a statement, and the data after it can only be a constant (non-negative integer), representing the number of times the statement is executed.

 

Solution:

This is a questionExpression simplificationSimulation questions, directly simulated using recursion.

Use the first example to describe the processing method:

Begin

Loop n

OP 4

Loop 3

Loop n

OP 1

End

OP 2

End

OP 1

End

OP 17

End

In this example, we can obtain an initial expression about N:

N * (4 + 3 * (N * 1 + 2) + 1) + 17

A little simpler. Merge the op values of the same layer to obtain

N * (3 * (N * 1 + 2) + 5) + 17

It is not hard to see that every loop can be writtenK * n + IChild expression, whereLoop is * link, op is + Link

 

Since the maximum number of loops is 10, we use exp [11] to store the exponent I and coefficients of each polynomial K = exp [I], exp [0] is actually a constant, produced by OP statements.

 

Note that the loop may be followed by characters N or numbers. Processing Method: input s using a string. If s [0] = N, the loop will be processed directly; if s [0]! = N, it is considered a numeric string, convert it to int and then process it.

Source correction:

Http://www.informatik.uni-ulm.de/acm/Regionals/1997/

 

// Memory time // 264 K 0 Ms # include <iostream> using namespace STD;/* convert string to Number */INT strtonum (char * s) {int digit = 0; for (INT I = 0; s [I]; I ++) digit = digit * 10 + (s [I]-'0'); Return digit ;} bool solve (int * exp) {char s [30]; CIN> S; If (s [0] = 'E') // endreturn false; else if (s [0] = 'B') // beginwhile (solve (exp); // if it is returned because of OP, continue; if it is returned because of end, end else if (s [0] = 'O') // 0 p {CIN> S; exp [0] + = strtonum (s ); return solve (exp);} else // loop {int Tempe XP [11] = {0}; // temporary exp [] CIN> S; while (solve (tempexp); If (s [0] = 'n ') // loop N {for (INT I = 10; I> 0; I --) tempexp [I] = tempexp [I-1]; // expression multiplied by N, then the number of all items + 1 tempexp [0] = 0;} else // loop num {int x = strtonum (s); For (INT I = 0; I <11; I ++) tempexp [I] * = x; // if the expression is multiplied by const, the coefficient * const} For (INT I = 0; I <11; I ++) exp [I] + = tempexp [I];} return true;} int main (void) {int test; CIN> test; for (int t = 1; t <= test; t ++) {char s [6]; int exp [11] = {0 }; // The index is an I item and its Coefficient Is exp [I] solve (exp); cout <"Program #" <t <Endl; cout <"RunTime ="; bool flag = false; bool before = false; // indicates whether the previous item for (INT I = 10; I> = 0; I --) has been output before the current item is output --) if (exp [I]) // when the coefficient is not 0, the output item {flag = true; If (Before) {cout <'+ '; before = false;} If (! I) // when the exponent is 0, the direct output coefficient {cout <exp [I]; before = false;} else {bool Sign = false; // indicates whether the coefficient is 1if (I & exp [I]! = 1) {Sign = true; cout <exp [I]; before = true;} if (I) // when the index is not 0 {If (sign) cout <'*'; cout <'n'; if (I! = 1) cout <'^' <I; before = true ;}} if (! Flag) cout <0 <Endl; elsecout <Endl ;}return 0 ;}

 

 

Sample Input
11
Begin
Loop n
OP 4
Loop 3
Loop n
OP 1
End
OP 2
End
OP 1
End
OP 17
End
 
Begin
OP 1997 loop n op 1 end
End
 
Begin
Loop 0 OP 17 end
End
 
Begin
Loop n op 0 end
End
 
Begin
OP 1 loop n op 3 loop n op 2 end
End
 
Begin
Loop n OP 1
Loop n OP 2
Loop n OP 3
Loop n OP 4
Loop n OP 5
Loop n OP 6
Loop n OP 7
Loop n OP 8
Loop n OP 9
Loop n OP 10
End end
Loop 17 loop n op 3 end
End end
End
 
Begin loop 1 loop 2 loop 3 OP 1 loop 4 loop 5 loop 6 loop 7 loop 8 OP 1
End end OP 2 end OP 17 end
 
Begin OP 1 OP 2 OP 3 OP 4 OP 5 OP 6 OP 7 OP 8 OP 9 OP 10 OP 11 OP 12 End
 
Begin loop N
OP 12345 end
 
Begin op
17
Loop 2
Loop n
 
Loop
2 op
 
4 Loop
 
 
N op 4
Loop n OP 5
 
 
End
 
End OP 4 end
 
End end
 
End
 
 
 
Begin
OP 0 loop N Loop
N
 
OP 88 op 0 loop N loop 0 OP 17 end
 
 
 
End end
 
OP 0 loop N loop 3 op 0 end
End OP 8
End Sample output
Program #1
Runtime = 3 * n ^ 2 + 11 * n + 17
 
Program #2
Runtime = n ^ 2 + 1997
 
Program #3
Runtime = 0
 
Program #4
Runtime = 0
 
Program #5
Runtime = 2 * n ^ 3 + 3 * n ^ 2 + 1
 
Program #6
Runtime = 10 * n ^ 10 + 9 * n ^ 9 + 8 * n ^ 8 + 58 * n ^ 7 + 6 * n ^ 6 + 5 * n ^ 5 + 4 * N ^ 4 + 3 * n ^ 3 + 2 * n ^ 2 + n
 
Program #7
Runtime = 40391
 
Program #8
Runtime = 78
 
Program #9
Runtime = 12345 * n ^ 10
 
Program #10
Runtime = 20 * n ^ 3 + 16 * n ^ 2 + 32 * n + 17
 
Program #11
Runtime = 88 * n ^ 2 + 8

 

 

 

 

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.