Você está na página 1de 19

Sum

ario
1 Tabelas
2 Codigos
2.1 Exemplos . . . . . . . .
2.2 Teoria dos n
umeros . . .
2.3 Programac
ao Din
amica
2.4 Grafos . . . . . . . . . .
2.5 Geometria . . . . . . . .
2.6 Casamento de strings . .
2.7 Outros . . . . . . . . . .

17
18
19
20
21
22

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

3
3
8
9
10
15
16
16

23
24
25
26
27
28
29
30
31
32

3 Biblioteca C/C++
18
3.1 I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
3.2 Map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18

Lista de Tabelas
1
2
3
4
5
6

Limites de representac
ao de dados
Fatorial . . . . . . . . . . . . . . .
scanf() - %[*][width][modifiers]type
scanf() %[*][width][modifiers]type .
stdlib . . . . . . . . . . . . . . . .
math (angulos em radianos) . . . .

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

2
2
2
2
2
3

Modelo . . . . . . . . . . . . . . . . . . . . . . . . . . .
comparcao de ponto flutuante . . . . . . . . . . . . . . .
.vimrc para a configurac
ao do vim . . . . . . . . . . . .
func
ao que acelara o cin. N
ao deve ser usada com printf
printf . . . . . . . . . . . . . . . . . . . . . . . . . . . .
exemplo de map . . . . . . . . . . . . . . . . . . . . . .
exemplo de set e multset . . . . . . . . . . . . . . . . . .
exemplo de list . . . . . . . . . . . . . . . . . . . . . . .
exemplo de queue . . . . . . . . . . . . . . . . . . . . . .
exemplo de priority queue . . . . . . . . . . . . . . . . .
exemplo de stack . . . . . . . . . . . . . . . . . . . . . .
exemplo de vector . . . . . . . . . . . . . . . . . . . . .
exemplo de string . . . . . . . . . . . . . . . . . . . . . .
exemplo de stringstream . . . . . . . . . . . . . . . . . .
exemplo de ordenac
ao . . . . . . . . . . . . . . . . . . .
pesquisa bin
aria . . . . . . . . . . . . . . . . . . . . . . .

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

3
3
4
4
4
4
4
5
5
5
6
6
6
7
7
7

33
34
35
36
37
38
39
40
41
42
43

Algoritmos
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

Arredondamento e output em outras bases . . . . . . . . . . . . . . . . . . .


maximo divisor comum e mnimo multiplo comum . . . . . . . . . . . . . .
decide se um n
umero e primo . . . . . . . . . . . . . . . . . . . . . . . . . .
Retorna a fatorac
ao em n
umeros primos de abs(n). . . . . . . . . . . . . . .
Calcula Valor de ab mod n de forma r
apida. . . . . . . . . . . . . . . . . . .
Sub Set Sum: Verifica se h
a um sobconjunto dos elementos do vetor cuja
soma seja igual a soma pedida. . . . . . . . . . . . . . . . . . . . . . . . . .
Lis: longest increasing (decreasing) subsequence O(n2 ) . . . . . . . .
Lis: longest increasing subsequence O(n*logn) . . . . . . . . . . . . . .
Verifica se o grafo e aciclico. . . . . . . . . . . . . . . . . . . . . . . . . . . .
Dijkstra Caminho minimo 1 para todos pesos positivos. . . . . . . . . . . .
Floresta dijunta de arvores . . . . . . . . . . . . . . . . . . . . . . . . . . .
Kruskal Arvore geradora mnima kruskal . . . . . . . . . . . . . . . . . . .
verifica se um grafo e bipartido . . . . . . . . . . . . . . . . . . . . . . . . .
faz a ordenac
ao topol
ogica de um grafo acclico . . . . . . . . . . . . . . . .
calcula fluxo m
aximo, Ford-Fulkerson . . . . . . . . . . . . . . . . . . . .
calcula fluxo m
aximo, algoritmo mais eficiente porem muito maior em
tempo de codificac
ao . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
ponto e poligono . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Decide se q est
a sobre o segmento fechado pr. . . . . . . . . . . . . . . . . .
Decide se os segmentos fechados pq e rs tem pontos em comum. . . . . . . .
Calcula a dist
ancia do ponto r ao segmento pq. . . . . . . . . . . . . . . . .
Classifica o ponto p em relac
ao ao polgono T. Retorna 0, -1 ou 1 dependendo se p est
a no exterior, na fronteira ou no interior de T, respectivamente.
String matching - Algoritmo KMP - O( n + m) . . . . . . . . . . . . . . .
josephus problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Simplex . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Gera as permutac
oes dos elementos da string . . . . . . . . . . . . . . . . .
Ignora os dois floats do meio. Retornar
a 2 no sucesso. . . . . . . . . . . . .
Referencias map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

8
8
8
8
8
9
9
9
10
10
10
11
12
12
12
13
15
15
15
16
16
16
16
17
18
18
18

Tabelas

tipo
char
signed char
unsigned char
short
unsigned short
int
unsigned int
int64 t
uint64 t

bits
8
8
8
16
16
32
32
64
64

min...max
0..127
-128..127
0..255
-32.768 .. 32.767
0 .. 65.535
-2x10**9 .. 2 x 10**9
0 .. 4x10**9
-9 x 10**18 .. 9 x 10**18
0 .. 18 x 10**18

precisao
2
2
2
4
4
9
9
18
19

Tipo
char
int
float
int (octal)
int (hexa)
uint
char*

%
c
d
e, E, f, g, G
o
x, X
u
s
Tabela 3: scanf() - %[*][width][modifiers]type

Tabela 1: Limites de representacao de dados

0! = 1
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
6! = 720
7! = 5.040
8! = 40.320
9! = 362.880
10! = 3.628.800
11! = 39.916.800
12! = 479.001.600 [limite do (unsigned) int]
13! = 6.227.020.800
14! = 87.178.291.200
15! = 1.307.674.368.000
16! = 20.922.789.888.000
17! = 355.687.428.096.000
18! = 6.402.373.705.728.000
19! = 121.645.100.408.832.000
20! = 2.432.902.008.176.640.000 [limite do (u)int64 t]

modifiers
h
l
ll
L

tipo
short int (d, i, n), or unsigned short int (o, u, x)
long int (d, i, n), or unsigned long int (o, u, x), or double (e, f, g)
long long int (d, i, n), or unsigned long long int (o, u, x)
long double (e, f, g)
Tabela 4: scanf() %[*][width][modifiers]type

funcao
atof
atoi
atol
strtod
strtol
strtoul

Tabela 2: Fatorial

descricao
Convert string
Convert string
Convert string
Convert string
Convert string
Convert string

to
to
to
to
to
to

double
integer
long integer
double
long integer
unsigned long integer
Tabela 5: stdlib

Codigos

2.1

Exemplos
C
odigo 1: Modelo

1
2
3
4

#include
#include
#include
#include

<s t d i o . h>
< s t d l i b . h>
< s t r i n g . h>
<math . h>

func
ao
cos
sin
tan
acos
asin
atan
atan2
cosh
sinh
tanh
exp
frexp
ldexp
log
log10
modf
pow
sqrt
ceil
fabs
floor
fmod

descric
ao
Compute cosine
Compute sine
Compute tangent
Compute arc cosine
Compute arc sine
Compute arc tangent
Compute arc tangent with two parameters
Compute hyperbolic cosine
Compute hyperbolic sine
Compute hyperbolic tangent
Compute exponential function
Get significand and exponent
Generate number from significand and exponent
Compute natural logarithm
Compute common logarithm
Break into fractional and integral parts
Raise to power
Compute square root
Round up value
Compute absolute value
Round down value
Compute remainder of division

7
8

#include <i n t t y p e s . h>


#include <c t y p e . h>
#include < l i m i t s . h>

9
10
11
12

#include <a l g o r i t h m >


#include < u t i l i t y >
#include <i o s t r e a m >

13
14
15
16
17
18
19

#include
#include
#include
#include
#include
#include

<map>
<s e t >
<v e c t o r >
<l i s t >
<queue>
<s s t r e a m >

20
21

u s i n g namespace s t d ;

22
23

#define abs ( a ) ( ( a ) > 0 ? ( a ) : (a ) )

24
25
26
27

