but i got TLE in the contest
can any one give me any clue.....
thanx for advance.
Moderator: Board moderators
durjay wrote:I think this is a simple adhoc problem.....
but i got TLE in the contest![]()
can any one give me any clue.....
thanx for advance.

durjay wrote:I think this is a simple adhoc problem.....
but i got TLE in the contest![]()
can any one give me any clue.....
thanx for advance.
void cleanDocument( string dirty_document, vector<string> &clean_document ) {
string clean_word = "";
for ( unsigned int i = 0; i < dirty_document.size(); ++i )
if ( dirty_document[i] > 96 && dirty_document[i] < 123 )
clean_word += dirty_document[i];
else
if ( clean_word != "" ) {
clean_document.push_back( clean_word );
clean_word = "";
}
}
import java.io.ByteArrayInputStream;
import java.io.CharArrayReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Set;
import java.util.TreeSet;
class Main {
public static final String END = "END";
public static void main(String[] args) {
InputStreamReader reader = new InputStreamReader(System.in);
char[] buf = new char[1024 * 1024 * 9];
try {
reader.read(buf);
}
catch (IOException e) {
}
CharArrayReader bufReader = new CharArrayReader(buf);
int intch;
try {
while ((intch = bufReader.read()) != -1) {
if (intch == 0) {
break;
}
Integer count = null;
try {
count = Integer.valueOf(Character.valueOf((char) intch).toString());
}
catch (NumberFormatException e) {
continue;
}
for (int i = 1; i <= count; i++) {
int position = 0;
Map<String, PriorityQueue<Integer>> words = readTheWords(bufReader, position);
if (words == null) {
return;
}
PriorityQueue<Interval> resultQueue = new PriorityQueue<Interval>();
while (true) {
WordsDataStructure dataStructure = new WordsDataStructure();
for (Map.Entry<String, PriorityQueue<Integer>> wordAndPositions : words.entrySet()) {
dataStructure.addWord(wordAndPositions.getKey(), wordAndPositions.getValue().peek());
}
resultQueue.add(new Interval(dataStructure.minPosittion, dataStructure.maxPostition));
PriorityQueue<Integer> integers = words.get(dataStructure.maxWord);
if (integers.size() > 1) {
integers.poll();
}
else {
Interval poll = resultQueue.poll();
System.out.println("Document: " + i + ": " + poll.start + " " + poll.end);
break;
}
}
}
}
}
catch (IOException e) {
}
}
private static Map<String, PriorityQueue<Integer>> readTheWords(final Reader reader, int position)
throws IOException {
int intch;
Map<String, PriorityQueue<Integer>> words = new HashMap<String, PriorityQueue<Integer>>();
StringBuilder builder = new StringBuilder();
boolean isEnd = false;
while (!isEnd) {
intch = reader.read();
if (intch == -1) {
return null;
}
char c = (char) intch;
if (Character.isLetter(c)) {
builder.append(c);
}
else {
String value = builder.toString();
isEnd = value.equals(END);
if (builder.length() > 0 && !isEnd) {
PriorityQueue<Integer> priorityQueue = words.get(value);
if (priorityQueue == null) {
priorityQueue = new PriorityQueue<Integer>(10000, new Comparator<Integer>() {
public int compare(final Integer o1, final Integer o2) {
return o2.compareTo(o1);
}
});
words.put(value, priorityQueue);
}
priorityQueue.add(++position);
}
builder = new StringBuilder();
}
}
return words;
}
static class WordsDataStructure {
String maxWord;
int maxPostition = Integer.MIN_VALUE;
int minPosittion = Integer.MAX_VALUE;
public void addWord(String word, int position) {
if (position > maxPostition) {
maxPostition = position;
maxWord = word;
}
if (position < minPosittion) {
minPosittion = position;
}
}
}
static class Interval implements Comparable<Interval> {
final Integer start;
final Integer end;
Interval(final int start, final int end) {
this.end = end;
this.start = start;
}
public int compareTo(final Interval o) {
int compareResult = (end - start) - (o.end - o.start);
if (compareResult != 0) {
return compareResult;
}
return start.compareTo(o.start);
}
}
}
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.CharArrayReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.Reader;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Set;
import java.util.TreeSet;
class Main {
public static final String END = "END";
public static void main(String[] args) {
BufferedReader reader= new BufferedReader(new InputStreamReader(System.in),1024*1024*9);
BufferedWriter writer = new BufferedWriter(new PrintWriter(System.out));
int intch;
try {
while ((intch = reader.read()) != -1) {
if (intch == 0) {
break;
}
Integer count = null;
try {
count = Integer.valueOf(Character.valueOf((char) intch).toString());
}
catch (NumberFormatException e) {
continue;
}
for (int i = 1; i <= count; i++) {
int position = 0;
Map<String, PriorityQueue<Integer>> words = readTheWords(reader, position);
if (words == null) {
return;
}
PriorityQueue<Interval> resultQueue = new PriorityQueue<Interval>();
while (true) {
WordsDataStructure dataStructure = new WordsDataStructure();
for (Map.Entry<String, PriorityQueue<Integer>> wordAndPositions : words.entrySet()) {
dataStructure.addWord(wordAndPositions.getKey(), wordAndPositions.getValue().peek());
}
resultQueue.add(new Interval(dataStructure.minPosittion, dataStructure.maxPostition));
PriorityQueue<Integer> integers = words.get(dataStructure.maxWord);
if (integers.size() > 1) {
integers.poll();
}
else {
Interval poll = resultQueue.poll();
writer.write("Document: " + i + ": " + poll.start + " " + poll.end + "\n");
break;
}
}
}
}
writer.flush();
}
catch (IOException e) {
}
}
private static Map<String, PriorityQueue<Integer>> readTheWords(final Reader reader, int position)
throws IOException {
int intch;
Map<String, PriorityQueue<Integer>> words = new HashMap<String, PriorityQueue<Integer>>();
StringBuilder builder = new StringBuilder();
boolean isEnd = false;
while (!isEnd) {
intch = reader.read();
if (intch == -1) {
return words;
}
char c = (char) intch;
if (Character.isLetter(c)) {
builder.append(c);
}
else {
String value = builder.toString();
isEnd = value.equals(END);
if (builder.length() > 0 && !isEnd) {
PriorityQueue<Integer> priorityQueue = words.get(value);
if (priorityQueue == null) {
priorityQueue = new PriorityQueue<Integer>(10000, new Comparator<Integer>() {
public int compare(final Integer o1, final Integer o2) {
return o2.compareTo(o1);
}
});
words.put(value, priorityQueue);
}
priorityQueue.add(++position);
}
builder = new StringBuilder();
}
}
return words;
}
static class WordsDataStructure {
String maxWord;
int maxPostition = Integer.MIN_VALUE;
int minPosittion = Integer.MAX_VALUE;
public void addWord(String word, int position) {
if (position > maxPostition) {
maxPostition = position;
maxWord = word;
}
if (position < minPosittion) {
minPosittion = position;
}
}
}
static class Interval implements Comparable<Interval> {
final Integer start;
final Integer end;
Interval(final int start, final int end) {
this.end = end;
this.start = start;
}
public int compareTo(final Interval o) {
int compareResult = (end - start) - (o.end - o.start);
if (compareResult != 0) {
return compareResult;
}
return start.compareTo(o.start);
}
}
}
10
a b c d e
END
a b c d e
END
a b c d e
END
a b c d e
END
a b c d e
END
a b c d e
END
a b c d e
END
a b c d e
END
a b c d e
END
a b c d e
END
100Document 1: 1 5
Document 2: 1 5
Document 3: 1 5
Document 4: 1 5
Document 5: 1 5
Document 6: 1 5
Document 7: 1 5
Document 8: 1 5
Document 9: 1 5
Document 10: 1 5import java.io.*;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.PriorityQueue;
class Main {
public static final String END = "END";
public static void main(String[] args) {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in), 1024 * 1024 * 9);
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
int intch;
try {
while ((intch = reader.read()) != -1) {
if (intch == 0) {
break;
}
Integer count = null;
try {
StringBuilder builder = new StringBuilder();
char intch1 = (char) intch;
while (intch1 != '\n') {
builder.append(intch1);
intch1 = (char) reader.read();
}
count = Integer.valueOf(builder.toString().trim());
} catch (NumberFormatException e) {
continue;
}
for (int i = 1; i <= count; i++) {
int position = 0;
Map<String, PriorityQueue<Integer>> words = readTheWords(reader, position);
if (words == null) {
return;
}
PriorityQueue<Interval> resultQueue = new PriorityQueue<Interval>();
while (true) {
WordsDataStructure dataStructure = new WordsDataStructure();
for (Map.Entry<String, PriorityQueue<Integer>> wordAndPositions : words.entrySet()) {
dataStructure.addWord(wordAndPositions.getKey(), wordAndPositions.getValue().peek());
}
resultQueue.add(new Interval(dataStructure.minPosittion, dataStructure.maxPostition));
PriorityQueue<Integer> integers = words.get(dataStructure.maxWord);
if (integers.size() > 1) {
integers.poll();
} else {
Interval poll = resultQueue.poll();
writer.write("Document: " + i + ": " + poll.start + " " + poll.end + "\n");
break;
}
}
}
}
writer.flush();
} catch (IOException e) {
}
}
private static Map<String, PriorityQueue<Integer>> readTheWords(final Reader reader, int position)
throws IOException {
int intch;
Map<String, PriorityQueue<Integer>> words = new HashMap<String, PriorityQueue<Integer>>();
StringBuilder builder = new StringBuilder();
boolean isEnd = false;
while (!isEnd) {
intch = reader.read();
if (intch == -1) {
return words;
}
char c = (char) intch;
if (Character.isLetter(c)) {
builder.append(c);
}
else {
String value = builder.toString();
isEnd = value.equals(END);
if (builder.length() > 0 && !isEnd) {
PriorityQueue<Integer> priorityQueue = words.get(value);
if (priorityQueue == null) {
priorityQueue = new PriorityQueue<Integer>(10000, new Comparator<Integer>() {
public int compare(final Integer o1, final Integer o2) {
return o2.compareTo(o1);
}
});
words.put(value, priorityQueue);
}
priorityQueue.add(++position);
}
builder = new StringBuilder();
}
}
return words;
}
static class WordsDataStructure {
String maxWord;
int maxPostition = Integer.MIN_VALUE;
int minPosittion = Integer.MAX_VALUE;
public void addWord(String word, int position) {
if (position > maxPostition) {
maxPostition = position;
maxWord = word;
}
if (position < minPosittion) {
minPosittion = position;
}
}
}
static class Interval implements Comparable<Interval> {
final Integer start;
final Integer end;
Interval(final int start, final int end) {
this.end = end;
this.start = start;
}
public int compareTo(final Interval o) {
int compareResult = (end - start) - (o.end - o.start);
if (compareResult != 0) {
return compareResult;
}
return start.compareTo(o.start);
}
}
}
import java.io.*;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.PriorityQueue;
class Main {
public static final String END = "END";
public static void main(String[] args) {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
int intch;
try {
while ((intch = reader.read()) != -1) {
if (intch == 0) {
break;
}
Integer count = null;
try {
StringBuilder builder = new StringBuilder();
char intch1 = (char) intch;
while (intch1 != '\n') {
builder.append(intch1);
intch1 = (char) reader.read();
}
count = Integer.valueOf(builder.toString().trim());
} catch (NumberFormatException e) {
continue;
}
for (int i = 1; i <= count; i++) {
int position = 0;
Map<String, PriorityQueue<Integer>> words = readTheWords(reader, position);
if (words == null) {
return;
}
PriorityQueue<Interval> resultQueue = new PriorityQueue<Interval>();
while (true) {
WordsDataStructure dataStructure = new WordsDataStructure();
for (Map.Entry<String, PriorityQueue<Integer>> wordAndPositions : words.entrySet()) {
dataStructure.addWord(wordAndPositions.getKey(), wordAndPositions.getValue().peek());
}
resultQueue.add(new Interval(dataStructure.minPosittion, dataStructure.maxPostition));
PriorityQueue<Integer> integers = words.get(dataStructure.maxWord);
if (integers.size() > 1) {
integers.poll();
} else {
Interval poll = resultQueue.poll();
writer.write("Document: " + i + ": " + poll.start + " " + poll.end + "\n");
break;
}
}
}
writer.flush();
return;
}
} catch (IOException e) {
}
}
private static Map<String, PriorityQueue<Integer>> readTheWords(final Reader reader, int position)
throws IOException {
int intch;
Map<String, PriorityQueue<Integer>> words = new HashMap<String, PriorityQueue<Integer>>();
StringBuilder builder = new StringBuilder();
boolean isEnd = false;
while (!isEnd) {
intch = reader.read();
if (intch == -1) {
return words;
}
char c = (char) intch;
if (Character.isLetter(c)) {
builder.append(c);
} else {
String value = builder.toString();
isEnd = value.equals(END);
if (builder.length() > 0 && !isEnd) {
PriorityQueue<Integer> priorityQueue = words.get(value);
if (priorityQueue == null) {
priorityQueue = new PriorityQueue<Integer>(10000, new Comparator<Integer>() {
public int compare(final Integer o1, final Integer o2) {
return o2.compareTo(o1);
}
});
words.put(value, priorityQueue);
}
priorityQueue.add(++position);
}
builder = new StringBuilder();
}
}
return words;
}
static class WordsDataStructure {
String maxWord;
int maxPostition = Integer.MIN_VALUE;
int minPosittion = Integer.MAX_VALUE;
public void addWord(String word, int position) {
if (position > maxPostition) {
maxPostition = position;
maxWord = word;
}
if (position < minPosittion) {
minPosittion = position;
}
}
}
static class Interval implements Comparable<Interval> {
final Integer start;
final Integer end;
Interval(final int start, final int end) {
this.end = end;
this.start = start;
}
public int compareTo(final Interval o) {
int compareResult = (end - start) - (o.end - o.start);
if (compareResult != 0) {
return compareResult;
}
return start.compareTo(o.start);
}
}
}
Users browsing this forum: No registered users and 1 guest