任何人都可以帮助我使用下面的代码来解决这个问题http://www.spoj.com/problems/SBANK/。虽然测试表明代码按照要求正常工作,但我得到了错误的答案。请不要为我的帖子添加减号,因为我对Java很新。提前谢谢。
import java.util.*;
import java.io.*;
class Exercise
{
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
int pos;
String line;
int noTimes = 0;
int bTimes;
int first = 0;
int m;
// Creating an arraylist to store the result and then print it off
List<String> arr = new ArrayList<String>(100000);
// List used for carrying out the checks
List<Exercise> list = new LinkedList<Exercise>();
public String bankAccount;
public int counter;
// Object Template
public Exercise (String bank)
{
bankAccount = bank;
counter = 0;
}
// Filling the list and the arraylist
public void fillInList() throws IOException
{
line = input.readLine();
// No of test cases to follow
noTimes = Integer.parseInt(line);
if (noTimes <= 5)
{
for (int p = 1; p <= noTimes; p++)
{
line = input.readLine();
// No of Bank accounts
bTimes = Integer.parseInt(line);
if (bTimes < 100000)
{
for (int j = 1; j <= bTimes; j++)
{
// Read the next line
line = input.readLine();
// Putting the first object in the list
if (first == 0)
{
Exercise element = new Exercise(line);
list.add(element);
first++;
}
// Carrying out the checks
compare(line);
}
// Filling the first portion of items in the arraylist
fill();
// Creating space line for the next that follows
System.out.print("\n");
first = 0;
// Clearing the list items in preparation for the next set
list.clear();
}
}
}
}
public void compare(String lineT) throws IOException
{
for(int i = 0; i < list.size(); i++)
{
Exercise object = list.get(i);
int result = object.bankAccount.compareTo(lineT);
// If item already present then increment counter
if (result == 0)
{
object.counter++;
break;
}
// If the item is greater than the last one in the list then add it last
else if(result < 0 && i == list.size() - 1)
{
Exercise newElement = new Exercise (lineT);
list.add(newElement);
newElement.counter++;
break;
}
// If the input is less than item then add it right before it
else if(result > 0)
{
pos = list.indexOf(object);
Exercise newElement = new Exercise(lineT);
list.add(pos, newElement);
newElement.counter++;
break;
}
}
}
// Filling the array list
public void fill() throws IOException
{
for(int i = 0; i < list.size(); i++)
{
Exercise printObject = list.get(i);
arr.add(printObject.bankAccount + " " + printObject.counter);
}
arr.add("");
}
// Printing the arraylist
public void print() throws IOException
{
for(int i = 0; i < arr.size(); i++)
{
System.out.println(arr.get(i));
}
}
public static void main(String[] args) throws IOException
{
try
{
Exercise obj = new Exercise("test");
obj.fillInList();
obj.print();
}
catch(Exception e)
{
return;
}
}
}
我得到的结果(从eclipse复制粘贴):
2
6
03 10103538 2222 1233 6160 0142
03 10103538 2222 1233 6160 0141
30 10103538 2222 1233 6160 0141
30 10103538 2222 1233 6160 0142
30 10103538 2222 1233 6160 0141
30 10103538 2222 1233 6160 0142
5
30 10103538 2222 1233 6160 0144
30 10103538 2222 1233 6160 0142
30 10103538 2222 1233 6160 0145
30 10103538 2222 1233 6160 0146
30 10103538 2222 1233 6160 0143
03 10103538 2222 1233 6160 0141 1
03 10103538 2222 1233 6160 0142 1
30 10103538 2222 1233 6160 0141 2
30 10103538 2222 1233 6160 0142 2
30 10103538 2222 1233 6160 0142 1
30 10103538 2222 1233 6160 0143 1
30 10103538 2222 1233 6160 0144 1
30 10103538 2222 1233 6160 0145 1
30 10103538 2222 1233 6160 0146 1
答案 0 :(得分:0)
我快速浏览了两件事(没试过你的代码)
1)代码必须适用于最多100000个帐户,您的代码最多只适用于99999个帐户
2)您使用线性搜索在列表中查找帐号,并在get()
而不是ListIterator上使用(昂贵的)LinkedList
操作。这样,在最坏的情况下(列表已经排序),你有O(n³)
的性能,我怀疑在10秒的帐号中工作7秒。
尝试找到一个排序的数据结构,您可以在O(log n)
时间(而不是O(n²)
)进行“二分搜索”,并且仍然可以在恒定时间内插入条目,并且您的性能会提高很多。或者先对所有条目进行排序,然后消除重复项。
答案 1 :(得分:0)
......我不得不说,这是代码(尽管不是很好):
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class main
{
static final BufferedReader in=new BufferedReader(new
InputStreamReader(System.in));
public static void main(String args[]) throws IOException{
int num_of_test=getIntOrExit();
List<Map<String,Integer>> data=new ArrayList<Map<String,Integer>> (num_of_test);
for(int i=0;i<num_of_test;++i){
int num_of_id=getIntOrExit();
data.add(new HashMap<String,Integer> (num_of_id));
for(int j=0;j<num_of_id;++j){
String line=in.readLine();
/*to eliminate the wrong input*/
if(line.split("\\d\\d \\d\\d\\d\\d\\d\\d\\d\\d \\d\\d\\d\\d "
+"\\d\\d\\d\\d \\d\\d\\d\\d \\d\\d\\d\\d ").length!=0){
System.err.println("please follow the format:");
System.err.println("\"cc bbbbbbbb dddd dddd dddd dddd \"");
--j;
continue;
}
if(!data.get(i).containsKey(line)){
data.get(i).put(line,Integer.valueOf(0));
}
data.get(i).put(line,data.get(i).get(line)+1);
}
while(!in.readLine().equals("")){
System.err.println("please input empty line");;
}
}
for(int i=0;i<num_of_test;++i){
List<String> list=new ArrayList<String> (data.get(i).keySet());
java.util.Collections.sort(list);
for(String id:list){
System.out.printf("%s %d\n",id,data.get(i).get(id));
}
}
}
public static int getIntOrExit() throws IOException{
int output=0;
do{
try{
String input=in.readLine();
if(input.toLowerCase().equals("exit")){
System.exit(0);
}
output=Integer.parseInt(input);
break;
} catch(NumberFormatException nfe){
System.err.println("please input a number or \"exit\"");
continue;
}
} while(true);
return output;
}
}