Вы находитесь на странице: 1из 12

Programming Contest Preparation

Useful Libraries and Information


USACO High School Programming Competition Online Tests:
http://train.usaco.org/usacogate
ACM: online-judge.ua.es
!aa Collections
http://jaa.sun.com/docs/"oo#s/tutorial/collections/inter$aces/inde%.html
A set can onl& contain one o$ a particular element. There is also a sortedSet
'treeSet( that e%tends set and adds an ordering.
A list can contain more than one element and is sorted.
A map has #e&s and alues) li#e a hash ta"le. There is also a sortedMap
'treeMap( that e%tends a map and adds an ordering.
Collections use *enerics:
http://jaa.sun.com/docs/"oo#s/tutorial/jaa/generics/inde%.html
Set<String> s = new HashSet<String>();
Use$ul classes) ta#en $rom the AP CS A+ hand"oo#
'http://,,,.college"oard.com/prod-do,nloads/ap/students/compsci/./01.
2/3APCompSci-4oc#ed.pd$(
5interface java.util.List<E>
boolean add(E x)
int size()
E get(int index)
E set(int index, E x)
// replaces the element at index with x
// returns the element formerly at the specifed position
void add(int index, E x)
// inserts x at position index, sliding elements
// at position index and higher to the right
// (adds to their indices) and ad!usts size
E remove(int index)
// removes element from position index, sliding
// elements at position index and higher to the
// left (subtracts from their indices) and ad!usts size
// returns the element formerly at the specifed position
"terator#E$ iterator()
%ist"terator#E$ list"terator()
class ja va.util.ArrayList<E>
implements java.util.List<E>
class ja va.util.LinkedList<E>
implements java.util.List<E>, java.util.Queue<E>
&ethods in addition to the %ist methods'
void add(irst(E x)
void add%ast(E x)
E get(irst()
E get%ast()
E remove(irst()
E remove%ast()
interface java.util.Set<E>
boolean add(E x)
boolean contains()b!ect x)
boolean remove()b!ect x)
int size()
"terator#E$ iterator()
class ja va.util.HashSet<E>
implements java.util.Set<E>
class ja va.util.TreeSet<E>
implements java.util.Set<E>
interface java.util.ap<!,">
* put(+ ,ey, * value)
* get()b!ect ,ey)
* remove()b!ect ,ey)
boolean contains+ey()b!ect ,ey)
int size()
-et#+$ ,ey-et()
class ja va.util.Hashap<!,">
implements java.util.ap<!,">
class ja va.util.Treeap<!,">
implements java.util.ap<!,">
'Use treeMap i$ &ou ,ant a sorted Map) HashMap i$ &ou ,ant a $ast Map.
Same ,ith Set.(
interface java.util.#terat$r<E>
boolean has.ext()
E next()
void remove()
interface ja va.util.List#terat$r<E>
e%tends java.util.#terat$r<E>
&ethods in addition to the "terator methods
void add(E x)
void set(E x)
class java.util.Stack<E>
E push(E x)
E pop()
E pee,()
boolean isEmpty()
interface java.util.Queue<E>
boolean add(E x)
E remove()
E pee,()
boolean isEmpty()
class java.util.&ri$rityQueue<E>
boolean add(E x)
E remove()
E pee,()
boolean isEmpty()
!aa.util.6ector resi7a"le lin#ed list8si7e and capacit& can ar&
.add'inde%) o"j(
.add'o"ject(
.get'inde% i(
.remoe'inde% i(
is9mpt&'(
remoe'o"ject(
clear'(
Arra&4ists are similar to 6ectors.
:ou"le-ended ;ueue < Stac#s com"ined ,ith ;ueues
class jaa.util.de;ue=9> e%tends ;ueue - ne, ,ith !aa ?
Methods) $rom http://jaa.sun.com/jaase/?/docs/api/jaa/util/:e;ue.html
'special alue $unctions return null or $alse appropriatel&(:
First Element (Head) Last Element (Tail)
Throws exception Special value Throws exception Special value
Insert
addFirst(e) offerFirst(e) addLast(e) offerLast(e)
Remove
removeFirst() pollFirst() removeLast() pollLast()
Examine
getFirst() peekFirst() getLast() peekLast()
Collections implementations and methods) $rom the Sun !aa Collections
Oerie, Site)
http://jaa.sun.com/jaase/?/docs/technotes/guides/collections/inde%.html
:

Implementations
Has
Table
Resi!able
"rra#
$alan%ed
Tree
Lin&ed List
Has Table '
Lin&ed List
Interfa%es
(et
HashSet

TreeSet

LinkedHashSet
List
ArraList

LinkedList

)e*ue
Arra!e"#e

