Escolar Documentos
Profissional Documentos
Cultura Documentos
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
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
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
9
10
11
12
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
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
37
38
}
return 0 ;
39
40
41
C
odigo 2: comparcao de ponto flutuante
1
2
3
4
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 ;
}
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
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
7
8
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
36
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
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
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 ) ;
10
30
return 0 ;
31
32
11
12
13
14
15
C
odigo 8: exemplo de list
1
2
3
17
18
19
4
5
6
7
8
9
16
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
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
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
21
22
c o u t << e n d l ;
21
return 0 ;
22
23
24
25
23
24
25
26
C
odigo 11: exemplo de stack
1
2
3
27
28
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
15
16
17
18
19
C
odigo 13: exemplo de string
1
2
3
20
21
22
return 0 ;
23
24
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
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
23
14
15
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
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
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
42
46
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 .
/
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
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 ;
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
19
20
40
21
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
i n t key = 4 0 ;
Codigo 18: m
aximo divisor comum e mnimo multiplo comum
1
2
3
C
odigo 17: Arredondamento e output em outras bases
4
1
2
3
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
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
<< 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
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
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
32
33
34
8
9
10
11
36
37
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 )
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
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
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
22
23
24
25
26
27
28
3
4
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
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
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
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
C
odigo 27: Floresta dijunta de arvores
1
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
38
int c u r r e n t e d g e = 0 ;
39
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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
56
57
58
59
60
61
7
8
9
10
11
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
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
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
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
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
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
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
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
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
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
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
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
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
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
39
48
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
26
140
160
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
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
=
=
=
=
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
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
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
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
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
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
61
62
63
64
11
12
65
66
13
14
67
#define a l l ( x ) ( x ) . b e g i n ( ) , ( x ) . end ( )
68
15
16
17
18
69
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
90
91
92
93
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
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
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
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 " ;
40
41
42
43
44
45
46
47
48
49
50
51
19