SPOJ | Solved SPOJ problems | Learning library
kandi X-RAY | SPOJ Summary
kandi X-RAY | SPOJ Summary
This repository contains solutions to almost all problems I have solved on SPOJ. They’re intended as a last resort in case you’ve tried very hard to solve a problem but you just haven’t been able to figure it out. When I was trying to solve GSS2, I mostly found vague hints on online forums, which weren’t helpful. I wasn’t able to solve the problem until after I looked at some code. So obviously the code helps, and there can be good reasons to just look at code rather than being stuck indefinitely.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of SPOJ
SPOJ Key Features
SPOJ Examples and Code Snippets
Community Discussions
Trending Discussions on SPOJ
QUESTION
I'm working on SPOJ problem where you have to write an algorithm that based on input string conditions outputs new string, but you can't exceede time limit. problem link
The fastest i could get was by using two stacks, but time limit was still exceeded, now I tried implementing doubly linked list, but it's twice slower than when I used stack. Do you have any idea on how can I increase performance of implemented linked list, or maybe should I use other data structure for this problem? Thought of implementing Node as a structure but not really sure if you can do that.
...ANSWER
Answered 2022-Mar-27 at 09:35An implementation using a linked list will not be as fast as one that uses StringBuilder
, but assuming you are asking about a linked list based implementation I would suggest not to reimplement LinkedList
. Just use the native one.
This means you don't have to change much in your code, just this:
- Define the type of the list nodes as
char
:new LinkedList();
- Instead of
.Head
use.First
- Instead of
.Print
usestring.Join("", list)
However, there are these problems in your code:
When the input is
>
, you should allow the logic to execute whennode
isnull
. Currently youcontinue
, but anull
may mean that your "cursor" is in front of the non-empty list, so you should still deal with it, and move the "cursor" tolist.First
When the input is
-
, you should still perform the removal even whennode.Previous
isnull
, because it is not the previous node that gets removed, but the current node. We should imagine the cursor to be between two consecutive nodes, and your removal logic shows that you took as rule that the cursor is between the currentnode
andnode.Next
. You could also have taken another approach (with the cursor is just beforenode
), but important is that all your logic is consistent with this choice.When executing the logic for
-
-- in line with the previous point -- you should take into account thatnode.Previous
could benull
, and in that case you cannot do the removal as you have it. Instead, you could first assign the node reference to a temporary variable, then move the cursor, and then delete the node that is referenced by the temporary reference.
Here is the corrected code, using the native LinkedList
implementation. I moved the logic for doing nothing (your continue
) inside each separate case, as I find that easier to understand/debug:
QUESTION
I'm trying to get the job done MOHIBPIZ - PIZZA (https://www.spoj.com/problems/MOHIBPIZ/). I'm already sitting on it the second day, I've tried everything I can and found on the internet. The last chance before giving up is to ask you guys For recudces time I'm using InputOutput class created by davidsekar (https://github.com/davidsekar/C-sharp-Programming-IO/blob/master/ConsoleInOut/InputOutput.cs) but still I have time "time limit exceeded". :(
I tried with two loops, but the method with the function seems more optimal to me. Thanks in advance for all the hints, suggestions and answers.
This is code (link on ideone: https://ideone.com/):
...ANSWER
Answered 2021-Nov-09 at 09:17As far as I can see, you have a well known Circle Division problem; see also A000124 sequence:
number of pieces after n
cuts are (n * n + n + 2) / 2
That's why we can put O(1)
time and space complexity
Code:
QUESTION
I'm a beginner in c# and I'm training on codeforces and SPOJ to learning how to solve problems, the problem with me is when the loop is break doesn't print all the input it's just end the program without printing
please can someone tell me what is the mistake in these 2 codes that i write >
the Input: 1,2,88,42 ,99 Output: 1,2,88
...ANSWER
Answered 2021-Oct-11 at 20:20Answering to "...the problem is when the loop is break doesn't print all the input it's just end the program...":
That's because after you breaked loop there is no more code to execute - so Console closes. You can add Console.ReadKey()
or Console.ReadLine()
at the end of Main
method to keep Console opened after loop breaked. Console.ReadKey()
would wait until you press any key, Console.ReadLine()
would wait until you input something and press Enter (or just press Enter). Or in both ways Console will stay opened until you close it manually.
QUESTION
I was solving
https://www.spoj.com/problems/BEADS/
above question at SPOJ. I have stated the relevant information below:
Problem Statement: The description of the necklace is a string A = a1a2 ... am specifying sizes of the particular beads, where the last character am is considered to precede character a1 in circular fashion. The disjoint point i is said to be worse than the disjoint point j if and only if the string aiai+1 ... ana1 ... ai-1 is lexicografically smaller than the string ajaj+1 ... ana1 ... aj-1. String a1a2 ... an is lexicografically smaller than the string b1b2 ... bn if and only if there exists an integer i, i <= n, so that aj=bj, for each j, 1 <= j < i and ai < bi.
Output: For each test case, print exactly one line containing only one integer -- number of the bead which is the first at the worst possible disjoining, i.e. such i, that the string A[i] is lexicographically smallest among all the n possible disjoinings of a necklace. If there are more than one solution, print the one with the lowest i.
Now the solution is using SUFFIX ARRAY. Input string s, and concat with itself, s'=s+s ,since I have to sort cyclic suffixes of array. Then create a suffix array on s', and output the smallest index that points to a suffix of original s, i.e., index < len(s).
But there is a problem I face. I was appending '$' character to get SA, but I was getting wrong answer. After looking online, I found 1 solution that had appended '}' to string. I found that ascii('$') < ascii('a') < ascii('z') < ascii('}')
But i don't understand how this will make a difference, why this is accepted answer and haven;t found a case where this will make a difference. The solution (AC) can be found here:
...ANSWER
Answered 2021-Jul-30 at 15:22The difference is in the last condition:
If there are more than one solution, print the one with the lowest i.
Consider input "abab"
.
The correct answer is 0, which you get when you append '}', because "abababab}"
is less than all of its suffixes.
If you append '$', you get the wrong answer, because "ab$"
< "abab$"
< "ababab$"
< "abababab$"
.
QUESTION
I am trying to solve the SPOJ problem DIVFACT where we need to find the factorial of a number. Though I used the correct formulae and at the same time I also checked the special case of 0 and 1,still I am getting wrong answer and it's really difficult for me to figure out what's wrong with my code. Can someone please help me figure out the problem? For reference I am providing the link to the problem:- Link of the problem
...ANSWER
Answered 2021-Jun-10 at 21:59The problem states that the answer should be in MOD 10^9+7
but you have mistakenly defined MOD
as 10^8+7
.
QUESTION
Using pandas/python, I want to calculate the longest increasing subsequence of tuples for each DTE
group, but efficiently with 13M rows. Right now, using apply/iteration, takes about 10 hours.
Here's roughly my problem:
DTE Strike Bid Ask 1 100 10 11 1 200 16 17 1 300 17 18 1 400 11 12 1 500 12 13 1 600 13 14 2 100 10 30 2 200 15 20 2 300 16 21 ...ANSWER
Answered 2021-May-27 at 13:27What is the complexity of your algorithm of finding the longest increasing subsequence?
This article provides an algorithm with the complexity of O(n log n).
Upd: doesn't work.
You don't even need to modify the code, because in python comparison works for tuples: assert (1, 2) < (3, 4)
QUESTION
I am solving task on spoj platform - count number of digits in factorial. I found Kamenetsky Formula and implemented it:
...ANSWER
Answered 2021-May-19 at 07:00I think the time limit is exceeding because of the slow I/O. This is mainly because of System.out.println's underlying PrintStream. Find more details in this post why-is-system-out-println-so-slow. You can refer to the below Fast I/O template which will help solve this problem.
Reference - Fast I/O in java
QUESTION
I tried a problem from SPOJ called The next palindrome. The problem statement is given as
A positive integer is called a palindrome if its representation in the decimal system is the same when read from left to right and from right to left. For a given positive integer K of not more than 1000000 digits, write the value of the smallest palindrome larger than K to output. Numbers are always displayed without leading zeros.
Here is my code
...ANSWER
Answered 2021-May-16 at 06:21The error is caused because your input "n" makes both arguments same in the range function in this line "for j in range(int(n) + 1, 1000000 + 1)". It looks like this range(1000001, 1000001) which return nothing. Use input of "n" smaller than 1000000. It will work.
It return empty list when I use this code.
QUESTION
I'm trying to submit this code on spoj https://www.spoj.com/problems/PALIN which is asking to find next smallest palindrome of a given number n, but as this code works, it is slow therefor it exceeds the time limit (2-9 seconds). is there another way to solve this exercise in a faster way?
The first line contains integer t, the number of test cases. Integers K are given in the next t lines.
code:
...ANSWER
Answered 2021-Mar-08 at 07:29The most obvious thing to do is to stop copying and reversing the string. Instead, compare the first character to the last character, then the second to the second-to-last, and so on.
Also, why are you using strings at all? Strings are complex and expensive. The operations you are performing can be done entirely on numbers.
Lastly, consider numbers like "473X". None of those can ever be palindromes. You don't have to test all ten of them. If you're going to look at four-digit numbers starting with "47", there's only one that's a palindrome -- so why are you checking all hundred of them?
Before writing code to solve a problem like this, think through the algorithm you're going to use and make sure you don't have any obvious wasted effort.
QUESTION
The following code gets TLE (time limit exceeded) if the standard output is not flushed.
...ANSWER
Answered 2021-Mar-01 at 16:10The problem specification tells you what to do:
Attention: the program should clear the output buffer after printing each line.
It can be done using fflush(stdout) command or by setting the proper type of buffering at the beginning of the execution - setlinebuf(stdout).
The problem title indicates why:
(Interactive)
The judging software is operating the program interactively. After giving the program an input, it waits for output before providing more input. When your program does not flush the buffered output, the judging software keeps waiting until its time limit is exceeded.
Commonly, output streams to terminals are line-buffered, in which case printing a new-line character causes them to be flushed. However, this judging software is likely using a pipe that is fully buffered, so output will not be flushed until the buffer is full or you explicitly request a flush (or, at the start of the program, you change the buffering mode to unbuffered or line-buffered).
When you flush the output, the judging software will see it, and then it will continue and provide more input.
As an alternative to flushing the output after each printf
, you can set the stream to line buffering mode by executing setvbuf(stdout, NULL, _IOLBF, 0);
at the beginning of the program (before doing any other operation on the stream).
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install SPOJ
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page