Home > Back-end >  Why do I this code or timeout
Why do I this code or timeout

Time:02-16


The above is subject
Requirements: time complexity is O (n), space complexity is O (1),
It is my code:

#include
Int main ()
{
Int a, b, c, d, I;
char ch;
A=b=c=d=0;
While ((ch=getchar ())! )
='\ n'{
If (ch=='a') +;
Else if (ch=='b') b++;
Else if (ch=='c') c + +;
Else if (ch=='d') d++;
}
for(i=0; Ifor(i=0; Ifor(i=0; Ifor(i=0; IPrintf (" \ n ");
return 0;
}

CodePudding user response:

 
#include
#include
#include
Int main ()
{
Int a [4]={0}, I=0;
Char ch, * p, * q;
While ((ch=getchar ())! )
='\ n'A [ch - 'a'] + +;
Q=p=malloc (a [0] a [2] + [1] a + + a + 1) [3].
While (I & lt; 4)
{
Memset (q, 'a' + I, a [I]);
Q +=a [i++];
}
* q='\ 0';
Puts (p);
Free (p);
return 0;
}

Half after the front half can shorten a bit, but don't know the upper limit of data size is not good ah, had to figure out a way to reduce the output frequency, output only one try,

CodePudding user response:

This limited space is O (n), alas,

CodePudding user response:

refer to the second floor forever74 response:
this limited space is O (n), alas,

Yes, no, if space is O (n) is much simpler

CodePudding user response:

The problem is your problem is timeout,
Time space down the gourd ladle this is known to all,
How do you try first systems to express space exceeds bid,

CodePudding user response:

reference 4 floor forever74 response:
problem is your problem is timeout,
Time space down the gourd ladle this is known to all,
You first try system is how to express spatial overweight,

Time more than you will be prompted a timeout, the space is more than you will be prompted out of memory

CodePudding user response:

I mean, I think the system can not accurately determine your O level of space and time,

CodePudding user response:

Such a try, for your reference:
 # include & lt; Stdio. H> 
Int main ()
{
Char [4] b={' a ', 'b', 'c', 'd'}, ch.
Int a [4]={0};
While ((ch=getchar ())! )
='\ n'A [ch - b [0]] + +;
for(int i=0; I<4. I++) {
While (a [I] -)
Printf (" % c ", [I] b);
}
system("pause");
return 0;
}

CodePudding user response:

refer to 7th floor QZJHJXJ response:
such a try, for reference:
 # include & lt; Stdio. H> 
Int main ()
{
Char [4] b={' a ', 'b', 'c', 'd'}, ch.
Int a [4]={0};
While ((ch=getchar ())! )
='\ n'A [ch - b [0]] + +;
for(int i=0; I<4. I++) {
While (a [I] -)
Printf (" % c ", [I] b);
}
system("pause");
return 0;
}


I feel the time complexity and space complexity are super

CodePudding user response:

refer to the eighth floor m0_55067373 response:
Quote: refer to 7th floor QZJHJXJ response:
such a try, for reference:
 # include & lt; Stdio. H> 
Int main ()
{
Char [4] b={' a ', 'b', 'c', 'd'}, ch.
Int a [4]={0};
While ((ch=getchar ())! )
='\ n'A [ch - b [0]] + +;
for(int i=0; I<4. I++) {
While (a [I] -)
Printf (" % c ", [I] b);
}
system("pause");
return 0;
}


I feel the time complexity and space complexity are super

This conforms to the time complexity is O (n), space complexity is O (1),

CodePudding user response:

Traversal time complexity is O (n), space is constant so complexity is O (1),

CodePudding user response:

references to the tenth floor QZJHJXJ response:
traversal time complexity is O (n), the space is constant so complexity is O (1),

Try, or timeout,
You for a while, a double loop, or O (n), because haven't learned, only yourself to self-study, so don't understand of time and space complexity
  • Related