LinkedList

+ap
Hash$ap

Tree$ap

LinkedHash$ap
Collections sort(List) - Sorts a list using a merge sort algorithm)
,hich proides aerage-case per$ormance compara"le to a high-
;ualit& ;uic#sort) guaranteed O'n@log n( per$ormance 'unli#e
;uic#sort() and stability 'unli#e ;uic#sort(. 'A sta"le sort is one that
does not reorder e;ual elements.(
sort'4ist) comparator(
binar#(ear%(List, -b.e%t) - Searches $or an element in an ordered
list using the "inar& search algorithm.
reverse(List) - Aeerses the order o$ the elements in the a list.
suffle(List) - Aandoml& permutes the elements in a list.
fill(List, -b.e%t) - Oer,rites eer& element in a list ,ith the
speci$ied alue.
%op#(List dest, List sr%) - Copies the source list into the destination
list.
min(Colle%tion) - Aeturns the minimum element in a collection.
max(Colle%tion) - Aeturns the ma%imum element in a collection.
rotate(List list, int distan%e) - Aotates all o$ the elements in the list
"& the speci$ied distance.
repla%e"ll(List list, -b.e%t old/al, -b.e%t ne0/al) - Aeplaces all
occurrences o$ one speci$ied alue ,ith another.
index-f(ubList(List sour%e, List target) - Aeturns the inde% o$ the
$irst su"list o$ source that is e;ual to target.
lastIndex-f(ubList(List sour%e, List target) - Aeturns the inde% o$
the last su"list o$ source that is e;ual to target.
s0ap(List, int, int) - S,aps the elements at the speci$ied positions in
the speci$ied list.
fre*uen%#(Colle%tion, -b.e%t) - Counts the num"er o$ times the
speci$ied element occurs in the speci$ied collection.
dis.oint(Colle%tion, Colle%tion) - :etermines ,hether t,o
collections are disjoint) in other ,ords) ,hether the& contain no
elements in common.
add"ll(Colle%tion12 super T3, T444) - Adds all o$ the elements in the
speci$ied arra& to the speci$ied collection.
ne0(etFrom+ap(+ap) - Creates a general purpose Set
implementation $rom a general purpose $ap implementation.
asLifo5ueue()e*ue) - Aeturns a ie, o$ a !e"#e as a 4ast-in-$irst-
out '4i$o( %#e#e.
Aange-ie, operations on a sorted set
di&tionar's#(Set()f)* )g))'&lear();
's#(Set(+f,*,g,)'si-e()
dictionar&.headSet'o"ject n( up to n) e%clusie
dictionar&.tailSet'O"ject n(
.$irst'( last'(
Bor each-loops jaa.sun.com/j/se/C...3/docs/guide/language/$oreach.html
Dnstead o$
void &an&elAll(.olle&tion<TimerTask> &) /
for (0terator<TimerTask> i = &'iterator();
i'has1e2t(); )
i'ne2t()'&an&el();
3
we &an #se
void &an&elAll(.olle&tion<TimerTask> &) /
for (TimerTask t 4 &)
t'&an&el();
3
// a is an array of integers
for (int i 4 a)
res#lt 5= i;

:oes not ,or# $or collections i$ &ouEre remoing elements
9%ample ,ith a map:
for (6eTpe ke 4 m'keSet())
Sstem'o#t'println(ke);
and ,ith an iterator:
77 Filter a map (ased on some propert of its kes'
for (0terator<Tpe> it = m'keSet()'iterator();
it'has1e2t(); )
if (it'ne2t()'is8og#s())
it'remove();
Sorting an arra& 'code ta#en $rom
http://,,,.e%ampledepot.com/egs/jaa.util/p#g.html ) !aa :eeloperEs
Almanac(
Arras'sort(strArra*
String'.AS9:01S91S0T0;9:<=!9=);
77 >a* .* -?

77 =everse@order sort
Arras'sort(strArra*
.olle&tions'reverse<rder());
.onvert an arra to a .olle&tion
77 Fi2ed@si-e list
List list = Arras'asList(array);

77 Arowa(le list
list = new LinkedList(Arras'asList(array));

77 !#pli&ate elements are dis&arded
Set set = new HashSet(Arras'asList(array));
Conerting a Collection to an Arra&
.toArra&'( returns an arra& o$ O"jects. D$ &ou ,ant an arra& o$ a particular
#ind) &ou hae to cast it as $ollo,s.
77 .reate an arra &ontaining the elements in a
list
<(Be&t>? o(Be&tArra = list'toArra();
MyClass>? arra = (MyClass>?)list'toArra(new
MyClass>list'si-e()?);

77 .reate an arra &ontaining the elements in a
set
o(Be&tArra = set'toArra();
arra = (MyClass>?)set'toArra(new
MyClass>set'si-e()?);

77 .reate an arra &ontaining the kes in a map
o(Be&tArra = map'keSet()'toArra();
arra = (MyClass>?)map'keSet()'toArra(new
MyClass>set'si-e()?);

77 .reate an arra &ontaining the val#es in a
map
o(Be&tArra = map'val#es()'toArra();
arra = (MyClass>?)map'val#es()'toArra(new
MyClass>set'si-e()?);
Su"arra&s ,ith !aa ?
int>? newArra = Arras'&op<f(a* newLength);
p#(li& stati& int>? copyOfRange(int>? original*
int from*
int to)
$ath'=andom()
=andom'ne2t0nt(int) returns F3)int(
float nextFloat() F3.3)C.3G
(trings, Cara%ters, and I6-
Use the Scanner class $or reading input.
.ne%tDnt'( ne%t4ine'( hasHe%t'( ne%tBloat'(I
ne, Scanner'ne, Bile'5pro".inJ((I
-tring substring(int from, int to)
// returns the substring beginning at from
// and ending at to/
String charAt'char(
String8#ffer (#ffer = new String8#ffer(CDE);
1ew String(String8#ffer);
o#rString8#ffer'append(string);
String'toLower.ase() toFpper.ase() e"#als()
.an #se G.H IGAH 5 GaH to &onvert to lower &aseJor
.hara&ter'toLower.ase
.an also #se &hara&ter &odes as a(ove to inde2 an
arra of &hara&ters instead of intsJ(e &reativeK
'startsLith(string) trim()
'inde2<f(&har) 'inde2<f(string)
Hum"erBormat8can use $or output
Hum"erBormat n$ K ne, Hum"erBormat'(I
nf'set$inim#mFra&tion!igits( M );
nf'set$a2im#mFra&tion!igits( M );
nf'set$inim#m0nteger!igits( C );
nf'set$a2im#m0nteger!igits( CN );
string K n$.$ormat'int(I
9as& toString: concatenate starting ,ith a string)
Or an empt& string: 5J L &ourint < no toString needed
<f &o#rse o#Hre familiar with
0nteger'parse0nt(String) and related &lasses'
Character.is:igit'( .is4etterOr:igit'( .is4etter'( isSpace'(
String FG K &ourString.split'( instead o$ to#eni7er < uses regular e%pressions
Mou can use .split'( on the ne%t4ine'( $rom a Scanner.
Split'5NNsJ( splits ,ith spaces as separators
Ns stands $or spaces) NN is so the "ac#slash is recogni7ed
NS all "ut ,hitespace
jaa.sun.com/jaase/?/docs/api/jaa/util/rege%/Pattern.html
Fa"cG onl& characters a)")c
O negation
Fa-7A-PG inclusie range union
ACM/Programming Contest !udge Beed"ac#
Accepted 'AC(
Presentation 9rror 'P9( < The D/O is o$$. The&Ere pic#& ,ith this8"e
care$ul.
Accepted 'P9( < Accepted ,ith presentation error) li#e e%tra "lan# at end o$
line
Qrong Ans,er'QA( < +e sure to loo# at "oundar& cases and ensure &ouEre
soling the right pro"lem. Mou ,onEt "e a"le to #no, ,hat the judgeEs input
is to test.
Compile 9rror 'C9(
Auntime 9rror 'A9(
Time 4imit 9%ceeded 'T4( < Mou pro"a"l& ,onEt hae to ,orr& a"out this
$or the contest. Ma#e sure &ouEre outputting to S&stem.out.
Memor& 4imit 9%ceed 'M4(
Output 4imit 9%ceeded 'O4( < Bor this and the t,o a"oe) ,atch out $or
in$inite loops.
Aestricted Bunction 'AB( < Dn the real contest) some $unctions ma& "e
restricted) li#e +igDnteger. Chec# ,ith the judges $or details.
Su"mission 9rror 'S9( < Mou gae an incorrect user D: or pro"lem num"er
8the automated s&stem ,e use ta#es care o$ this.
Mou can hae more than one pro"lem ,ith &our program and onl& hae one
error message reported to &ou8thereEs a hierarch&. MouEll get a P9 "e$ore a
QA) $or e%ample.

Вам также может понравиться