i n t main ( )
{
int n ;

28

c i n >> n ;

29
30

f o r ( i n t i = 0 ; i < n ; i ++)
{

31
32
33

34
35

while ( c i n >> n )
{

36

Tabela 6: math (angulos em radianos)

37
38

}
return 0 ;

39
40
41

C
odigo 2: comparcao de ponto flutuante
1
2
3
4

const double EPS = 1 e 10;


/
1 s e x < y
0 se x = y
1 se x > y

6
7
8
9
10

/
i n l i n e i n t cmp ( double x , double y = 0 , double t o l = EPS)
{
return ( x <= y + t o l ) ? ( x + t o l < y ) ? 1 : 0 : 1 ;
}

map<char , int> mymap ;


map<char , int > : : i t e r a t o r i t ;
p a i r <map<char , int > : : i t e r a t o r , b o o l > r e t ;

7
8
9
10

// f i r s t i n s e r t f u n c t i o n v e r s i o n ( s i n g l e parameter ) :
mymap . i n s e r t ( p a i r <char , int >(a , 1 0 0 ) ) ;
mymap . i n s e r t ( p a i r <char , int >(z , 2 0 0 ) ) ;

11
12
13
14

C
odigo 3: .vimrc para a configuracao do vim
1
2

r e t=mymap . i n s e r t ( p a i r <char , int >(z , 5 0 0 ) ) ;


i f ( r e t . s e c o n d==f a l s e )
{
c o u t << " element z already existed " ;
c o u t << " with a value of " << r e t . f i r s t >s e c o n d << e n d l ;
}

15

s e t a i n o e t t s =4 sw=4 bs=2
set cindent

16
17
18
19
20

C
odigo 4: func
ao que acelara o cin. N
ao deve ser usada com printf
1

21

std : : cout . s y n c w i t h s t d i o ( f a l s e ) ;

// t h i r d i n s e r t f u n c t i o n v e r s i o n ( r ange i n s e r t i o n ) :
map<char , int> anothermap ;
anothermap . i n s e r t (mymap . b e g i n ( ) ,mymap . f i n d ( c ) ) ;

22
23
24
25

C
odigo 5: printf

// showing c o n t e n t s :
c o u t << " mymap contains :\ n " ;
f o r ( i t=mymap . b e g i n ( ) ; i t != mymap . end ( ) ; i t++ )
c o u t << ( i t ) . f i r s t << " = > " << ( i t ) . s e c o n d << e n d l ;

26
1
2

/ p r i n t f example /
#include <s t d i o . h>

27
28

3
4
5
6
7
8
9
10

11

12
13
14
15
16
17
18
19
20
21
22
23
24
25

29

i n t main ( )
{
p r i n t f ( " Characters : % c % c \ n " , a , 6 5 ) ;
p r i n t f ( " Decimals : % d % ld \ n " , 1 9 7 7 , 650000L) ;
p r i n t f ( " Preceding with blanks : %10 d \ n " , 1 9 7 7 ) ;
p r i n t f ( " Preceding with zeros : %010 d \ n " , 1 9 7 7 ) ;
p r i n t f ( " Some different radixes : % d % x % o %# x %# o \ n " , 1 0 0 , 1 0 0 , 1 0 0 ,
100 , 100) ;
p r i n t f ( " floats : %4.2 f %+.0 e % E %4.2 f \ n " , 3 . 1 4 1 6 , 3 . 1 4 1 6 , 3 . 1 4 1 6 ,
3.1) ;
p r i n t f ( " Width trick : %* d \ n " , 5 , 1 0 ) ;
p r i n t f ( " % s \ n " , " A string " ) ;
return 0 ;
}
/ %[ f l a g s ( , +, e t c ) ] [ w i d t h ] [ . p r e c i s i o n ] [ l e n g t h ( h , l , L) ] s p e c i f i e r
Characters : a A
Decimals : 1977 650000
Preceding with blanks :
1977
P r e c e d i n g w i t h z e r o s : 0000001977
Some d i f f e r e n t r a d i x e s : 100 64 144 0 x64 0144
f l o a t s : 3 . 1 4 +3e+000 3 . 1 4 1 6 0 0E+000 3 . 1 0
Width t r i c k :
10
A string
/

30

32
33
34
35

37
38
39
40

41
42

44

2
3

#include <i o s t r e a m >


#include <s e t >
u s i n g namespace s t d ;

7
8

#include <i o s t r e a m >


#include <map>
u s i n g namespace s t d ;

i n t main ( )
{
m u l t i s e t <int> m y m u l t i s e t ;
m u l t i s e t <int > : : i t e r a t o r i t ;

9
10
11

C
odigo 7: exemplo de set e multset
1

C
odigo 6: exemplo de map

return 0 ;

43

while ( ! mymap . empty ( ) )


{
c o u t << mymap . b e g i n ( )> f i r s t << " = > " ;
c o u t << mymap . b e g i n ( )>s e c o n d << e n d l ;
map<char , int > : : i t e r a t o r e r a s e d e l e m e n t = mymap . e r a s e (mymap . b e g i n ( ) )
;
}

36

map<char , s t r i n g > mymap ;


mymap [ a ]= " an element " ;
i f (mymap . count ( a ) > 0 )
c o u t << mymap [ a ] << " is an element of mymap .\ n " ;

31

// s e t some i n i t i a l v a l u e s :
f o r ( i n t i =1; i <=5; i ++) m y m u l t i s e t . i n s e r t ( i 1 0 ) ;

// 10 20 30 40 50

12

i n t main ( )
{

13

14

c o u t << " size : " << ( i n t ) m y m u l t i s e t . s i z e ( ) << e n d l ;


c o u t << " count : " << ( i n t ) m y m u l t i s e t . count ( 1 0 ) << e n d l ;

35

15

i t=m y m u l t i s e t . f i n d ( 2 0 ) ;
mymultiset . e r a s e ( i t ) ;

16
17

return 0 ;

36
37

18

i f ( ! m y m u l t i s e t . empty )
mymultiset . e r a s e ( mymultiset . f i n d (40) ) ;

19
20

C
odigo 9: exemplo de queue

21

f o r ( i t=m y m u l t i s e t . b e g i n ( ) ; i t != m y m u l t i s e t . end ( ) ; i t ++)


c o u t << " " << i t ;

22
23

1
2
3

24

i n t myints [ ] = { 1 9 , 7 2 , 4 , 3 6 , 2 0 , 2 0 } ;
m u l t i s e t <int> f i r s t ( myints , myints +3) ;
m u l t i s e t <int> s e c o n d ( myints +3 , myints +6) ;

25
26
27

// 4 , 1 9 , 7 2
// 2 0 , 2 0 , 3 6

5
6
7

28

f i r s t . swap ( s e c o n d ) ; // t r o c a c o n t e u d o . o p r i m e i r o f i c a [ 2 0 , 2 0 , 3 6 ] e o
segundo [ 4 , 1 9 , 7 2 ]

29

i n t main ( )
{
queue<int> myqueue ;
i n t sum ( 0 ) ;

f o r ( i n t i =1; i <=10; i ++) myqueue . push ( i ) ;

10

30

return 0 ;

31
32

#include <i o s t r e a m >


#include <queue>
u s i n g namespace s t d ;

11

myqueue . back ( ) = myqueue . f r o n t ( ) ;

12
13

c o u t << " size : " << ( i n t ) myqueue . s i z e ( ) << e n d l ;

14
15

C
odigo 8: exemplo de list
1
2
3

17
18
19

4
5
6
7
8
9

while ( ! myqueue . empty ( ) )


{
sum += myqueue . f r o n t ( ) ;
myqueue . pop ( ) ;
}

16

#include <i o s t r e a m >


#include < l i s t >
u s i n g namespace s t d ;

20

i n t main ( )
{
l i s t <int> m y l i s t ( 2 , 1 0 0 ) ;
mylist . push front (200) ;
m y l i s t . push back (300) ;

21

c o u t << " total : " << sum << e n d l ;

22

// two i n t s w i t h a v a l u e o f 100

23

return 0 ;

24
25

10
11
12
13

i t = mylist . begin () ;
mylist . insert ( it ,10) ;
m y l i s t . i n s e r t ( i t , 2 , 2 0 ) ; // two i n t s w i t h a v a l u e o f 20

C
odigo 10: exemplo de priority queue

14
15

m y l i s t . r e v e r s e ( ) ; // R e v e r s e s t h e o r d e r o f t h e e l e m e n t s i n t h e l i s t .

16
17
18
19

c o u t << " mylist contains : " ;


f o r ( l i s t <int > : : i t e r a t o r i t=m y l i s t . b e g i n ( ) ; i t != m y l i s t . end ( ) ; ++i t )
c o u t << " " << i t ;

c o u t << " Popping out the elements in mylist : " ;


while ( ! m y l i s t . empty ( ) )
{
c o u t << " " << m y l i s t . f r o n t ( ) ;
mylist . pop front () ;
}

5
6

20
21
22
23
24
25
26

9
10
11
12

29
30
31
32

14

while ( ! m y l i s t . empty ( ) )
{
c o u t << " " << m y l i s t . back ( ) ;
m y l i s t . pop back ( ) ;
}

16
17
18
19
20

c o u t << m y l i s t . s i z e ( ) << e n d l ;

mypq . push ( 3 0 ) ;
mypq . push ( 1 0 0 ) ;
mypq . push ( 2 5 ) ;
mypq . push ( 4 0 ) ;
c o u t << " size : " << ( i n t ) mypq . s i z e ( ) << e n d l ;

15

33
34

i n t main ( )
{
p r i o r i t y q u e u e <int> mypq ;

13

27
28

#include <i o s t r e a m >


#include <queue>
u s i n g namespace s t d ;

21

c o u t << " Popping out elements ... " ;


while ( ! mypq . empty ( ) )
{
c o u t << " " << mypq . top ( ) ;
mypq . pop ( ) ;
}

22

c o u t << e n d l ;

21

return 0 ;

22

v e c t o r <int> i n i t i a l i z e r ( myarray / P o i n t e r I n i c i o / , myarray+s i z e o f (


myarray ) / s i z e o f ( i n t ) / PointerFim / ) ;
myvector . i n s e r t ( i t +2 , a n o t h e r v e c t o r . b e g i n ( ) , a n o t h e r v e c t o r . end ( ) ) ;
myvector . i n s e r t ( myvector . b e g i n ( ) , myarray , myarray +3) ;

23
24
25

23
24

c o u t << " myvector contains : " ;


f o r ( i t=myvector . b e g i n ( ) ; i t <myvector . end ( ) ; i t ++)
c o u t << " " << i t ;
c o u t << e n d l ;

25
26

C
odigo 11: exemplo de stack
1
2
3

27
28

#include <i o s t r e a m >


#include <s t a c k >
u s i n g namespace s t d ;

29

5
6
7
8

// e r a s e t h e 6 t h e l e m e n t
myvector . e r a s e ( myvector . b e g i n ( ) +5) ;
i n t sum ;
while ( ! myvector . empty ( ) )
{
sum += myvector . back ( ) ;
myvector . pop back ( ) ;
}

30
31

32

i n t main ( )
{
s t a c k <int> mystack ;
i n t sum = 0 ;

33
34
35
36

37

mystack . push ( 1 0 ) ;
mystack . push ( 2 0 ) ;

10
11

38

return 0 ;

39

12

40

mystack . top ( ) = 5 ;

13

14

while ( ! mystack . empty ( ) )


{
sum += mystack . top ( ) ;
mystack . pop ( ) ;
}

15
16
17
18
19

C
odigo 13: exemplo de string
1
2
3

20

c o u t << " size : " << ( i n t ) mystack . s i z e ( ) << e n d l ;

21

22

return 0 ;

23
24

#include <i o s t r e a m >


#include <s t r i n g >
u s i n g namespace s t d ;

8
9

i n t main
{
string
string
size t

()
s t r ( " There are two needles in this haystack with needles . " ) ;
s t r 2 ( " needle " ) ;
found ;

10
11

C
odigo 12: exemplo de vector
1
2
3

12

#include <i o s t r e a m >


#include <v e c t o r >
u s i n g namespace s t d ;

13
14
15

4
5
6
7
8

16

i n t main ( )
{
v e c t o r <int> myvector ( 3 , 1 0 0 ) ; // (100 100 100)
v e c t o r <int > : : i t e r a t o r i t ;

17
18

20
21

myvector . r e s e r v e ( 1 0 0 ) ;

22

11
12
13

16
17

24

i t = myvector . b e g i n ( ) ;
i t = myvector . i n s e r t ( i t , 200 ) ;
myvector . i n s e r t ( i t , 2 , 3 0 0 ) ;

27

v e c t o r <int> a n o t h e r v e c t o r ( 2 , 4 0 0 ) ;
i n t myarray [ ] = { 5 0 1 , 5 0 2 , 5 0 3 } ;

31

25
26

28
29

18
19
20

found=s t r . f i n d ( " haystack " ) ;


i f ( found != s t r i n g : : npos )
c o u t << " haystack also found at : " << i n t ( found ) << e n d l ;

23

f o r ( i n t i =0; i <myvector . s i z e ( ) ; i ++)


myvector . a t ( i )=i ; // = myvector [ i ] = i

14
15

found=s t r . f i n d ( " needles are small " , found +1 ,6) ;


i f ( found != s t r i n g : : npos )
c o u t << " second needle found at : " << i n t ( found ) << e n d l ;

19

9
10

// d i f f e r e n t member v e r s i o n s o f f i n d i n t h e same o r d e r as a b o v e :
found=s t r . f i n d ( s t r 2 ) ;
i f ( found != s t r i n g : : npos )
c o u t << " first needle found at : " << i n t ( found ) << e n d l ;

30

32

found=s t r . f i n d ( . ) ;
i f ( found != s t r i n g : : npos )
c o u t << " Period found at : " << i n t ( found ) << e n d l ;
// l e t s r e p l a c e t h e f i r s t n e e d l e :
s t r . r e p l a c e ( s t r . f i n d ( s t r 2 ) , s t r 2 . l e n g t h ( ) , " preposition " ) ;
c o u t << s t r << e n d l ;
s t r i n g s t r=" We think in generalities , but we live in details . " ;

// q u o t i n g A l f r e d N. Whitehead

33

string str2 , str3 ;


s i z e t pos ;

34
35

4
5

36

37

s t r 2 = s t r . s u b s t r ( 1 2 , 1 2 ) ; // g e n e r a l i t i e s

pos = s t r . f i n d ( " live " ) ;


s t r 3 = s t r . s u b s t r ( pos ) ;

38

39
40

// p o s i t i o n o f l i v e i n s t r
// g e t from l i v e t o t h e end

10

41

12

43

13

44

14

return 0 ;

45

47
48
49
50
51
52
53
54

b o o l m y f u n c t i o n ( i n t i , i n t j ) { return ( i <j ) ; }
struct m y c l a s s {
b o o l o p e r a t o r ( ) ( i n t i , i n t j ) { return ( i <j ) ; }
} myobject ;

11

c o u t << s t r 2 << << s t r 3 << e n d l ;

42

46

#include <v e c t o r >


u s i n g namespace s t d ;

15

}
/
f i r s t n e e d l e found a t : 14
s ec o nd n e e d l e found a t : 44
h a y s t a c k a l s o found a t : 30
P e r i o d found a t : 51
There a r e two p r e p o s i t i o n s i n t h i s h a y s t a c k w i t h n e e d l e s .
g e n e r a l i t i e s l i v e in d e t a i l s .
/

i n t compare ( const void a , const void b )


{
return ( ( i n t ) a ( i n t ) b ) ;
}

16
17
18
19
20

i n t main ( ) {
i n t myints [ ] = { 3 2 , 7 1 , 1 2 , 4 5 , 2 6 , 8 0 , 5 3 , 3 3 } ;
v e c t o r <int> myvector ( myints , myints +8) ;
26 80 53 33

21

// u s i n g d e f a u l t comparison ( o p e r a t o r <) :
s o r t ( myvector . b e g i n ( ) , myvector . b e g i n ( ) +4) ;
// (12 32 45 71)
26 80 53 33
// u s i n g f u n c t i o n as comp
s o r t ( myvector . b e g i n ( ) +4 , myvector . end ( ) , m y f u n c t i o n ) ; // 12 32 45
71(26 33 53 80)
// u s i n g o b j e c t as comp
s o r t ( myvector . b e g i n ( ) , myvector . end ( ) , myobject ) ;
// (12 26 32 33
45 53 71 80)

22
23

24
25

C
odigo 14: exemplo de stringstream
1
2
3

#include <s t r i n g >


#include <s s t r e a m >
#include <i o s t r e a m >

26
27

28

5
6

30
31

10
11
12
13
14
15
16
17

// i f s t a b l e i s need
s t a b l e s o r t ( myvector . b e g i n ( ) , myvector . end ( ) , m y f u n c t i o n ) ;

29

u s i n g namespace s t d ;

// Rearranges t h e e l e m e n t s i n t h e range [ f i r s t , l a s t ) , i n such a way


t h a t the subrange [ f i r s t , middle )
// c o n t a i n s t h e s m a l l e s t e l e m e n t s o f t h e e n t i r e rang e s o r t e d i n
a s c e n d i n g order , and t h e s u b r a n g e
// [ middle , end ) c o n t a i n s t h e r e m a i n i n g e l e m e n t s w i t h o u t any s p e c i f i c
order .
p a r t i a l s o r t ( myvector . b e g i n ( ) , myvector . b e g i n ( ) +3 , myvector . end ( ) ) ;

32

i n t main ( ) {
s t r i n g l i n h a=" Olah mundo " ;
s t r i n g s t r e a m s e p a r a d o r ( l i n h a ) ; // T o k e n i z a d o r
s t r i n g word ;
s e p a r a d o r >> word ; // word=Olah
char mu [ 4 ] ;
s e p a r a d o r . readsome (mu, 3 ) ; // Le mu ( i n c l u i e s p a c o )
c o u t << s e p a r a d o r . t e l l g ( ) << e n d l ; // 7 ( p o s i c a o de l e i t u r a )
c o u t << s e p a r a d o r . t e l l p ( ) << e n d l ; // 0 , p o s i c a o de e s c r i t a

33

34

35
36

q s o r t ( myints , 8 , s i z e o f ( i n t ) , compare ) ;

37
38

18

return 0 ;

39

separador . seekp ( separador . s t r ( ) . s i z e ( ) ) ;


s e p a r a d o r << " cruel " ; // s e p a r a d o r = Olah mundo c r u e l

19
20

40

21

separador . seekp (5) ;


s e p a r a d o r << " doido " ; // s e p a r a d o r = Olah d o i d o c r u e l ( s o b r e s c r i t o )
return 0 ;

22
23
24
25

C
odigo 16: pesquisa bin
aria
1

2
3
4
5

C
odigo 15: exemplo de ordenacao
1
2

// 32 71 12 45

#include <i o s t r e a m >


#include <a l g o r i t h m >

i n t compareMyType ( const void a , const void b )


{
i f ( (MyType ) a > (MyType ) b ) return 1 ;
i f ( (MyType ) a == (MyType ) b ) return 0 ;
i f ( (MyType ) a < (MyType ) b ) return 1;
}

i n t key = 4 0 ;

Codigo 18: m
aximo divisor comum e mnimo multiplo comum

item = ( i n t ) b s e a r c h (&key , v a l u e s , n , s i z e o f ( i n t ) , compareMyType ) ;

1
2
3

C
odigo 17: Arredondamento e output em outras bases

4
1
2
3

#include <i o s t r e a m >


#include <iomanip> // s e t p r e c i s i o n ( )
u s i n g namespace s t d ;

5
6
7

4
5
6
7
8

i n t main
double
double
double

() {
a = 3.1415926534;
b = 2006.0;
c = 1 . 0 e 10;

i n t gcd ( i n t x , i n t y )
{
return y ? gcd ( y , x % y ) : abs ( x ) ;
}
u i n t 6 4 t lcm ( i n t x , i n t y )
{
i f ( x && y ) return abs ( x ) / gcd ( x , y ) u i n t 6 4 t ( abs ( y ) ) ;
e l s e return u i n t 6 4 t ( abs ( x | y ) ) ;
}

C
odigo 19: decide se um n
umero e primo

9
10
11
12
13
14

// s e t p r e c i s i o n ( 1 ) => 1 c a s a d e c i m a l apos a v i r g u l a
c o u t << f i x e d << s e t p r e c i s i o n ( 1 ) << 9 . 0 9 0 9 0 9 0 1 << e n d l ;
c o u t << f i x e d << s e t p r e c i s i o n ( 2 ) << 9 . 0 9 0 9 0 9 0 1 << e n d l ;
c o u t << f i x e d << s e t p r e c i s i o n ( 3 ) << 9 . 0 9 0 9 0 9 0 1 << e n d l ;
c o u t << f i x e d << s e t p r e c i s i o n ( 2 ) << 9 . 1 << e n d l ;

1
2
3
4
5

15
16
17

20

23
24

8
9

// 5 d i g i t o s no maximo
cout . p r e c i s i o n (5) ;

10
11
12

c o u t << a << \ t << b << \ t << c << e n d l ;


c o u t << f i x e d << a << \ t << b << \ t << c << e n d l ;
c o u t << s c i e n t i f i c << a << \ t << b << \ t << c << e n d l ;

13

25
26

27
28
29
30

// S e t s t h e
oct .
i n t n =70;
c o u t << dec
c o u t << hex
c o u t << o c t

Codigo 20: Retorna a fatorac


ao em n
umeros primos de abs(n).

b a s e f i e l d f o r m a t f l a g f o r t h e s t r stream t o dec , hex or


1
2

<< n << e n d l ;
<< n << e n d l ;
<< n << e n d l ;

3
4
5

31
32
33
34
35
36
37
38
39
40
41
42
43
44
45

return 0 ;

}
/ o u t p u t
9.1
9.09
9.091
9.10
3 . 1 4 1 6 2006
1e10
3.14159 2006.00000
0.00000
3 . 1 4 1 5 9 e+00
2 . 0 0 6 0 0 e+03
1 . 0 0 0 0 0 e10
70
46
106
/

8
9

typedef map<int , int> prime map ;


void s q u e e z e ( prime map& M, i n t& n , i n t p )
{
f o r ( ; n % p == 0 ; n /= p ) M[ p]++;
}
void f a c t o r ( i n t n , prime map& M)
{
i f ( n < 0 ) { f a c t o r (n , M) ; return ; }
i f ( n < 2 ) return ;

10

s q u e e z e (M, n , 2 ) ;
s q u e e z e (M, n , 3 ) ;

11
12
13

i n t maxP = s q r t ( n ) + 2 ;
f o r ( i n t p = 5 ; p < maxP ; p += 6 )
{
s q u e e z e (M, n , p ) ;
s q u e e z e (M, n , p+2) ;
}
i f ( n > 1 ) M[ n]++;

14
15
16
17
18
19
20
21

2.2

( n < 0 ) return i s P r i m e (n ) ;
( n == 1 ) return t r u e ;
( n < 5 | | n % 2 == 0 | | n % 3 == 0 ) return ( n == 2 | | n == 3 ) ;

i n t maxP = s q r t ( n ) + 2 ;
f o r ( i n t p = 5 ; p < maxP ; p += 6 )
{
i f ( n % p == 0 | | n % ( p+2) == 0 ) return f a l s e ;
}
return t r u e ;

21
22

isPrime ( int n )

// a n u l a o e f e i t o de s e t p r e c i s i o n
cout . u n s e t f ( i o s : : f l o a t f i e l d ) ;

18
19

bool
{
if
if
if

Codigo 21: Calcula Valor de ab mod n de forma r


apida.

Teoria dos n
umeros
8

i n t mpow( i n t a , i n t b , i n t n = 1 0 )
{
i f ( b == 0 )
return 1 ;
else {
long long r e s = mpow( a , b / 2 , n ) ;
res = ( res res ) % n;
i f ( b%2 == 1 )
r e s = ( r e s a ) % n ;
return ( i n t ) r e s ;
}
}

1
2
3
4
5
6
7
8
9
10
11
12

12
13
14
15
16
17

19

21
22

25

Programac
ao Din
amica

26
27
28

C
odigo 22: Sub Set Sum: Verifica se h
a um sobconjunto dos elementos do vetor cuja
soma seja igual a soma pedida.

29
30
31

2
3
4
5

//soma maxima dos e l e m e n t o s do v e t o r


#define MAX SUM 10000
int n ;
i n t v e t [TAM] ;
b o o l m[MAX SUM ] ;

32
33
34

8
9
10
11

36
37

//M>soma maxima dos e l e m e n t o s do v e t o r c>soma p r o c u r a d a


b o o l subSetSum ( i n t M, i n t c )
{
f o r ( i n t i = 0 ; i <= M; i ++) m[ i ] = f a l s e ;
m[ 0 ] = t r u e ;

38
39

42

15
16
17
18
19

44
45
46

48

1
2
3
4

C
odigo 23: Lis: longest increasing (decreasing) subsequence O(n2 )

#define TAM 10000


i n t c [TAM] ;
i n t A[TAM] ;
i n t H[TAM] ;

8
9

7
8
9
10

10

void s s c t f ( i n t n )
{
f o r ( i n t m = 1 ; m <= n ; m++)
{
c [m] = H[m ] ;

// L o n g e s t I n c r e a s i n g S u b s e q u e n c e LIS O( n l o g n )
#define f o r i ( i , n ) f o r ( i n t i = 0 ; i < ( n ) ; ++i )
void l i s ( const v e c t o r < i n t > & v , v e c t o r < i n t > & asw )
{
v e c t o r <int> pd ( v . s i z e ( ) , 0 ) , p d i n d e x ( v . s i z e ( ) ) , pred ( v . s i z e ( ) ) ;
i n t maxi = 0 , x , j , i n d ;

5
6

Codigo 24: Lis: longest increasing subsequence O(n*logn)

return m[ c ] ;

21

return max ;

47

20

f o r ( i n t i = 1 ; i <= n ; i ++)
i f (max < c [ i ] )
max = c [ i ] ;

43

14

i n t max = 0 ;

41

f o r ( i n t i = 0 ; i < n ; i ++)
{
f o r ( i n t j = M; j >= v e t [ i ] ; j )
{
m[ j ] |= m[ j v e t [ i ] ] ;
}
}

13

int l i s 1 d ( int n , bool i n c = true )


{
i f ( inc ) s s c t f (n) ;
else s s d t f (n) ;

40

12

22

void s s d t f ( i n t n )
{
f o r ( i n t m = 1 ; m <= n ; m++)
{
c [m] = H[m ] ;
f o r ( i n t i = m 1; i > 0 ; i )
{
i f (A[ i ] > A[m] && c [ i ] + H[m] > c [m] )
{
c [m] = c [ i ] + H[m ] ;
}
}
}
}

35

6
7

20

23

18

24

2.3

f o r ( i n t i = m 1; i > 0 ; i )
{
i f (A[ i ] < A[m] && c [ i ] + H[m] > c [m] )
{
c [m] = c [ i ] + H[m ] ;
}
}

11

11
12

13

14

f o r i ( i , v . size () )
{
x = v[ i ];
j = l o w e r b o u n d ( pd . b e g i n ( ) , pd . b e g i n ( ) + maxi , x ) pd . b e g i n ( ) ;
// j = u p p e r b o u n d ( pd . b e g i n ( ) , pd . b e g i n ( ) + maxi , x ) pd . b e g i n ( )
; para l d s
pd [ j ] = x ;
pd index [ j ] = i ;

i f ( j == maxi ) { maxi++; i n d = i ; }
pred [ i ] = j ? p d i n d e x [ j 1] : 1;

15
16

36

}
// r e t u r n maxi ; s e a s e q u e n c i a nao p r e c i s a s e r r e f e i t a

17
18

35

return t r u e ;

37
38

19

i n t pos = maxi 1 , k = v [ i n d ] ;
asw . r e s i z e ( maxi ) ;

20
21

Codigo 26: Dijkstra Caminho minimo 1 para todos pesos positivos.

22

while ( pos >= 0 )


{
asw [ pos ] = k ;
i n d = pred [ i n d ] ;
k = v [ ind ] ;
}

23
24
25
26
27
28

3
4

typedef v e c t o r <map<int , int> > A d j L i s t ;


typedef A d j L i s t Grafo ;

5
6

29

#include <queue>

i n t d i s t [ MAX VERTICES ] ;
i n t p r e v [ MAX VERTICES ] ; // para r e c u p e r a r o caminho usando um d i j o i n t
forest set

8
9

2.4

Grafos

10
11
12
13

C
odigo 25: Verifica se o grafo e aciclico.
1
2
3
4
5
6

14

#define TAM 100


#define BRANCO 0
#define CINZA 1
#define PRETO 2
b o o l g r a f o [TAM] [TAM] ;
i n t p a s s [TAM] ;

15
16

9
10

18
19
20

22
23
24
25

for ( int i = 0 ;
{
i f ( grafo [ v ] [
{
i f ( pass [ i
i f ( pass [ i
}
}

12
13
14
15
16
17
18
19

i < TAM; i ++)

27

i ])
28

] == CINZA) return f a l s e ;
] == BRANCO && ! d f s ( i ) ) return f a l s e ;

29
30
31
32
33

p a s s [ v ] = PRETO;
return t r u e ;

21
22

34
35

36

24

26
27

// para cada v i z i n h o de u
f o r (map<int , int > : : i t e r a t o r i = g r a f o [ u ] . b e g i n ( ) ; i != g r a f o [ u ] .
end ( ) ; i ++)
{
int t o t a l D i s t = d i s t [ u ] + ( i ) . second ;
i f ( t o t a l D i s t <= d i s t [ ( i ) . f i r s t ] )
{
dist [( i ) . f i r s t ] = totalDist ;
heap . push ( m a k e p a i r ( t o t a l D i s t , ( i ) . f i r s t ) ) ;
prev [ ( i ) . f i r s t ] = u ;
}
}

26

20

25

while ( ! heap . empty ( ) )


{
i n t u = heap . top ( ) . s e c o n d ;
heap . pop ( ) ;

21

bool d f s ( int v )
{
p a s s [ v ] = CINZA ;

11

23

dist [ source ] = 0;
p r i o r i t y q u e u e <p a i r <int , int> > heap ;
heap . push ( m a k e p a i r ( 0 , s o u r c e ) ) ;

17

7
8

void d i j k s t r a ( Grafo& g r a f o , i n t s o u r c e )
{
f o r ( i n t i = 0 ; i < g r a f o . s i z e ( ) ; i ++)
{
d i s t [ i ] = INF ;
p r e v [ i ] = 1;
}

37

bool a c i c l i c o ()
{
memset ( pass , BRANCO, TAM s i z e o f ( i n t ) ) ;

38

28
29
30
31
32
33
34

f o r ( i n t i = 0 ; i < TAM; i ++)


{
i f ( p a s s [ i ] == BRANCO)
{
i f ( ! d f s ( i ) ) return f a l s e ;
}

C
odigo 27: Floresta dijunta de arvores
1

#define SIZE 100

2
3
4

10

struct d s f
{
int element count ;

i n t p a r e n t [ SIZE ] ;
i n t rank [ SIZE ] ;

6
7
8
9

13

};
typedef struct d s f d i s j o i n t s e t f o r e s t p ;

11

13
14
15

15
16

void d s f i n i t ( d i s j o i n t s e t f o r e s t p f o r e s t , i n t e l e m e n t c o u n t )
{
f o r e s t >e l e m e n t c o u n t = e l e m e n t c o u n t ;
memset ( f o r e s t >p a r e n t , 0 , e l e m e n t c o u n t s i z e o f ( i n t ) ) ;
memset ( f o r e s t >rank , 0 , e l e m e n t c o u n t s i z e o f ( i n t ) ) ;

17
18
19
20
21
22

16

f o r ( i n t i = 0 ; i < e l e m e n t c o u n t ; ++i )
f o r e s t >p a r e n t [ i ] = i ;

17
18
19

23
24

25
26

20
21
22
23
24
25
26
27
28

int d s f f i n d s e t ( d i s j o i n t s e t f o r e s t p f o r e s t , int i )
{
i f ( i != f o r e s t >p a r e n t [ i ] )
{
f o r e s t >p a r e n t [ i ] = d s f f i n d s e t ( f o r e s t , f o r e s t >p a r e n t [ i ] ) ;
}
return f o r e s t >p a r e n t [ i ] ;
}

27
28
29
30
31
32

30

32
33

34
35

void d s f u n i o n ( d i s j o i n t s e t f o r e s t p f o r e s t , i n t i , i n t j )
{
int x = d s f f i n d s e t ( f o r e s t , i ) ;
int y = d s f f i n d s e t ( f o r e s t , j ) ;

36
37

struct edge g e t e d g e l i s t ( Grafo& graph )


{
i n t e d g e c o u n t = graph . edgeCnt ;
struct edge e d g e s = ( struct edge ) m a l l o c ( e d g e c o u n t s i z e o f (
struct edge ) ) ;

38

int c u r r e n t e d g e = 0 ;

39

34

i f ( f o r e s t >rank [ x ] > f o r e s t >rank [ y ] )


{
f o r e s t >p a r e n t [ y ] = x ;
}
else
{
f o r e s t >p a r e n t [ x ] = y ;
i f ( f o r e s t >rank [ x ] == f o r e s t >rank [ y ] )
{
f o r e s t >rank [ y]++;
}
}

35
36
37
38
39
40
41
42
43
44
45
46
47

i n t e d g e c o m p a r e ( const void e1 , const void e2 )


{
struct edge p1 = ( struct edge ) e1 ;
struct edge p2 = ( struct edge ) e2 ;
i n t f = p1>w e i g h t p2>w e i g h t ;
i f ( f < 0)
{
return 1;
}
else
i f ( f == 0 )
{
return e d g e c o m p a r e 1 ( e1 , e2 ) ;
}
else
{
return 1 ;
}
}

33

29

31

};

14

10

12

int weight ;

12

40

f o r ( i n t i = 0 ; i < graph . a d j . s i z e
{
f o r (map<int , int > : : i t e r a t o r j
graph . a d j [ i ] . end ( ) ; j ++)
{
struct edge e ;
e . u = i < ( j ) . f i r s t ? i :
e . v = i > ( j ) . f i r s t ? i :
e . weight = ( j ) . second ;
e d g e s [ c u r r e n t e d g e ++] = e ;
}
}

41
42
43

44
45
46
47
48
49
50

51

( ) ; ++i )
= graph . a d j [ i ] . b e g i n ( ) ; j !=

( j ) . f i r s t ;
( j ) . f i r s t ;

52

return e d g e s ;

53
54

C
odigo 28: Kruskal Arvore geradora mnima kruskal

55
1
2
3
4
5
6

typedef v e c t o r <map<int , int> > A d j L i s t ;


struct Grafo
{
i n t edgeCnt ;
AdjList adj ;
};

56
57
58
59
60
61

7
8
9
10
11

void k r u s k a l ( Grafo& graph , Grafo& mst )


{
// Obtain a l i s t o f e d g e s and s o r t i t by w e i g h t i n O(E l g E) time
i n t e d g e c o u n t = graph . edgeCnt ;
struct edge e d g e s = g e t e d g e l i s t ( graph ) ;
q s o r t ( e d g e s , e d g e c o u n t , s i z e o f ( struct edge ) , e d g e c o m p a r e ) ;

62

struct edge
{
int u ;
int v ;

63
64
65

11

d i s j o i n t s e t f o r e s t dsf ;
d s f i n i t (& d s f , e d g e c o u n t ) ;

f o r ( i n t i = 0 ; i < e d g e c o u n t ; ++i )
{
struct edge e = e d g e s [ i ] ;
int uset = d s f f i n d s e t ( dsf , e . u) ;
int v s e t = d s f f i n d s e t ( dsf , e . v ) ;
i f ( u s e t != v s e t )
{
mst . a d j [ e . u ] [ e . v ] = e . w e i g h t ;
mst . edgeCnt++;
d s f u n i o n ( dsf , uset , vset ) ;
}
}

66
67
68
69
70
71
72
73
74
75
76
77

8
9
10
11
12
13
14
15
16
17
18
19

78

20

f r e e ( edges ) ;

79
80

void d f s T o p s o r t ( i n t no )
{
f o r ( i n t v i z = 0 ; v i z < SIZE ; v i z ++)
{
i f ( g r a f o [ no ] [ v i z ] )
{
i f ( p r o f [ v i z ] == UNVISITED)
{
p r o f [ v i z ] = p r o f [ no ] + 1 ;
dfsTopsort ( viz ) ;
}
}
}

21

s o r t e d [ nordem] = no ;

22
23

24
25

C
odigo 29: verifica se um grafo e bipartido
1

26

#define TAM 200

27

2
3
4
5

28

b o o l g r a f o [TAM] [TAM] ;
i n t p a s s [TAM] ;
int n ;

29

8
9
10
11

f o r ( i n t i = 0 ; i < nvt ; i ++)


{
i f ( p r o f [ i ] == UNVISITED)
{
prof [ i ] = 0;
dfsTopsort ( i ) ;
}
}

30
31

6
7

void t o p S o r t ( i n t nvt )
{
memset ( p r o f , UNVISITED , nvt s i z e o f ( i n t ) ) ;
nordem = nvt 1 ;

32

bool b i p a r t i d o ( int v , int c o l o r = 1)


{
pass [ v ] = c o l o r ;
int t h i s C o l o r = c o l o r ;
bool ret = true ;

33
34
35
36
37

12

38

c o l o r = c o l o r == 1 ? 2 : 1 ;

13
14

f o r ( i n t i = 0 ; i < n ; i ++)
{
i f ( grafo [ v ] [ i ] )
{
i f ( ! pass [ i ] ) r et = dfs ( i , c o l o r ) ;
e l s e i f ( p a s s [ i ] == t h i s C o l o r ) return f a l s e ;

15
16
17
18
19
20

C
odigo 31: calcula fluxo m
aximo, Ford-Fulkerson
1
2
3
4
5

21

if

22

( ! r e t ) return f a l s e ;

23

24

return r e t ;

26

10

11
12
13

15

#define UNVISITED 1

16

2
3
4
5
6
7

17

int
int
int
int

bool b f s ( int n , int i n i , int fim )


{
i n t no , s = 0 , e = 0 ;
f i l a [ e++] = i n i ;

14

C
odigo 30: faz a ordenac
ao topol
ogica de um grafo acclico
1

i n t g r a f o [TAM] [TAM] ;
i n t pred [TAM] ;
i n t f [TAM] [TAM] ;
b o o l v i s i t a d o s [TAM] ;
i n t f i l a [TAM] ;

25

27

#define TAM 1000


#define MAX INT 1000000

g r a f o [ SIZE ] [ SIZE ] ;
p r o f [ SIZE ] ;
s o r t e d [ SIZE ] ;
nordem ;

while ( s != e )
{
no = f i l a [ s ++];

18
19
20

i f ( v i s i t a d o s [ no ] == 2 ) continue ;
v i s i t a d o s [ no ] = 2 ;

21

12

22

f o r ( i n t i = 0 ; i < n ; i ++)

23

78

i f ( v i s i t a d o s [ i ] < 2)
{
i f ( g r a f o [ no ] [ i ] f [ no ] [ i ] > 0 )
{
pred [ i ] = no ;
i f ( i == f i m ) return t r u e ;
i f ( v i s i t a d o s [ i ] == 0 )
{
f i l a [ e++] = i ;
visitados [ i ] = 1;
}
}
}

24
25
26
27
28
29
30
31
32
33
34
35
36

80
81
82
83

86
87
88
89

92
93
94

39

return f a l s e ;

40
41

44
45
46

96

98

101
102

49
50
51
52
53
54
55

103

1
2

63
64
65

66
67
68

bool findPath ( int s , int t , int s i z e )


{
memset ( v i s i t a d o s , f a l s e , s i z e o f ( b o o l ) s i z e ) ;
pred [ s ] = s ;
// Aqui pode s e r usado t a n t o b u s c a em l a r g u r a qu a n to em p r o f u n d i d a d e .
// b u s c a em l a r g u r a g e r a l m e n t e a p r e s e n t a tempos de e x e c u c a o bem
menores .
return b f s ( s i z e , s , t ) ;
// r e t u r n d f s ( s , t , s i z e ) ;
}

5
6
7
8
9
10

71
72

12
13
14

74

76
77

grafo
l i s t a de a d j a c e n c i a s r e p r e s e n t a d a na forma de v e t o r
nvt , nar ;
d e s t [ 2 AR ] ;
a d j [VT ] [ 2 VT ] ;
n a d j [VT ] ;

i n t cap [AR ] ; // c a p a c i d a d e do a r c o
i n t f l u x o [AR ] ;
i n t e n t [VT ] ;

15

i n t maxFlow ( i n t s i z e , i n t s , i n t t )
{
int d e l t a ;

16

i n t p a d j [VT] , l i m [VT] , n i v e l [VT] , qtd [VT ] ;

17
18
19

73

75

struct
{
//
int
int
int
int

11

69
70

const i n t VT = 1 0 0 ;
const i n t AR = VT VT;

59

62

Codigo 32: calcula fluxo m


aximo, algoritmo mais eficiente porem muito maior em tempo
de codificacao

return f a l s e ;

57

return soma ;

104
105

56

61

f o r ( i n t i = 0 ; i < s i z e ; i ++)
{
soma += f [ i ] [ t ] ;
}

99
100

f o r ( i n t v = 0 ; v < s i z e ; v++)
{
i f ( ! v i s i t a d o s [ v ] && g r a f o [ s ] [ v ] f [ s ] [ v ] > 0 )
{
pred [ v ] = s ;
i f ( d f s ( v , t , s i z e ) ) return t r u e ;
}
}

48

60

i n t soma = 0 ;

97

bool d f s ( int s , int t , int s i z e )


{
visitados [ s ] = true ;
i f ( s == t ) return t r u e ;

47

58

95

42
43

f o r ( i n t c = t ; pred [ c ] != c ; c = pred [ c ] )
{
f [ pred [ c ] ] [ c ] += d e l t a ;
f [ c ] [ pred [ c ] ] = d e l t a ;
}

90
91

38

d e l t a = MAX INT ;
f o r ( i n t c = t ; pred [ c ] != c ; c = pred [ c ] )
{
d e l t a = min ( d e l t a , g r a f o [ pred [ c ] ] [ c ] f [ pred [ c ] ] [ c ] ) ;
}

84
85

37

while ( 1 )
{
b o o l path = f i n d P a t h ( s , t , s i z e ) ;
i f ( ! path ) break ;

79

f o r ( i n t i = 0 ; i < s i z e ; i ++)
{
memset ( f [ i ] , 0 , s i z e o f ( i n t ) s i z e ) ;
}

20

i n t i n v ( i n t a ) { return a 0 x1 ; }
i n t o r i g ( i n t a ) { return d e s t [ i n v ( a ) ] ; }
i n t c a p r e s ( i n t a ) { return cap [ a ] f l u x o [ a ] ; }

21
22
23

13

24

void i n i c ( i n t n = 0 )
{
nvt = n ;

nar = 0 ;
memset ( nadj , 0 , s i z e o f ( n a d j ) ) ;

25
26
27

81

30
31
32
33
34
35
36
37
38
39
40

84

// ////////////////////////////////////////////////////////
// A d i c i o n a uma a r e s t a ao g r a f o .
//
// i n t u apenas para F l u x o s ;
//
int a r e s t a ( int i , int j , int u = 0)
{
i n t a r = nar ;
cap [ nar ] = u ;
d e s t [ nar ] = j ;
a d j [ i ] [ n a d j [ i ] ] = nar++;
n a d j [ i ]++;

85

86
87
88

90
91

93
94
95

44
45
46

52

98

101

102
103

void r e v b f s ( i n t i n i , i n t f i m )
{
i n t i , no , v i z , a r ;
queue<int> f i l a ;

104
105

i f ( menor != NULO) n i v e l [ no ] = n i v e l [ menor ] ;


qtd[++ n i v e l [ no ] ] + + ;

106
107
108

memset ( n i v e l , NULO, s i z e o f ( n i v e l ) ) ;
memset ( qtd , 0 , s i z e o f ( qtd ) ) ;

54
55

return ( ( e n t [ no ] == NULO) ? no : o r i g ( e n t [ no ] ) ) ;

109
110

111

56

n i v e l [ fim ] = 0 ;
f i l a . push ( f i m ) ;

57
58

112
113
114

59

while ( ! f i l a . empty ( ) )
{
no = f i l a . f r o n t ( ) ;
f i l a . pop ( ) ;
qtd [ n i v e l [ no ] ] + + ;

60
61
62
63
64

115
116
117
118

120

f o r ( i = 0 ; i < n a d j [ no ] ;
{
a r = a d j [ no ] [ i ] ;
v i z = dest [ ar ] ;

66
67
68
69

i n t avanca ( i n t no , i n t a r )
{
int v i z = dest [ ar ] ;
ent [ v i z ] = ar ;
l i m [ v i z ] = min ( l i m [ no ] , c a p r e s ( a r ) ) ;
return v i z ;
}

119

65

i ++)

121
122

i n t aumenta ( i n t i n i , i n t f i m )
{
i n t ar , no = fim , fmax = l i m [ f i m ] ;

123

while ( no != i n i )
{
f l u x o [ a r = e n t [ no ] ] += fmax ;
f l u x o [ i n v ( a r ) ] = fmax ;
no = o r i g ( a r ) ;
}

124
125

70

i f ( cap [ a r ] == 0 && n i v e l [ v i z ] == NULO)


{
n i v e l [ v i z ] = n i v e l [ no ] + 1 ;
f i l a . push ( v i z ) ;
}

71
72
73
74
75

126
127
128
129
130

76

return fmax ;

131

77

132

133
134

79
80

f o r ( i = 0 ; i < n a d j [ no ] ; i ++)
{
a r = a d j [ no ] [ i ] ; v i z = d e s t [ a r ] ;
i f ( c a p r e s ( a r ) <= 0 ) continue ;
i f ( menor == NULO | | n i v e l [ v i z ] < n i v e l [ menor ] ) menor = v i z ;
}

99
100

53

78

i f (qtd [ n i v e l [ no ] ] == 0 ) return NULO;

97

48

51

i n t r e t r o c e d e ( i n t no )
{
i n t i , ar , v i z , menor = NULO;

96

43

50

92

cap [ nar ] = 0 ;
d e s t [ nar ] = i ;
a d j [ j ] [ n a d j [ j ] ] = nar++;
n a d j [ j ]++;
return a r ;

42

49

p a d j [ no ] = 0 ;
return NULO;

89

41

47

while ( p a d j [ no ] < n a d j [ no ] )
{
i n t a r = a d j [ no ] [ p a d j [ no ] ] ;
i f ( n i v e l [ no ] == n i v e l [ d e s t [ a r ] ] + 1 && c a p r e s ( a r ) > 0 ) return
ar ;
p a d j [ no ]++;
}

83

28
29

82

i n t a d m i s s i v e l ( i n t no )

135

14

i n t maxflow ( i n t i n i , i n t f i m )
{

i n t ar , no = i n i , fmax = 0 ;

136

memset ( f l u x o , 0 , s i z e o f ( f l u x o ) ) ;
memset ( padj , 0 , s i z e o f ( p a d j ) ) ;

138
139

24
25

r e v b f s ( i n i , fim ) ;

141

27
28

142

l i m [ i n i ] = INF ;
e n t [ i n i ] = NULO;

143
144

29
30

while ( n i v e l [ i n i ] < nvt && no != NULO)


{
i f ( ( a r = a d m i s s i v e l ( no ) ) == NULO)
{
no = r e t r o c e d e ( no ) ;
}
e l s e i f ( ( no = avanca ( no , a r ) ) == f i m )
{
fmax += aumenta ( i n i , f i m ) ;
no = i n i ;
}
}
return fmax ;

146
147
148
149
150
151
152
153
154
155
156
157
158

32
33
34

36

38

40
41
42
43

45

};

46

double a n g l e ( p o i n t p , p o i n t q , p o i n t r )
{
point u = p q , v = r q ;
return atan2 ( u % v , u v ) ;
}

Geometria
Codigo 34: Decide se q est
a sobre o segmento fechado pr.

C
odigo 33: ponto e poligono
1

i n t ccw ( p o i n t p , p o i n t q , p o i n t r )
{
return cmp ( ( p r ) % ( q r ) ) ;
}

44

49

typedef v e c t o r <p o i n t > p o l y g o n ;

39

48

point point : : pivot ;

37

47

// para p o n t o s 2D
double abs ( p o i n t p ) { return hypot ( p . x , p . y ) ; }
double a r g ( p o i n t p ) { return atan2 ( p . y , p . x ) ; }

35

159

};

31

145

2.5

f r i e n d os tr ea m& o p e r a t o r <<(os tr ea m& o , p o i n t p ) {


return o << " ( " << p . x << " , " << p . y << " , " << p . z << " ) " ;
}
static point pivot ;

26

140

160

b o o l o p e r a t o r ==(p o i n t q ) const { return cmp ( q ) == 0 ; }


b o o l o p e r a t o r !=( p o i n t q ) const { return cmp ( q ) != 0 ; }
b o o l o p e r a t o r < ( p o i n t q ) const { return cmp ( q ) < 0 ; }

22
23

137

struct p o i n t
{
double x , y ;
double z ; // para p o n t o s no e s p a c o
p o i n t ( double x = 0 , double y = 0 , double z = 0 ) : x ( x ) , y ( y ) , z ( z ) {}

2
3
4

b o o l between ( p o i n t p , p o i n t q , p o i n t r )
{
return ccw ( p , q , r ) == 0 && cmp ( ( p q ) ( r q ) ) <= 0 ;
}

6
7

9
10
11
12

13

Codigo 35: Decide se os segmentos fechados pq e rs tem pontos em comum.

p o i n t o p e r a t o r +( p o i n t q ) { return p o i n t ( x + q . x , y + q . y , z + q . z ) ;
}
p o i n t o p e r a t o r ( p o i n t q ) { return p o i n t ( x q . x , y q . y , z q . z ) ;
}
p o i n t o p e r a t o r ( double t ) { return p o i n t ( x t , y t , z t ) ; }
p o i n t o p e r a t o r / ( double t ) { return p o i n t ( x / t , y / t , z / t ) ; }
double o p e r a t o r ( p o i n t q ) { return x q . x + y q . y + z q . z ; }
p o i n t v e c ( p o i n t q ) { return p o i n t ( y q . z z q . y , z q . x x q .
z , x q . y y q . x) ; }
double o p e r a t o r %( p o i n t q ) { return x q . y y q . x ; }

1
2
3
4
5
6

16
17
18
19
20
21

i n t e r s e c t ( point p , point q , point r , point s )


A
B
C
D

=
=
=
=

q
s
r
s

p;
r;
p;
q;

i n t a = cmp (A % C) + 2 cmp (A % D) ;
i n t b = cmp (B % C) + 2 cmp (B % D) ;

8
9

14
15

bool s e g
{
point
point
point
point

10

i n t cmp ( p o i n t q ) const
{
i f ( i n t t = : : cmp ( x , q . x ) ) return t ;
e l s e i f ( i n t t = : : cmp ( y , q . y ) ) return t ;
return : : cmp ( z , q . z ) ;
}

i f ( a == 3 | | a == 3 | | b == 3 | | b == 3) return f a l s e ;
i f ( a | | b | | p == r | | p == s | | q == r | | q == s ) return t r u e ;

11
12
13

int t = ( p < r ) + ( p < s ) + ( q < r ) + ( q < s ) ;


return t != 0 && t != 4 ;

14
15
16

15

C
odigo 36: Calcula a dist
ancia do ponto r ao segmento pq.
double s e g
{
point A
point B
point C

1
2
3
4
5

19

b u i l d f a i l u r e f u n c t i o n ( pattern ) ;
v e c t o r <int> s t a r t p o s i t i o n s ;
int j = 0 , m = pattern . s i z e ( ) , n = t e x t . s i z e ( ) ;

20

distance ( point p , point q , point r )

21
22

= r q;
= r p;
= q p;

23

f o r ( i n t i = 0 ; i < n ; i ++)
{
while ( t r u e )
{
i f ( t e x t [ i ] == p a t t e r n [ j ] )
{
i f ( ++j == m )
{
s t a r t p o s i t i o n s . push back ( i m + 1 ) ;
j = F[ j ] ;
}
break ;
}

24
25

6
26

double a = A A, b = B B, c = C C ;

27

8
28

i f ( cmp ( b , a + c ) >= 0 ) return s q r t ( a ) ;


e l s e i f ( cmp ( a , b + c ) >= 0 ) return s q r t ( b ) ;
e l s e return f a b s (A % B) / s q r t ( c ) ;

9
10
11

29
30
31

12

32
33
34

C
odigo 37: Classifica o ponto p em relac
ao ao polgono T. Retorna 0, -1 ou 1 dependendo
se p est
a no exterior, na fronteira ou no interior de T, respectivamente.
int i n p o l y ( point p ,
{
double a = 0 ;
int N = T. s i z e ( ) ;
for ( int i = 0 ; i
{
i f ( between (T [
a += a n g l e (T [ i
}
return cmp ( a ) !=
}

1
2
3
4
5
6
7
8
9
10
11

2.6

35
36
37

p o l y g o n& T)

39

3
4
5
6
7
8
9
10
11
12
13
14
15

< N; i ++)

42

18

return s t a r t p o s i t i o n s ;

43

i ] , p , T [ ( i +1) % N ] ) ) return 1;
] , p , T [ ( i +1) % N ] ) ;

44

0;

2.7

// F [ i ] s i z e o f t h e l a r g e s t p r e f i x o f p a t t e r n [ 0 . . i ] t h a t i s a l s o a
// s u f f i x o f p a t t e r n [ 1 . . i ] . Ex : p a t t e r n = {a , b , a , c , a , b } , F =
{0 ,0 ,1 ,0 ,1 ,2}
#define MAX PATTERN SIZE 10010
i n t F [ MAX PATTERN SIZE ] ;
void b u i l d f a i l u r e f u n c t i o n ( const s t r i n g & p a t t e r n )
{
int m = pattern . s i z e ( ) ;
F [ 0 ] = 1;
f o r ( i n t i = 0 ; i < m; i ++)
{
F [ i +1] = F [ i ] + 1 ;
while ( F [ i +1] > 0 && p a t t e r n [ i ] != p a t t e r n [ F [ i +1]1 ] )
F [ i +1] = F [ F [ i +1]1 ] + 1 ;
}
}

/
The J o s e p h u s problem ( or J o s e p h u s p e r m u t a t i o n ) i s a t h e o r e t i c a l problem
r e l a t e d t o a c e r t a i n c o u n t i n g o u t game . There a r e p e o p l e s t a n d i n g i n
a c i r c l e w a i t i n g t o be e x e c u t e d . A f t e r t h e f i r s t man i s e x e c u t e d ,
c e r t a i n number o f p e o p l e a r e s k i p p e d and one man i s e x e c u t e d . Then
again , p e o p l e a r e s k i p p e d and a man i s e x e c u t e d . The e l i m i n a t i o n
p r o c e e d s around t h e c i r c l e ( which i s becoming s m a l l e r and s m a l l e r as
t h e e x e c u t e d p e o p l e a r e removed ) , u n t i l o n l y t h e l a s t man remains ,
who i s g i v e n freedom . The t a s k i s t o c h o o s e t h e p l a c e i n t h e i n i t i a l
c i r c l e so t h a t you a r e t h e l a s t one r e m a i n i n g and so s u r v i v e .
/

4
5

u s i n g namespace s t d ;

6
7
8
9
10
11

i n t j o s e p h u s ( i n t n , i n t m)
{
int r e s = 0 ;
v e c t o r <int> p e o p l e ;
int l o c = 0 ;

12
13

// r e t o r n a a p o s i c a o i n i c i a l de cada o c o r r e n c i a de p a t t e r n em t e x t
v e c t o r <int> KMP( const s t r i n g & t e x t , const s t r i n g & p a t t e r n )

Outros
C
odigo 39: josephus problem

Casamento de strings

16
17

41

40

C
odigo 38: String matching - Algoritmo KMP - O( n + m)
1

i f ( j == 0 ) break ;
j = F[ j ] ;

38

f o r ( i n t i = 0 ; i < n ; i ++) p e o p l e . p u s h b a c k ( i +1) ;

14

16

15

while ( p e o p l e . s i z e ( ) > 1 )

16

18

43

44

19

p e o p l e . e r a s e ( p e o p l e . b e g i n ( )+l o c ) ;
l o c += (m1) ;

20
21

45
46

22

}
res . resize ( k , 0 ) ;
f o r i ( i ,m) r e s [ k t [ i ] ] = b [ i ] ;
return r e s ;

47
48

23

return p e o p l e [ 0 ] ;

24
25

i f ( e == 1 ) break ;
f o r i ( i ,m) i f ( cmpD(A[ i ] [ e ] ) > 0 && ( l == 1 | | cmpD( b [ i ] / A
[ i ][ e] ,
b [ l ] / A[ l ] [ e ] , 1 e 20 ) < 0 ) ) l = i ;
i f ( l == 1 ) return v e c t o r < double >() ; // unbounded
pivot ( k , l , e ) ;

42

i f ( l o c >= p e o p l e . s i z e ( ) )
l o c %= p e o p l e . s i z e ( ) ;

17

49

50

}
v e c t o r < double > s o l v e ( ) {
m = A. s z ; n = A [ 0 ] . s z ; i n t k = m+n+1;
N = v e c t o r < b o o l >( k , t r u e ) ; v e c t o r < double > c c o p y = c ;
c . r e s i z e ( n+m) ; k t . r e s i z e (m) ;
f o r i ( i ,m) {
A[ i ] . r e s i z e ( k ) ; A[ i ] [ n+i ] = 1 ; A[ i ] [ k 1] = 1;
k t [ i ] = n+i ; N[ k t [ i ] ] = f a l s e ;
}
int l = min element ( a l l ( b) ) b . begin ( ) ;
i f (cmpD( b [ 1 ] ) < 0 ) {
c = v e c t o r <double>(k , 0 ) ;
c [ k 1] = 1; p i v o t ( k , 1 , k1) ; s o l=go ( k ) ;
i f (cmpD( s o l [ k 1]) >0) return v e c t o r <double >() ; // i n f e a s i b l e
f o r i ( i ,m) i f ( k t [ i ] == k1) {
f o r i ( j , k1) i f (N[ j ] && cmpD( A[ i ] [ j ] ) != 0 ) {
p i v o t ( k , i , j ) ; break ;
}
}
c=c c o p y ; c . r e s i z e ( k , 0 ) ;
f o r i ( i ,m) f o r i ( j , k ) i f (N[ j ] ) c [ j ] = c [ k t [ i ] ] A[ i ] [ j ] ;
}
s o l = go ( k1) ;
i f ( ! s o l . empty ( ) ) s o l . r e s i z e ( n ) ;
return s o l ;
}

51
52
53

C
odigo 40: Simplex

54
1
2
3

#include <i o s t r e a m >


#include <v e c t o r >
#include <a l g o r i t h m >

55
56
57

4
5

58

u s i n g namespace s t d ;

59

6
7
8
9
10

60

#define
#define
#define
#define

f o r i ( i , n ) f o r ( i n t i =0; i < ( n ) ; ++i )


f o r r ( i , a , b ) f o r ( i n t i =(a ) ; i <= ( b ) ; ++i )
f o r d ( i , a , b ) f o r ( i n t i =(a ) ; i >= ( b ) ; i )
sz s i z e ()

61
62
63
64

11
12

65

const double EPS=1e 9; const i n t INF = 0 x 3 f 3 f 3 f 3 f ;

66

13
14

67

#define a l l ( x ) ( x ) . b e g i n ( ) , ( x ) . end ( )

68

15
16
17
18

69

i n t cmpD( double x , double y=0 , double t o l=EPS) {


return ( x <= y+t o l ) ? ( x+t o l <y ) ? 1 : 0 : 1 ;
}

70
71
72

19
20
21
22

23
24
25
26
27
28
29
30

31
32
33
34
35
36
37
38
39
40
41

73

struct s i m p l e x {
// max c x , s . t : A x <= b ; x >= 0
s i m p l e x ( const v e c t o r < v e c t o r < double > > & A , const v e c t o r < double
> & b ,
const v e c t o r < double > & c ) : A( A ) , b ( b ) , c ( c ) {}
v e c t o r < v e c t o r < double > > A; v e c t o r < double > b , c , s o l ;
v e c t o r < b o o l > N; v e c t o r < i n t > k t ; i n t m, n ;
void p i v o t ( i n t k , i n t l , i n t e ) {
i n t x = k t [ l ] ; double p = A[ l ] [ e ] ;
f o r i ( i , k ) A[ l ] [ i ] /= p ;
b [ l ] /= p ; N[ e ] = f a l s e ;
f o r i ( i ,m) i f ( i != l ) {b [ i ] = A[ i ] [ e ] b [ l ] ; A[ i ] [ x ] = A[ i ] [ e ] A[
l ][ x];}
f o r i ( j , k ) i f ( N[ j ] ) {
c [ j ] = c [ e ] A[ l ] [ j ] ;
f o r i ( i ,m) i f ( i != l ) A[ i ] [ j ] = A[ i ] [ e ] A[ l ] [ j ] ;
}
k t [ l ] = e ; N[ x ] = t r u e ; c [ x ] = c [ e ] A[ l ] [ x ] ;
}
v e c t o r < double > go ( i n t k ) {
v e c t o r < double > r e s ;
while ( 1 ) {
i n t e = 1, l = 1;
f o r i ( i , k ) i f ( N[ i ] && cmpD( c [ i ] ) > 0 ) { e = i ; break ; }

74
75
76
77

};

78
79
80
81
82
83
84
85

// Como u s a r
i n t main ( ) {
/ Exemplo : Maximize cx S u b j e c t t o Ax <= b /
v e c t o r <v e c t o r <double> > A( 9 ) ;
double Av [ ] [ 3 ] = { { 1 , 1 , 0 } , {0 ,0 , 1} , { 1 , 1 ,0} ,
{0 ,0 ,1} , {1 ,0 ,0} , {0 ,1 ,0} ,
{0 ,0 ,1} , {1 ,0 ,1} , {0 ,1 ,0}};

86
87
88

89

f o r ( i n t i =0; i < 9 ; i ++) {


A[ i ] . i n s e r t (A[ i ] . b e g i n ( ) , &(Av [ i ] [ 0 ] ) , &(Av [ i ] [ 3 ] ) ) ; // Sim , [ 3 ] !
Ou s e j a , i d x f i n a l +1
}

90
91
92
93

v e c t o r <double > c ( 3 , 1 ) ; // c =[1 1 1 ]


double bv [ ] = { 2 , 1 , 2 , 1 , 2 , 1 , 1 , 2 , 1 } ;
v e c t o r <double > b ( bv , bv+s i z e o f ( bv ) / s i z e o f ( double ) ) ;

94
95

17

s i m p l e x sim (A, b , c ) ;

98
99
100
101
102

v e c t o r <double> s = sim . s o l v e ( ) ;
i f ( ! s . s i z e ( ) ) c o u t << " Impossivel \ n " ;
else
f o r ( i n t i =0; i < s . s i z e ( ) ; i ++) {
c o u t << s [ i ] << e n d l ;
}

96
97

Biblioteca C/C++

3.1

Ignorando entradas na famlia scanf:

Codigo 42: Ignora os dois floats do meio. Retornar


a 2 no sucesso.
s c a n f ( " % f %* f %* f % d " , &a , &b ) ;

C
odigo 41: Gera as permutac
oes dos elementos da string
1
2
3
4

3.2

b o o l n e x t P e r m u t a t i o n ( s t r i n g& number )
{
bool isBigger = true ;
int i , j ;

1
2

f o r ( i = number . s i z e ( ) 1 ; i >= 0 ; i )
{
i f ( number [ i ] < number [ i +1]) break ;
}

7
8
9

13

9
10
11

f o r ( j = number . s i z e ( ) 1 ; j >= i +1; j )


{
i f ( number [ j ] > number [ i ] )
{
break ;
}
}

16
17
18
19
20
21

12
13
14
15
16
17
18

22
19

i n t tmp = number [ i ] ;
number [ i ] = number [ j ] ;
number [ j ] = tmp ;

23
24
25

20
21

23

j = number . s i z e ( ) 1;
i ++;

28

24
25

29
26

while ( i < j )
{
tmp = number [ i ] ;
number [ i ] = number [ j ] ;
number [ j ] = tmp ;
i ++;
j ;
}

30
31
32
33
34
35
36
37

27
28
29

30
31
32

34

39

41

35

return i s B i g g e r ;

40

c l a s s Comparadora { // Ordena c r e s c e n t e m e n t e
public : // < IMPORTANTE
bool operator ( ) ( const P e s s o a &a , const P e s s o a &b )
{
i n t i d D i f = a . i d a d e b . i d a d e ;
i f ( i d D i f < 0 ) return true ;
e l s e i f ( i d D i f ==0) return a . nome . compare ( b . nome ) < 0 ? true : f a l s e
;
e l s e return f a l s e ;
}
};

33

38

class Pessoa {
int idade ;
s t r i n g nome ;
friend c l a s s Comparadora ;
public :
P e s s o a ( s t r i n g nome , i n t i d a d e ) {
this>i d a d e = i d a d e ;
this>nome = nome ;
}
void p r i n t ( ) const {
p r i n t f ( " Nome : % s Idade : % d \ n " , nome . c s t r ( ) , i d a d e ) ;
}
};

22

26
27

c l a s s Comparadora ;

14
15

using namespace s t d ; // USE ISTO ! ! !

i f ( i != 1)
{
isBigger = false ;

12

#include <map>
#include <s t r i n g >
#include <c s t d i o >

10
11

Map
C
odigo 43: Referencias map

5
6

I/O

36

i n t main ( ) {
P e s s o a r ( " Rangelz " , 8 6 ) ;
P e s s o a r 2 ( " Rangelzao " , 8 6 ) ;

37
38

18

39

map<Pessoa , s t r i n g , Comparadora> a l u n o s ;
a l u n o s [ r ]= " UFMG " ;

a l u n o s [ r 2 ]= " PUC " ;


// I t e r a t o r
f o r (map<Pessoa , s t r i n g , Comparadora > : : i t e r a t o r i t=a l u n o s . b e g i n ( ) ; i t
!= a l u n o s . end ( ) ; i t ++) {
i t > f i r s t . p r i n t ( ) ;
p r i n t f ( " \ t % s \ n \ n " , i t >s e c o n d . c s t r ( ) ) ;
}
// Find
i f ( a l u n o s . f i n d ( P e s s o a ( " Rangelz " , 8 6 ) ) != a l u n o s . end ( ) ) { // Achou !
p r i n t f ( " Achei Rangel !\ n " ) ;
}
return 0 ;

40
41
42

43
44
45
46
47
48
49
50
51

19

Você também pode gostar