@amruthp wrote:
Where can I get the solutions posted by others on a particular problem
Posts: 1
Participants: 1
@amruthp wrote:
Where can I get the solutions posted by others on a particular problem
Posts: 1
Participants: 1
@godzila555 wrote:
i have come across lots of java codes for some problems which seem easy to solve, but some people write codes which include additional classes and the program runs faster. for example there is an easy task: http://www.spoj.com/problems/SMPCIRC/
i saw some kinds of solutions of this problem. one of them is this:import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { static class CircleCoord { int cX; int cY; int r; public CircleCoord(int cx, int cy, int r) { this.cX = cx; this.cY = cy; this.r = r; } public int getXTerm(int x) { return (x - cX) * (x - cX); } public int getYTerm(int y) { return (y - cY) * (y - cY); } } static class TestCase { CircleCoord o1; CircleCoord o2; public TestCase(CircleCoord o1, CircleCoord o2) { this.o1 = o1; this.o2 = o2; } public CircleCoord getO1() { return o1; } public CircleCoord getO2() { return o2; } } public static void main(String[] args) { TestCase[] testCases = readTestCases(); if (testCases == null) { testCases = new TestCase[0]; } for (TestCase testCase : testCases) { CircleCoord o1 = testCase.getO1(); CircleCoord o2 = testCase.getO2(); double distance = Math.sqrt(Math.pow((o1.cX - o2.cX), 2) + Math.pow(o1.cY - o2.cY, 2)); if (distance == Math.abs(o1.r - o2.r)) { System.out.println('E'); } else if (distance < Math.abs(o1.r - o2.r)) { System.out.println('I'); } else { System.out.println('O'); } } } private static TestCase[] readTestCases() { TestCase[] testCases = new TestCase[0]; BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); try { int n = Integer.parseInt(reader.readLine().trim()); testCases = new TestCase[n]; int index = 0; while (index < n) { String[] coord = reader.readLine().trim().split(" "); testCases[index++] = new TestCase(new CircleCoord(Integer.parseInt(coord[0]), Integer.parseInt(coord[1]), Integer.parseInt(coord[2])), new CircleCoord(Integer.parseInt(coord[3]), Integer.parseInt(coord[4]), Integer.parseInt(coord[5]))); } } catch (Exception e) { e.printStackTrace(); } finally { try { reader.close(); reader = null; } catch (IOException e) { e.printStackTrace(); } } return testCases; } }
which uses additional classes but is faster than this simple solution:
import java.util.Scanner; public class Main{ /** * @param args */ public static void main(String[] args) { Scanner scan = new Scanner (System.in); int cases= scan.nextInt(); for (int i = 0; i < cases; i++){ double x1= scan.nextInt(); double y1= scan.nextInt(); double r1= scan.nextInt(); double x2= scan.nextInt(); double y2= scan.nextInt(); double r2= scan.nextInt(); double d= Math.sqrt((Math.pow(x1-x2,2)+Math.pow(y1-y2,2))); if (d==Math.abs(r1-r2)){ System.out.println("E"); } else if(d<=Math.abs(r1-r2)){ System.out.println("I"); } else { System.out.println("O"); } } } }
i wonder how this happens. There is an additional class, therefore additional objects in the first solution and some test case, whereas the second one has only the test case. could you explain this to me? since this is not the first time i see such long solutions with additional classes which runs faster than simply written programs. Please, explain what slows down the second solution.
Posts: 1
Participants: 1
@amit_yadav wrote:
I am getting Wrong Answer.Can somebody tell where i am wrong?
int main() {
vector result;
string str;
cin >> str;
while(str[0]!='0')
{
int len=str.length();
long long dp[len+1];
/*Initializing dp to 0*/
for(int i=0; i/*base case*/ dp[0]=1; int num; int i=1; //i for index while(i<len) { num=(str[i-1]-'0')*10 + (str[i]-'0'); if(str[i]-'0'!=0 && (num>=10 && num<=26) ) { dp[i]=dp[i-1] + dp[(i-2)<0?0:i-2]; } else{ dp[i] = dp[i-1]; } i++; } result.push_back(dp[len-1]); cin >> str;
}
for(int i=0; i cout << result[i] << endl;
}
return 0;
}
Posts: 1
Participants: 1
@xproxmess wrote:
Hi, I don't know why I'm getting WA even though my code seems to satisfy the test cases and logic is correct. Would like it if someone could help out!
Code : http://ideone.com/ArlajP
Posts: 1
Participants: 1
@adi_tdkr wrote:
Anyone can please give me solution or any hint about how to solve Character Patterns (Act 5) problem.
Posts: 1
Participants: 1
@aastik wrote:
import java.util.Scanner; public class Main{ public static void main(String args[]){ Scanner input2 = new Scanner(System.in); Scanner input = new Scanner(System.in); int t = input2.nextInt(); for(int i=0;i<t;i++){ String s = input.nextLine(); char[] carr = s.toCharArray(); for(int j=0;j<carr.length/2;j=j+2){ System.out.print(carr[j]); } System.out.println(); } } }
My code is working fine in Eclipse but giving a NZEC error in SPOJ. Can anyone point out why?
Posts: 6
Participants: 2
@oceanofdreams wrote:
I used C++ to solve this problem and I get the same expected output. However, I am getting "wrong answer". Can anyone please help me to identify the problem below?
#include <stdio.h> int main(void) { // your code here int n, l, c, num = 0; int i,j; scanf("%d",&n); while(n--) { scanf("%d %d", &l, &c); num = 0; for(i = 0; i < l; i++) { for(j = 0; j < c ; j++) { if((num%2)==0) { printf("*"); } else { printf("."); } num ++; } printf("\n"); } printf("\n"); } return 0; }
Posts: 3
Participants: 2
@aastik wrote:
My code is exceeding the time limit.
import java.util.Scanner; class spojdivisorsum { public static void main(String args[]){ Scanner input = new Scanner(System.in); int t = input.nextInt(); while(t>0){ int x = input.nextInt(); printsum(x); t--; } } public static void printsum(int x){ int sum = 0; for(int i=1;i<=x/2;i++){ if(x%i==0){ sum=sum+i; } } System.out.println(sum); } }
Can anyone point out a way to do better?
Posts: 2
Participants: 2
@godzila555 wrote:
just solved the problem by replacing '\0' chars by ' '. however, the result seems the same in both cases. surprisingly it worked.
Posts: 6
Participants: 2
@aritra741 wrote:
Hello. SPOJ keeps showing the message,"Time Limit Exceeded" when I submit my solution to The problem "Prime Generator". Would anyone please advise me on how I can optimize my code?
#include <stdio.h> #include <math.h> int main() { int tst; int fir,sec,div; scanf("%d", &tst); while(tst--) { scanf("%d %d", &fir,&sec); if(fir==2 || sec==2) printf("2\n"); for(;fir<=sec;fir++) { if(fir%2==0) continue; for(div=2;div<=sqrt(fir);div++) { if(fir%div==0) break; } if(fir%div!=0) printf("%d\n", fir); } } return 0;
}
Posts: 5
Participants: 2
@shubhu1596 wrote:
I am not able to find the problem with this code. Test this for many input seems to be working fine on ideone
but when I submit it SPOJ says WA.
Here's the code in c++(g++ 5.1)include
include
using namespace std;
int main() {
int n=0,p=-1;
cin>>n;
long arr[n],m=0,i;for(int q=0;q cin>>arr[q];
long a=arr[q]+1;
i=a;int k=p; i=a; while(i){ m=0; p=-1; a=i; while(a){ a=a/10; p=p+1; } a=i; while(a){ m=m+(a%10)*pow(10,p); a=a/10; p=p-1; } if(i==m) {cout<<m<<"\n"; i=0;break; } else i=i+1; } } // your code here return 0;
}
Posts: 2
Participants: 2
@waqqas wrote:
I've implemented a solution to The Cursed Room problem.
#include <iostream> #include <fstream> #include <sstream> #include <vector> #include <set> #include <algorithm> #define INDEX(bedNumber) (bedNumber - 1) std::vector<int> bedroomDemand; bool leastInDemand(int bedroomNumberLeft, int bedroomNumberRight){ return bedroomDemand[INDEX(bedroomNumberLeft)] < bedroomDemand[INDEX(bedroomNumberRight)]; } int main() { std::string input = "3\n" "3 3\n" "1 1\n" "2 1\n" "2 2\n" "3 2\n" "3 3\n" "0 0\n" "4 3\n" "1 1\n" "1 3\n" "2 1\n" "3 1\n" "3 2\n" "4 2\n" "0 0\n" "4 2\n" "1 1\n" "1 2\n" "2 1\n" "2 2\n" "3 1\n" "3 2\n" "4 1\n" "4 2\n" "0 0"; std::istringstream inputString(input); int lineNumber; std::string line; std::set<int> assignedBedrooms; std::vector<std::vector<int> > studentNumberToRequestedBedroomsMap; while (getline(inputString, line)) { ++lineNumber; std::stringstream lineStream(line); if (lineNumber == 1) { continue; } if (bedroomDemand.empty()) { int numberOfStudents; lineStream >> numberOfStudents; int numberOfBeds; lineStream >> numberOfBeds; bedroomDemand.resize(numberOfBeds); std::fill(bedroomDemand.begin(), bedroomDemand.end(), 0); // initialize studentNumberToRequestedBedroomsMap studentNumberToRequestedBedroomsMap.resize(numberOfStudents); std::fill(studentNumberToRequestedBedroomsMap.begin(),studentNumberToRequestedBedroomsMap.end(),std::vector<int>()); continue; } int bedroomNumber; lineStream >> bedroomNumber; int studentNumber; lineStream >> studentNumber; if (studentNumber != 0) { studentNumberToRequestedBedroomsMap[studentNumber-1].push_back( bedroomNumber); bedroomDemand[INDEX(bedroomNumber)]++; } else { // End of test case // Sort bedRequested in order of least in-demand to most in-demand for (std::vector<int> &item : studentNumberToRequestedBedroomsMap) { std::sort( item.begin(), item.end(), leastInDemand); } // Start assigning rooms to students starting with the least in-demand // room requested. for (std::vector<int> item : studentNumberToRequestedBedroomsMap) { for (int bedroomNumber : item) { std::pair<std::set<int>::iterator,bool> result = assignedBedrooms.insert(bedroomNumber); if (result.second) { break; } } } std::cout << assignedBedrooms.size() << std::endl; assignedBedrooms.clear(); bedroomDemand.clear(); studentNumberToRequestedBedroomsMap.clear(); } } }
I ran the solution on command-line and on ideone.com and in both cases, my application's output matches the expected output listed in the question.
When I submit my solution to SPOJ, it claims that the answer is wrong. I do not understand why it is so. Is there a way I can find out in which scenario my code has failed?
Also, I'm new to SPOJ so my apologies if I'm posting this question in the wrong place. Thanks.
Posts: 4
Participants: 2
@godzila555 wrote:
Hello, i can't pass character pattern (act 5) . it tells me wrong answer. But i checked my code on some cases and it produced correct resuts. could you tell me what's wrong with it?
here is the task: http://www.spoj.com/problems/CPTTRN5/
here is my solution:
import java.util.*; import java.lang.*; class Main { public static void main (String[] args) throws java.lang.Exception { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); for(int i=0; i<t; ++i){ int line=sc.nextInt(); int column=sc.nextInt(); int height=sc.nextInt(); for(int j=0; j<line*height+line+1; ++j){ for(int k=0; k<column*height+column+1; ++k){ if(j%(height+1)==0 || k%(height+1)==0)System.out.print("*"); else if((k/(height+1)+j/(height+1))%2==0){ if(k%(height+1)==j%(height+1)){ System.out.print("\\"); } else if((k%(height+1)+j%(height+1))%(height+1)==0){ System.out.print("."); } else System.out.print(" "); } else if((k/(height+1)+j/(height+1))%2==1){ if((k%(height+1)+j%(height+1))%(height+1)==0){ System.out.print("/"); } else if(k%(height+1)==j%(height+1)){ System.out.print("."); } else System.out.print(" "); } } System.out.println(); } if(i!=t-1)System.out.println(); } sc.close(); } }
Posts: 6
Participants: 2
@godzila555 wrote:
Hello guys, i have problem with the task: http://www.spoj.com/problems/CPTTRN7/
i always get TLE, but could you tell me what is the main reason of long running time of my code?
I'm a beginner, so i really don't know what to do with it.
Here is the solution:import java.util.*; import java.lang.*; class Main { public static void main (String[] args) throws java.lang.Exception { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); for(int i=0; i<t; ++i){ int r=sc.nextInt(); int c=sc.nextInt(); int s=sc.nextInt(); for(int j=0; j<r; ++j){ for(int n=0; n<2*s; ++n){ for(int k=0; k<c; ++k){ for(int m=0; m<2*s; ++m){ if(n<s){ if(m==s-n-1){ System.out.print("/"); } else if(m==s+n){ System.out.print("\\"); } else{ System.out.print("."); } } else{ if(m==n-s){ System.out.print("\\"); } else if(3*s-1-n==m){ System.out.print("/"); } else{ System.out.print("."); } } } } System.out.println(); } } if(i!=t-1)System.out.println(); } } }
Posts: 5
Participants: 4
@shubhu1596 wrote:
Solution Id:16444505
Even multiple test cases are successful at ideone spoj is showing WA.
Please suggest the problem with this code.
Thanks
code:include
include
include
using namespace std;
void mod(char m[100000])
{
long k,j,z=0;
long l=strlen(m);
for(j=l-1;j>=0;j--)
{ if(m[0]=='9'&&j==0)
{
m[0]='1';
m[l]='0';
m[l+1]='\0';
l=l+1;
break;
}
if(m[j]=='9')
{
m[j]='0';
}
else
{
m[j]+=1;
break;
}} for(k=0;k<l&&l!=1;k++) { if(m[k]=='0') { z=z+1; } else break; } if(z!=0) { for(long i=0;i<=l;i++) { m[i]=m[z]; z=z+1; } l=l-z; m[l+1]='\0'; }
}
int main()
{
long t,k,z=0,d=0,j;
cin>>t;
char arr[1000000];
do{
t=t-1;
scanf("%s",&arr);
mod(arr);
d=strlen(arr);
if(d%2!=0)
{
char a[d/2],b[d/2],c=arr[d/2];
long o=d/2;
a[o]='\0';
b[o]='\0';
for(j=0,k=o+1;j {
a[o-j-1]=arr[j];
b[j]=arr[k];
k=k+1;
j=j+1;
}
if(strcmp(a,b)>=0)
{
strcpy(b,a);
}
else
{ if(c=='9')
{
c='0';
for(long j=0;j {
if(a[j]=='9')
{
a[j]='0';} else { a[j]+=1; break; } } } else { c+=1; } strcpy(b,a); }
for( j=o-1,k=0;j>=0||k {
arr[k]=a[j];
arr[o]=c;
arr[k+o+1]=b[k];
}
puts(arr);} else { char a[d/2],b[d/2]; long o=d/2; a[o]='\0'; b[o]='\0'; for( j=0,k=o;j<d/2;) { a[o-j-1]=arr[j]; b[j]=arr[k]; k=k+1; j=j+1; } if(strcmp(a,b)>=0) { strcpy(b,a); } else for(j=0;j<o;j++) { if(a[j]=='9') { a[j]='0'; } else { a[j]+=1; break; } } strcpy(b,a); for( j=o-1,k=0;j>=0||k<o;j--,k++) { arr[k]=a[j]; arr[k+o]=b[k]; } puts(arr); }
}while(t);
return 0;
}
Posts: 2
Participants: 2
@adi_tdkr wrote:
How to solve test case 3 in CPTTRN5 will 2 4 2 be a valid case.
Posts: 4
Participants: 2
@adi_tdkr wrote:
What is wrong in my code because I am getting wrong answer on spoj?
int main() { int t; scanf("%d",&t); while(t--){ int l,c,h,w,i,j,k,p,q,x,y,z; scanf("%d%d%d%d",&l,&c,&h,&w); if(c==1 && w==1 ){ for(x=1;x<=l*h+1;x++){ for(y=1;y<=w;y++){ if(x%2==0){ printf("-+-\n"); } else{ for(i=1;i<=h;i++){ for(j=1;j<=h+1;j++){ if(j%2==0){ printf("|");} else { printf(".");} } printf("\n"); }}} }} else if(h==1){ for(i=0;i<1;i++){ for(j=1;j<=(c*w)+1;j++){ if(j%2==0){ printf("|"); } else { printf(".."); } } } printf("\n"); for(i=1;i<=l*2;i++){ if(i%2==0){ for(k=0;k<1;k++){ for(z=1;z<=(c*w)+1;z++){ if(z%2==0){ printf("|"); } else{ printf(".."); } } printf("\n"); } }else{ for(k=0;k<1;k++){ for(z=1;z<=(c*w)+1;z++){ if(z%2==0){ printf("+"); } else{ printf("--"); } } printf("\n"); } } } } else{ for(x=1;x<=l*2+1;x++){ if(x%2==0){ for(i=0;i<1;i++){ for(j=1;j<=c*w+1;j++){ if(j%2==0){ printf("+");} else{ printf("--");} } } printf("\n"); }else{ for(i=0;i<h;i++){ for(j=1;j<=c*w+1;j++){ if(j%2==0) { printf("|"); } else{ printf(".."); } } printf("\n"); }} } } } return 0; }
Posts: 6
Participants: 2
@luoji83 wrote:
Hey, in the "Read this before posting" I checked:
- input/output methods - am using raw_input()
- test your code on boundary cases - generated test cases from 0 to inputs up to 100000 digits
- search - the few results didn't contain helpful answers
Other things pertinent info that has left me stumped:
- chose "ideone it" on the fail results page, and it runs fine there
- ensured that "python 2.7" was correct to choose from the language dropdown, by comparing to output of "$python --version"
- have solved another challenge (onp) with python without issue
- the fail results page says that the time spent is "0.00" meaning that it's failing very very earlyOk, here's the code, any help appreciated:
#!/usr/bin/env python # http://www.spoj.com/problems/PALIN/ # PALIN - The Next Palindrome import sys class PalindromeIterator: def __init__(self, n): self.setAt(n) def setAt(self, n): # precondition input log = 0 s = str(n) if len(s) % 2: # odd log = len(s)/2 self.count = int(s[0:log+1]) self.pivot = True else: # even log = len(s)/2-1 self.count = int(s[0:log+1]) self.pivot = False self.limitLo = 10**log self.limitHi = 10**(log+1) - 1 # cheap way to initialize while self.current() < n: self.next() #self.status() def status(self): print " count: %d" % self.count print " pivot: %d" % self.pivot print "limitLo: %d" % self.limitLo print "limitHi: %d" % self.limitHi print '--' def current(self): s = str(self.count) if self.pivot: return int(s + (s[:-1])[::-1]) else: return int(s + s[::-1]) def __iter__(self): return self def next(self): answer = self.current() # where next? self.count += 1 if self.count > self.limitHi: if self.pivot: # just reset, unset pivot self.count = self.limitLo self.pivot = False else: # set new limits self.limitLo = self.LimitLo * 10 self.count = self.LimitLo * 10 - 1 self.pivot = True #self.status() # done return int(answer) n = int(raw_input()) for z in range(n): k = 0 try: k = int(raw_input()) except: break it = iter(PalindromeIterator(k)) while it.current() <= k: it.next() print it.current() sys.exit(0)
Thanks!
Posts: 4
Participants: 2
@lmoreno wrote:
why is it not working in the test?
#include <stdio.h> void main() {int *a,h; a=malloc(4); int b=0,x=1; while (b!=42) {scanf("%i",&b); a=realloc(a,x*sizeof(int)); a[x-1]=b;x++; } for (h=0;h<x-2;h++){printf("%i\n",a[h]);} }
Posts: 2
Participants: 2
@amruthp wrote:
Where can I get the solutions posted by others on a particular problem
Posts: 3
Participants: 3