Noip2017 improves D1T2 time complexity

I didn't mean to brush the outrageous network flow today and finally filled in this mass grave

 

First of all, we know that this is a simulation problem.

Then, this is a simulation to be judged wrong.

Finally, this is a simulation problem of data diversification.

 1 #include<cstdio>
 2 #include<cstdlib>
 3 #include<cstring>
 4 #include<iostream>
 5 #include<algorithm>
 6 #define ADT class
 7 #define elif else if
 8 #define reg register int
 9 #define lop(c,m) for(int c=1;c<=m;c++)
10 #define loop(c,m) for(int c=0;c<m;c++)
11 #define getspace(n) (n*)memset(malloc(sizeof(n)),0,sizeof(n))
12 using namespace std;
13 //Default source series

It's terrifying to use the amount of code that can be entered and judged as a mistake with standard input. I tried hard to get close to 1.5 hours and then burst to zero. (what happened on the day of Noip)

Today, we finally decided to use the string method. Save the program, judge the error first, and then start the calculation. It can also save the judgment of skipping a pile of input when the program is wrong.

1 #include<stack>
2 char prog[101][100];//Store comparison answers and procedures
3 stack<bool> hascomp;//Whether the loop makes the complexity climb (judge whether the current complexity needs to be reduced when exiting the loop)

Error determination: there are only two kinds of error situations, which can be written pertinently.

 1 bool errordeal(int &l){
 2     stack<char> varin;
 3     bool chuse[26];//Variable weight determination
 4     memset(chuse,0,sizeof(chuse));
 5     char ch;
 6     lop(i,l){//Cycle from 1 because prog[0]Put the comparison answer
 7         switch(prog[i][0]){
 8             case'F':
 9                 sscanf(prog[i],"%*c %c",&ch);//(It is a lowercase letter) reading variable
10                 if(chuse[ch-'a']){//Variable repetition
11                     printf("ERR\n");
12                     return 1;
13                 }
14                 else{
15                     varin.push(ch);
16                     chuse[ch-'a']=1;//sign
17                 }
18                 break;
19             case'E':
20                 if(varin.size()==0){//F and E Mismatch
21                     printf("ERR\n");
22                     return 1;
23                 }
24                 else{
25                     chuse[varin.top()-'a']=0;
26                     varin.pop();//Clear tag
27                 }
28         }
29     }
30     if(varin.size()){//If there's a cycle left, it's less E,Still wrong
31         printf("ERR\n");
32         return 1;
33     }
34     return 0;
35 }
36 //Return 1 if there is an error, return 0 if there is no error

After the miscarriage, it's all legal.

Treatment of cycles with unsatisfied conditions (the initial value is larger than the upper bound)

 1 int unloopdeal(int now){//Accept current line number
 2     int need=1;//How many do you need E To end the current cycle block
 3     for(int i=now+1;;++i){
 4         switch(prog[i][0]){
 5             case'F':
 6                 ++need; 
 7                 break;
 8             case'E':
 9                 --need;
10         }
11         if(!need) return i;//A value of 0 indicates the end of the loop block, and returns the end line number
12     }
13 }

Main program: do input and time complexity statistics.

 1 int main(){
 2     int t,l,lvalue,rvalue,comp,maxcomp;
 3     //t-Number of groups, l-Number of lines, lv-Initial value of the cycle, rv-The upper bound of the cycle, comp-Current complexity, mc-Overall complexity
 4     //It's all written here because it's a mess
 5     char ch,vari;
 6     char ans[100];
 7     //ch-Variable name, vari-Judge whether the upper bound and cycle are n Or numbers
 8     scanf("%d",&t);
 9     while(t--){
10         comp=maxcomp=0;
11         scanf("%d ",&l);
12         memset(prog,0,sizeof(prog));
13         loop(i,l+1) while((ch=getchar())!='\n') prog[i][strlen(prog[i])]=ch;//Read in the program, by the way O(balabala)
14         if(errordeal(l)) continue;//Read the next set of data directly if there is an error; this set of data has been read
15         lop(i,l){
16             switch(prog[i][0]){
17                 case'F':
18                     sscanf(prog[i],"%*c %c ",&ch);//Read variable name
19                     sscanf(prog[i],"%*c %*c %c",&vari);//Check initial value
20                     if(isdigit(vari)) sscanf(prog[i],"%*c %*c%d",&lvalue);
21                     else lvalue=-1;//It's digital reading, if it's not digital (i.e n)Make a mark-1
22                     if(lvalue!=-1) sscanf(prog[i],"%*c %*c %*d %c",&vari);
23                     else sscanf(prog[i],"%*c %*c %*c %c",&vari);
24                     if(isdigit(vari)){
25                         if(lvalue!=-1) sscanf(prog[i],"%*c %*c%*d%d",&rvalue);
26                         else sscanf(prog[i],"%*c %*c %*c%d",&rvalue);
27                     } 
28                     else rvalue=-1;//And the above principle, but whether the initial value is n,Otherwise, the formatted string cannot be written
29                     //In fact, you don't need to write the format string faithfully, but it's easy to understand
30                     if(lvalue==-1){
31                         if(rvalue!=-1) i=unloopdeal(i);//Process 1: initial value is n,Upper bound is constant, unable to enter the cycle
32                         else hascomp.push(0);//Process 2: initial value is n,The upper bound is n,Constant complexity
33                     }
34                     else if(rvalue==-1){
35                         maxcomp=max(maxcomp,++comp);
36                         hascomp.push(1);//Treatment 3: initial value is constant, upper bound is n,Complexity climb
37                     }
38                     else if(lvalue<=rvalue) hascomp.push(0);//Treatment 4: initial value is constant, upper bound is constant, and initial value is less than upper bound, constant complexity
39                     else i=unloopdeal(i);//Processing 4: the initial value is constant, the upper bound is constant, and the initial value is greater than the upper bound, unable to enter the cycle
40                     //If you can't enter the loop, skip the line number
41                     break;
42                 case'E':
43                     if(hascomp.top()) --comp;
44                     hascomp.pop();//Unexpectedly simple, it only needs to judge whether it is a constant complexity to determine whether to reduce the current complexity
45             }
46         }
47         if(!maxcomp) sprintf(ans,"O(1)");
48         else sprintf(ans,"O(n^%d)",maxcomp);
49         if(!strcmp(ans,prog[0])) printf("Yes\n");
50         else printf("No\n");//String comparison to determine the right and wrong answers
51     }
52     exit(0);
53 }

Tags: C++ less network

Posted on Thu, 21 May 2020 10:46:38 -0400 by archbeta