Hello guys,
I am trying to solve Distinct Substrings http://www.spoj.com/problems/DISUBSTR/ problem on spoj.
but I am getting NZEC error. I am unable to find the bug in my code.
Here is the code:
package Distinct_substrings;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Scanner;
/**
* Created by amit on 31/1/15.
*/
public class Main {
public static void main(String args[]) throws Exception
{
try {
ArrayList<String> l;
int t;
//Scanner sc = new Scanner(System.in);
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
//t = sc.nextInt();
t = Integer.parseInt(in.readLine());
for (int k = 0; k < t; k++) {
int lcsum = 0;
int stringlen = 0;
l = new ArrayList<String>();
String temp;
// temp = sc.next();
temp = in.readLine();
stringlen = temp.length();
//System.out.println("Got: "+temp);
for (int i = temp.length() - 1; i >= 0; i--) {
l.add(temp.substring(i));
//System.out.println("Substring: "+temp.substring(i));
}
Collections.sort(l);
for (int i = 1; i < l.size(); i++) {
//System.out.println(l.get(i));
int lcp = 0;
String x, y;
x = l.get(i);
y = l.get(i - 1);
for (int j = 0; j < x.length() && j < y.length(); j++) {
if (x.charAt(j) != y.charAt(j))
break;
lcp++;
}
lcsum += lcp;
}
int ans = (stringlen * (stringlen - 1) / 2) + stringlen;
ans = ans - lcsum;
//System.out.println("LCP is:"+lcsum);
System.out.println(ans);
}
} catch (Exception e) {
e.printStackTrace();
return;
}
}
}
help me!
Thank you