├── algorithms ├── greedy │ ├── .gitignore │ ├── crush │ │ ├── output00.txt │ │ ├── input_test.txt │ │ ├── input00.txt │ │ ├── Cargo.lock │ │ ├── Cargo.toml │ │ └── src │ │ │ └── main.rs │ ├── sherlock-and-minimax │ │ ├── output00.txt │ │ ├── input00.txt │ │ ├── Cargo.lock │ │ ├── Cargo.toml │ │ └── src │ │ │ └── main.rs │ ├── chief-hopper │ │ ├── input.txt │ │ └── src.py │ ├── accessory-collection │ │ ├── input.txt │ │ ├── Cargo.lock │ │ ├── Cargo.toml │ │ └── src │ │ │ └── main.rs │ └── team-formation │ │ ├── Cargo.lock │ │ ├── input00.txt │ │ ├── Cargo.toml │ │ └── src │ │ └── main.rs ├── sorting │ ├── .gitignore │ ├── almost-sorted │ │ ├── output │ │ │ ├── output20.txt │ │ │ ├── output00.txt │ │ │ └── output21.txt │ │ ├── input │ │ │ ├── input00.txt │ │ │ ├── input20.txt │ │ │ └── input21.txt │ │ ├── Cargo.lock │ │ ├── Cargo.toml │ │ └── src │ │ │ └── main.rs │ └── insertion-sort │ │ ├── input.txt │ │ ├── Cargo.toml │ │ ├── src │ │ └── main.rs │ │ └── Cargo.lock ├── string │ ├── .gitignore │ ├── common-child │ │ ├── input01.txt │ │ ├── input00.txt │ │ ├── input02.txt │ │ ├── input03.txt │ │ ├── Cargo.lock │ │ ├── Cargo.toml │ │ └── src │ │ │ ├── main.rs │ │ │ └── first_attempt.rs │ ├── two-strings │ │ ├── input00.txt │ │ ├── Cargo.lock │ │ ├── Cargo.toml │ │ └── src │ │ │ └── main.rs │ └── sherlock-and-anagrams │ │ ├── Cargo.lock │ │ ├── input01.txt │ │ ├── Cargo.toml │ │ └── src │ │ └── main.rs ├── graph_theory │ ├── .gitignore │ ├── even-tree │ │ ├── output02.txt │ │ ├── output00.txt │ │ ├── Cargo.lock │ │ ├── input00.txt │ │ ├── Cargo.toml │ │ ├── input02.txt │ │ └── src │ │ │ └── main.rs │ ├── jeanies-route │ │ ├── output05.txt │ │ ├── output09.txt │ │ ├── input.txt │ │ ├── Cargo.lock │ │ ├── Cargo.toml │ │ ├── src │ │ │ └── main.rs │ │ └── input05.txt │ ├── bfsshortreach │ │ ├── output00.txt │ │ ├── input00.txt │ │ ├── Cargo.toml │ │ ├── Cargo.lock │ │ └── src │ │ │ └── main.rs │ └── dijkstrashortreach │ │ ├── Cargo.toml │ │ ├── input00.txt │ │ ├── Cargo.lock │ │ └── src │ │ └── main.rs ├── implementation │ ├── .gitignore │ ├── matrix-rotation-algo │ │ ├── output │ │ │ ├── output12.txt │ │ │ ├── output00.txt │ │ │ ├── output10.txt │ │ │ └── output11.txt │ │ ├── input │ │ │ ├── input12.txt │ │ │ ├── input00.txt │ │ │ ├── input10.txt │ │ │ └── input11.txt │ │ ├── Cargo.lock │ │ ├── Cargo.toml │ │ └── src │ │ │ └── main.rs │ ├── kaprekar-numbers │ │ ├── Cargo.lock │ │ ├── Cargo.toml │ │ └── src │ │ │ └── main.rs │ └── the-grid-search │ │ ├── Cargo.lock │ │ ├── Cargo.toml │ │ ├── input.txt │ │ └── src │ │ └── main.rs ├── dynamic_programming │ ├── .gitignore │ ├── candies │ │ ├── output │ │ │ ├── output00.txt │ │ │ └── output10.txt │ │ ├── input │ │ │ ├── input00.txt │ │ │ ├── test_00.txt │ │ │ └── input10.txt │ │ ├── Cargo.lock │ │ ├── Cargo.toml │ │ └── src │ │ │ └── main.rs │ ├── coin-change │ │ ├── output │ │ │ ├── output00.txt │ │ │ └── output01.txt │ │ ├── input │ │ │ ├── input00.txt │ │ │ └── input01.txt │ │ ├── Cargo.lock │ │ ├── Cargo.toml │ │ └── src │ │ │ ├── main.rs │ │ │ └── alternative.rs │ ├── covering-the-stains │ │ ├── output21.txt │ │ ├── output20.txt │ │ ├── input21.txt │ │ ├── Cargo.lock │ │ ├── input_test1.txt │ │ ├── Cargo.toml │ │ ├── src │ │ │ ├── brute_force.rs │ │ │ └── main.rs │ │ └── input20.txt │ ├── p-sequences │ │ ├── input_0.txt │ │ ├── Cargo.toml │ │ └── src │ │ │ └── main.rs │ ├── red-john-is-back │ │ ├── output00.txt │ │ ├── input00.txt │ │ ├── Cargo.lock │ │ ├── Cargo.toml │ │ └── src │ │ │ └── main.rs │ ├── stockmax │ │ ├── output10.txt │ │ ├── input_test1.txt │ │ ├── input10.txt │ │ ├── Cargo.lock │ │ ├── Cargo.toml │ │ ├── output00.txt │ │ └── src │ │ │ ├── main.rs │ │ │ └── alternatives.rs │ └── fibonacci_modified │ │ └── fib_mod.py ├── search │ └── pair │ │ ├── input.txt │ │ ├── Cargo.lock │ │ ├── Cargo.toml │ │ └── src │ │ └── main.rs └── bit_manipulation │ └── xoring-ninja │ ├── Cargo.lock │ ├── Cargo.toml │ ├── input.txt │ └── src │ └── main.rs └── reload_compile.sh /algorithms/greedy/.gitignore: -------------------------------------------------------------------------------- 1 | */target/ 2 | -------------------------------------------------------------------------------- /algorithms/greedy/crush/output00.txt: -------------------------------------------------------------------------------- 1 | 200 -------------------------------------------------------------------------------- /algorithms/sorting/.gitignore: -------------------------------------------------------------------------------- 1 | */target/ 2 | -------------------------------------------------------------------------------- /algorithms/string/.gitignore: -------------------------------------------------------------------------------- 1 | */target/ 2 | -------------------------------------------------------------------------------- /algorithms/graph_theory/.gitignore: -------------------------------------------------------------------------------- 1 | */target/ 2 | -------------------------------------------------------------------------------- /algorithms/graph_theory/even-tree/output02.txt: -------------------------------------------------------------------------------- 1 | 11 -------------------------------------------------------------------------------- /algorithms/graph_theory/even-tree/output00.txt: -------------------------------------------------------------------------------- 1 | 2 2 | -------------------------------------------------------------------------------- /algorithms/greedy/sherlock-and-minimax/output00.txt: -------------------------------------------------------------------------------- 1 | 4 -------------------------------------------------------------------------------- /algorithms/implementation/.gitignore: -------------------------------------------------------------------------------- 1 | */target/ 2 | -------------------------------------------------------------------------------- /algorithms/dynamic_programming/.gitignore: -------------------------------------------------------------------------------- 1 | */target/ 2 | -------------------------------------------------------------------------------- /algorithms/search/pair/input.txt: -------------------------------------------------------------------------------- 1 | 5 2 2 | 1 5 3 4 2 3 | -------------------------------------------------------------------------------- /algorithms/sorting/almost-sorted/output/output20.txt: -------------------------------------------------------------------------------- 1 | no -------------------------------------------------------------------------------- /algorithms/string/common-child/input01.txt: -------------------------------------------------------------------------------- 1 | AA 2 | BB 3 | -------------------------------------------------------------------------------- /algorithms/dynamic_programming/candies/output/output00.txt: -------------------------------------------------------------------------------- 1 | 4 -------------------------------------------------------------------------------- /algorithms/dynamic_programming/candies/output/output10.txt: -------------------------------------------------------------------------------- 1 | 19 -------------------------------------------------------------------------------- /algorithms/graph_theory/jeanies-route/output05.txt: -------------------------------------------------------------------------------- 1 | 1633 2 | -------------------------------------------------------------------------------- /algorithms/graph_theory/jeanies-route/output09.txt: -------------------------------------------------------------------------------- 1 | 1093490 2 | -------------------------------------------------------------------------------- /algorithms/greedy/chief-hopper/input.txt: -------------------------------------------------------------------------------- 1 | 5 2 | 3 4 3 2 4 3 | -------------------------------------------------------------------------------- /algorithms/sorting/almost-sorted/input/input00.txt: -------------------------------------------------------------------------------- 1 | 2 2 | 4 2 -------------------------------------------------------------------------------- /algorithms/sorting/almost-sorted/input/input20.txt: -------------------------------------------------------------------------------- 1 | 3 2 | 3 1 2 -------------------------------------------------------------------------------- /algorithms/string/common-child/input00.txt: -------------------------------------------------------------------------------- 1 | HARRY 2 | SALLY 3 | -------------------------------------------------------------------------------- /algorithms/dynamic_programming/coin-change/output/output00.txt: -------------------------------------------------------------------------------- 1 | 4 2 | -------------------------------------------------------------------------------- /algorithms/dynamic_programming/coin-change/output/output01.txt: -------------------------------------------------------------------------------- 1 | 5 2 | -------------------------------------------------------------------------------- /algorithms/dynamic_programming/covering-the-stains/output21.txt: -------------------------------------------------------------------------------- 1 | 8 -------------------------------------------------------------------------------- /algorithms/dynamic_programming/p-sequences/input_0.txt: -------------------------------------------------------------------------------- 1 | 2 2 | 2 3 | -------------------------------------------------------------------------------- /algorithms/dynamic_programming/red-john-is-back/output00.txt: -------------------------------------------------------------------------------- 1 | 0 2 | 3 -------------------------------------------------------------------------------- /algorithms/dynamic_programming/stockmax/output10.txt: -------------------------------------------------------------------------------- 1 | 0 2 | 197 3 | 3 -------------------------------------------------------------------------------- /algorithms/graph_theory/bfsshortreach/output00.txt: -------------------------------------------------------------------------------- 1 | 6 6 -1 2 | -1 6 -------------------------------------------------------------------------------- /algorithms/greedy/crush/input_test.txt: -------------------------------------------------------------------------------- 1 | 2 2 2 | 2 3 1 3 | 1 2 1 4 | -------------------------------------------------------------------------------- /algorithms/sorting/almost-sorted/input/input21.txt: -------------------------------------------------------------------------------- 1 | 6 2 | 1 5 4 3 2 6 -------------------------------------------------------------------------------- /algorithms/sorting/almost-sorted/output/output00.txt: -------------------------------------------------------------------------------- 1 | yes 2 | swap 1 2 -------------------------------------------------------------------------------- /algorithms/string/common-child/input02.txt: -------------------------------------------------------------------------------- 1 | SHINCHAN 2 | NOHARAAA 3 | -------------------------------------------------------------------------------- /algorithms/string/common-child/input03.txt: -------------------------------------------------------------------------------- 1 | ABCDEF 2 | FBDAMN 3 | -------------------------------------------------------------------------------- /algorithms/dynamic_programming/red-john-is-back/input00.txt: -------------------------------------------------------------------------------- 1 | 2 2 | 1 3 | 7 -------------------------------------------------------------------------------- /algorithms/sorting/almost-sorted/output/output21.txt: -------------------------------------------------------------------------------- 1 | yes 2 | reverse 2 5 -------------------------------------------------------------------------------- /algorithms/dynamic_programming/candies/input/input00.txt: -------------------------------------------------------------------------------- 1 | 3 2 | 1 3 | 2 4 | 2 -------------------------------------------------------------------------------- /algorithms/dynamic_programming/covering-the-stains/output20.txt: -------------------------------------------------------------------------------- 1 | 823066173 2 | -------------------------------------------------------------------------------- /algorithms/greedy/sherlock-and-minimax/input00.txt: -------------------------------------------------------------------------------- 1 | 3 2 | 5 14 8 3 | 4 9 4 | -------------------------------------------------------------------------------- /algorithms/implementation/matrix-rotation-algo/output/output12.txt: -------------------------------------------------------------------------------- 1 | 1 1 2 | 1 1 -------------------------------------------------------------------------------- /algorithms/greedy/crush/input00.txt: -------------------------------------------------------------------------------- 1 | 5 3 2 | 3 4 100 3 | 2 5 100 4 | 1 2 100 5 | -------------------------------------------------------------------------------- /algorithms/dynamic_programming/coin-change/input/input00.txt: -------------------------------------------------------------------------------- 1 | 4 3 2 | 1 2 3 3 | 4 | -------------------------------------------------------------------------------- /algorithms/dynamic_programming/coin-change/input/input01.txt: -------------------------------------------------------------------------------- 1 | 10 4 2 | 2 5 3 6 3 | 4 | -------------------------------------------------------------------------------- /algorithms/implementation/matrix-rotation-algo/input/input12.txt: -------------------------------------------------------------------------------- 1 | 2 2 3 2 | 1 1 3 | 1 1 -------------------------------------------------------------------------------- /algorithms/string/two-strings/input00.txt: -------------------------------------------------------------------------------- 1 | 2 2 | hello 3 | world 4 | hi 5 | world 6 | -------------------------------------------------------------------------------- /algorithms/greedy/accessory-collection/input.txt: -------------------------------------------------------------------------------- 1 | 2 2 | 6 5 3 2 3 | 2 1 2 2 4 | 6 4 3 2 5 | -------------------------------------------------------------------------------- /algorithms/greedy/crush/Cargo.lock: -------------------------------------------------------------------------------- 1 | [root] 2 | name = "crush" 3 | version = "0.1.0" 4 | 5 | -------------------------------------------------------------------------------- /algorithms/search/pair/Cargo.lock: -------------------------------------------------------------------------------- 1 | [root] 2 | name = "pair" 3 | version = "0.1.0" 4 | 5 | -------------------------------------------------------------------------------- /algorithms/dynamic_programming/stockmax/input_test1.txt: -------------------------------------------------------------------------------- 1 | 1 2 | 11 3 | 1 3 6 3 2 9 7 3 8 3 3 4 | -------------------------------------------------------------------------------- /algorithms/string/two-strings/Cargo.lock: -------------------------------------------------------------------------------- 1 | [root] 2 | name = "two-strings" 3 | version = "0.1.0" 4 | 5 | -------------------------------------------------------------------------------- /algorithms/dynamic_programming/candies/Cargo.lock: -------------------------------------------------------------------------------- 1 | [root] 2 | name = "candies" 3 | version = "0.1.0" 4 | 5 | -------------------------------------------------------------------------------- /algorithms/dynamic_programming/stockmax/input10.txt: -------------------------------------------------------------------------------- 1 | 3 2 | 3 3 | 5 3 2 4 | 3 5 | 1 2 100 6 | 4 7 | 1 3 1 2 -------------------------------------------------------------------------------- /algorithms/graph_theory/even-tree/Cargo.lock: -------------------------------------------------------------------------------- 1 | [root] 2 | name = "even-tree" 3 | version = "0.1.0" 4 | 5 | -------------------------------------------------------------------------------- /algorithms/graph_theory/jeanies-route/input.txt: -------------------------------------------------------------------------------- 1 | 5 3 2 | 1 3 4 3 | 1 2 1 4 | 2 3 2 5 | 2 4 2 6 | 3 5 3 7 | -------------------------------------------------------------------------------- /algorithms/sorting/almost-sorted/Cargo.lock: -------------------------------------------------------------------------------- 1 | [root] 2 | name = "almost-sorted" 3 | version = "0.1.0" 4 | 5 | -------------------------------------------------------------------------------- /algorithms/string/common-child/Cargo.lock: -------------------------------------------------------------------------------- 1 | [root] 2 | name = "common-child" 3 | version = "0.1.0" 4 | 5 | -------------------------------------------------------------------------------- /algorithms/dynamic_programming/covering-the-stains/input21.txt: -------------------------------------------------------------------------------- 1 | 5 2 2 | 0 1 3 | 3 3 4 | 2 0 5 | 0 3 6 | 2 3 -------------------------------------------------------------------------------- /algorithms/dynamic_programming/stockmax/Cargo.lock: -------------------------------------------------------------------------------- 1 | [root] 2 | name = "stockmax" 3 | version = "0.1.0" 4 | 5 | -------------------------------------------------------------------------------- /algorithms/graph_theory/jeanies-route/Cargo.lock: -------------------------------------------------------------------------------- 1 | [root] 2 | name = "jeanies-route" 3 | version = "0.1.0" 4 | 5 | -------------------------------------------------------------------------------- /algorithms/greedy/team-formation/Cargo.lock: -------------------------------------------------------------------------------- 1 | [root] 2 | name = "team-formation" 3 | version = "0.1.0" 4 | 5 | -------------------------------------------------------------------------------- /algorithms/bit_manipulation/xoring-ninja/Cargo.lock: -------------------------------------------------------------------------------- 1 | [root] 2 | name = "xoring-ninja" 3 | version = "0.1.0" 4 | 5 | -------------------------------------------------------------------------------- /algorithms/dynamic_programming/coin-change/Cargo.lock: -------------------------------------------------------------------------------- 1 | [root] 2 | name = "coin-change" 3 | version = "0.1.0" 4 | 5 | -------------------------------------------------------------------------------- /algorithms/graph_theory/bfsshortreach/input00.txt: -------------------------------------------------------------------------------- 1 | 2 2 | 4 2 3 | 1 2 4 | 1 3 5 | 1 6 | 3 1 7 | 2 3 8 | 5 9 | -------------------------------------------------------------------------------- /algorithms/implementation/matrix-rotation-algo/output/output00.txt: -------------------------------------------------------------------------------- 1 | 2 3 4 8 2 | 1 7 11 12 3 | 5 6 10 16 4 | 9 13 14 15 -------------------------------------------------------------------------------- /algorithms/implementation/matrix-rotation-algo/output/output10.txt: -------------------------------------------------------------------------------- 1 | 3 4 8 12 2 | 2 11 10 16 3 | 1 7 6 15 4 | 5 9 13 14 -------------------------------------------------------------------------------- /algorithms/dynamic_programming/candies/input/test_00.txt: -------------------------------------------------------------------------------- 1 | 8 2 | 2 3 | 4 4 | 6 5 | 12 6 | 10 7 | 8 8 | 4 9 | 2 10 | -------------------------------------------------------------------------------- /algorithms/greedy/accessory-collection/Cargo.lock: -------------------------------------------------------------------------------- 1 | [root] 2 | name = "accessory-collection" 3 | version = "0.1.0" 4 | 5 | -------------------------------------------------------------------------------- /algorithms/greedy/sherlock-and-minimax/Cargo.lock: -------------------------------------------------------------------------------- 1 | [root] 2 | name = "sherlock-and-minimax" 3 | version = "0.1.0" 4 | 5 | -------------------------------------------------------------------------------- /algorithms/implementation/kaprekar-numbers/Cargo.lock: -------------------------------------------------------------------------------- 1 | [root] 2 | name = "kaprekar-numbers" 3 | version = "0.1.0" 4 | 5 | -------------------------------------------------------------------------------- /algorithms/implementation/the-grid-search/Cargo.lock: -------------------------------------------------------------------------------- 1 | [root] 2 | name = "the-grid-search" 3 | version = "0.1.0" 4 | 5 | -------------------------------------------------------------------------------- /algorithms/string/sherlock-and-anagrams/Cargo.lock: -------------------------------------------------------------------------------- 1 | [root] 2 | name = "sherlock-and-anagrams" 3 | version = "0.1.0" 4 | 5 | -------------------------------------------------------------------------------- /algorithms/dynamic_programming/red-john-is-back/Cargo.lock: -------------------------------------------------------------------------------- 1 | [root] 2 | name = "red-john-is-back" 3 | version = "0.1.0" 4 | 5 | -------------------------------------------------------------------------------- /algorithms/implementation/matrix-rotation-algo/input/input00.txt: -------------------------------------------------------------------------------- 1 | 4 4 1 2 | 1 2 3 4 3 | 5 6 7 8 4 | 9 10 11 12 5 | 13 14 15 16 -------------------------------------------------------------------------------- /algorithms/implementation/matrix-rotation-algo/input/input10.txt: -------------------------------------------------------------------------------- 1 | 4 4 2 2 | 1 2 3 4 3 | 5 6 7 8 4 | 9 10 11 12 5 | 13 14 15 16 -------------------------------------------------------------------------------- /algorithms/dynamic_programming/candies/input/input10.txt: -------------------------------------------------------------------------------- 1 | 10 2 | 2 3 | 4 4 | 2 5 | 6 6 | 1 7 | 7 8 | 8 9 | 9 10 | 2 11 | 1 -------------------------------------------------------------------------------- /algorithms/dynamic_programming/covering-the-stains/Cargo.lock: -------------------------------------------------------------------------------- 1 | [root] 2 | name = "covering-the-stains" 3 | version = "0.1.0" 4 | 5 | -------------------------------------------------------------------------------- /algorithms/implementation/matrix-rotation-algo/Cargo.lock: -------------------------------------------------------------------------------- 1 | [root] 2 | name = "matrix-rotation-algo" 3 | version = "0.1.0" 4 | 5 | -------------------------------------------------------------------------------- /algorithms/string/sherlock-and-anagrams/input01.txt: -------------------------------------------------------------------------------- 1 | 5 2 | ifailuhkqq 3 | hucpoltgty 4 | ovarjsnrbf 5 | pvmupwjjjf 6 | iwwhrlkpek 7 | -------------------------------------------------------------------------------- /algorithms/graph_theory/even-tree/input00.txt: -------------------------------------------------------------------------------- 1 | 10 9 2 | 2 1 3 | 3 1 4 | 4 3 5 | 5 2 6 | 6 1 7 | 7 2 8 | 8 6 9 | 9 8 10 | 10 8 11 | -------------------------------------------------------------------------------- /algorithms/implementation/matrix-rotation-algo/output/output11.txt: -------------------------------------------------------------------------------- 1 | 28 27 26 25 2 | 22 9 15 19 3 | 16 8 21 13 4 | 10 14 20 7 5 | 4 3 2 1 -------------------------------------------------------------------------------- /algorithms/implementation/matrix-rotation-algo/input/input11.txt: -------------------------------------------------------------------------------- 1 | 5 4 7 2 | 1 2 3 4 3 | 7 8 9 10 4 | 13 14 15 16 5 | 19 20 21 22 6 | 25 26 27 28 -------------------------------------------------------------------------------- /algorithms/sorting/insertion-sort/input.txt: -------------------------------------------------------------------------------- 1 | 2 2 | 5 3 | 1 1 1 2 2 4 | 5 5 | 2 1 3 1 2 6 | 9 7 | 3 5 7 6 5 7 1 5 4 8 | 8 9 | 9 8 7 3 4 5 2 3 10 | -------------------------------------------------------------------------------- /algorithms/greedy/team-formation/input00.txt: -------------------------------------------------------------------------------- 1 | 5 2 | 7 4 5 2 3 -4 -3 -5 3 | 1 -4 4 | 4 3 2 3 1 5 | 7 1 -2 -3 -4 2 0 -1 6 | 6 1 2 3 1 2 3 7 | 9 1 2 2 3 3 3 4 4 5 8 | -------------------------------------------------------------------------------- /algorithms/search/pair/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "pair" 3 | version = "0.1.0" 4 | authors = ["Federico Giraud "] 5 | 6 | [dependencies] 7 | -------------------------------------------------------------------------------- /algorithms/dynamic_programming/covering-the-stains/input_test1.txt: -------------------------------------------------------------------------------- 1 | 11 4 2 | 0 0 3 | 2 0 4 | 4 0 5 | 3 1 6 | 0 2 7 | 2 2 8 | 4 2 9 | 1 3 10 | 0 4 11 | 2 4 12 | 4 4 13 | -------------------------------------------------------------------------------- /algorithms/greedy/crush/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "crush" 3 | version = "0.1.0" 4 | authors = ["Federico Giraud "] 5 | 6 | [dependencies] 7 | -------------------------------------------------------------------------------- /algorithms/string/two-strings/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "two-strings" 3 | version = "0.1.0" 4 | authors = ["Federico Giraud "] 5 | 6 | [dependencies] 7 | -------------------------------------------------------------------------------- /algorithms/dynamic_programming/candies/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "candies" 3 | version = "0.1.0" 4 | authors = ["Federico Giraud "] 5 | 6 | [dependencies] 7 | -------------------------------------------------------------------------------- /algorithms/graph_theory/even-tree/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "even-tree" 3 | version = "0.1.0" 4 | authors = ["Federico Giraud "] 5 | 6 | [dependencies] 7 | -------------------------------------------------------------------------------- /algorithms/greedy/team-formation/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "team-formation" 3 | version = "0.1.0" 4 | authors = ["Federico Giraud "] 5 | 6 | [dependencies] 7 | -------------------------------------------------------------------------------- /algorithms/sorting/almost-sorted/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "almost-sorted" 3 | version = "0.1.0" 4 | authors = ["Federico Giraud "] 5 | 6 | [dependencies] 7 | -------------------------------------------------------------------------------- /algorithms/string/common-child/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "common-child" 3 | version = "0.1.0" 4 | authors = ["Federico Giraud "] 5 | 6 | [dependencies] 7 | -------------------------------------------------------------------------------- /algorithms/dynamic_programming/stockmax/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "stockmax" 3 | version = "0.1.0" 4 | authors = ["Federico Giraud "] 5 | 6 | [dependencies] 7 | -------------------------------------------------------------------------------- /algorithms/graph_theory/jeanies-route/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "jeanies-route" 3 | version = "0.1.0" 4 | authors = ["Federico Giraud "] 5 | 6 | [dependencies] 7 | -------------------------------------------------------------------------------- /algorithms/greedy/accessory-collection/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "accessory-collection" 3 | version = "0.1.0" 4 | authors = ["Federico Giraud "] 5 | 6 | [dependencies] 7 | -------------------------------------------------------------------------------- /algorithms/sorting/insertion-sort/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "insertion-sort" 3 | version = "0.1.0" 4 | authors = ["Federico Giraud "] 5 | 6 | [dependencies] 7 | -------------------------------------------------------------------------------- /algorithms/bit_manipulation/xoring-ninja/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "xoring-ninja" 3 | version = "0.1.0" 4 | authors = ["Federico Giraud "] 5 | 6 | [dependencies] 7 | -------------------------------------------------------------------------------- /algorithms/dynamic_programming/coin-change/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "coin-change" 3 | version = "0.1.0" 4 | authors = ["Federico Giraud "] 5 | 6 | [dependencies] 7 | -------------------------------------------------------------------------------- /algorithms/dynamic_programming/p-sequences/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "p-sequences" 3 | version = "0.1.0" 4 | authors = ["Federico Giraud "] 5 | 6 | [dependencies] 7 | -------------------------------------------------------------------------------- /algorithms/dynamic_programming/stockmax/output00.txt: -------------------------------------------------------------------------------- 1 | 1970369240 2 | 583846419 3 | 443869051 4 | 2337671690 5 | 1936167774 6 | 942199160 7 | 1434469905 8 | 1327299960 9 | 1045435760 10 | 979622844 -------------------------------------------------------------------------------- /algorithms/implementation/the-grid-search/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "the-grid-search" 3 | version = "0.1.0" 4 | authors = ["Federico Giraud "] 5 | 6 | [dependencies] 7 | -------------------------------------------------------------------------------- /algorithms/greedy/sherlock-and-minimax/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "sherlock-and-minimax" 3 | version = "0.1.0" 4 | authors = ["Federico Giraud "] 5 | 6 | [dependencies] 7 | -------------------------------------------------------------------------------- /algorithms/implementation/kaprekar-numbers/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "kaprekar-numbers" 3 | version = "0.1.0" 4 | authors = ["Federico Giraud "] 5 | 6 | [dependencies] 7 | -------------------------------------------------------------------------------- /algorithms/string/sherlock-and-anagrams/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "sherlock-and-anagrams" 3 | version = "0.1.0" 4 | authors = ["Federico Giraud "] 5 | 6 | [dependencies] 7 | -------------------------------------------------------------------------------- /algorithms/dynamic_programming/red-john-is-back/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "red-john-is-back" 3 | version = "0.1.0" 4 | authors = ["Federico Giraud "] 5 | 6 | [dependencies] 7 | -------------------------------------------------------------------------------- /algorithms/implementation/matrix-rotation-algo/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "matrix-rotation-algo" 3 | version = "0.1.0" 4 | authors = ["Federico Giraud "] 5 | 6 | [dependencies] 7 | -------------------------------------------------------------------------------- /algorithms/dynamic_programming/covering-the-stains/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "covering-the-stains" 3 | version = "0.1.0" 4 | authors = ["Federico Giraud "] 5 | 6 | [dependencies] 7 | -------------------------------------------------------------------------------- /algorithms/graph_theory/bfsshortreach/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "bfsshortreach" 3 | version = "0.1.0" 4 | authors = ["Federico Giraud "] 5 | 6 | [dependencies] 7 | clippy='0.0.63' 8 | -------------------------------------------------------------------------------- /algorithms/graph_theory/dijkstrashortreach/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "dijkstrashortreach" 3 | version = "0.1.0" 4 | authors = ["Federico Giraud "] 5 | 6 | [dependencies] 7 | clippy="0.0.63" 8 | -------------------------------------------------------------------------------- /algorithms/bit_manipulation/xoring-ninja/input.txt: -------------------------------------------------------------------------------- 1 | 1 2 | 3 3 | 1 2 3 4 | 5 5 | 2 45 3 232 4 6 | 9 7 | 98 2934 234 2 12 5 4 6 25 23 1 23 4 5 3 56 4 12 43 125 34 5 6543 62 5 1 42 43 5324 6 643 57 657 3 452 35 14 214 215435 2 900009990 777790000 8 | -------------------------------------------------------------------------------- /algorithms/dynamic_programming/fibonacci_modified/fib_mod.py: -------------------------------------------------------------------------------- 1 | def fib_mod(A, B, n): 2 | a = A 3 | b = B 4 | for i in range(n-2): 5 | a, b = b, b*b + a 6 | return b 7 | 8 | params = [int(x) for x in raw_input().split(" ")] 9 | print fib_mod(*params) 10 | -------------------------------------------------------------------------------- /algorithms/graph_theory/dijkstrashortreach/input00.txt: -------------------------------------------------------------------------------- 1 | 3 2 | 4 5 3 | 1 2 24 4 | 1 4 20 5 | 3 1 3 6 | 3 1 10 7 | 4 3 12 8 | 1 9 | 5 5 10 | 1 2 24 11 | 1 4 20 12 | 3 1 3 13 | 3 1 10 14 | 4 3 12 15 | 1 16 | 10 9 17 | 1 2 24 18 | 1 4 20 19 | 3 1 3 20 | 3 1 10 21 | 4 3 12 22 | 3 3 1 23 | 5 9 3 24 | 9 8 3 25 | 9 8 12 26 | 9 27 | -------------------------------------------------------------------------------- /algorithms/graph_theory/even-tree/input02.txt: -------------------------------------------------------------------------------- 1 | 30 29 2 | 2 1 3 | 3 2 4 | 4 3 5 | 5 2 6 | 6 4 7 | 7 4 8 | 8 1 9 | 9 5 10 | 10 4 11 | 11 4 12 | 12 8 13 | 13 2 14 | 14 2 15 | 15 8 16 | 16 10 17 | 17 1 18 | 18 17 19 | 19 18 20 | 20 4 21 | 21 15 22 | 22 20 23 | 23 2 24 | 24 12 25 | 25 21 26 | 26 17 27 | 27 5 28 | 28 27 29 | 29 4 30 | 30 25 -------------------------------------------------------------------------------- /algorithms/greedy/chief-hopper/src.py: -------------------------------------------------------------------------------- 1 | ## B(n+1) += B(n) - H(n+1) 2 | ## B(n+1) = 2B(n) - H(n+1) 3 | ## B(n+2) = 4B(n) - 2H(n+1) - H(n+2) 4 | ## B(n+3) = 8B(n) - 4H(n+1) - 2H(n+2) - H(n+3) 5 | ## 6 | ## => B(n) >= 1/2H(n+1) + 1/4H(n+1) + 1/8(n+2) ... 7 | 8 | from decimal import * 9 | 10 | raw_input() 11 | nums = raw_input().split(" ") 12 | 13 | tot = 0 14 | 15 | for n in nums: 16 | tot = tot*2 + int(n) 17 | 18 | with localcontext() as ctx: 19 | ctx.prec = 35000 20 | res = Decimal(tot) / Decimal(2 ** len(nums)) 21 | print res.to_integral_exact(rounding=ROUND_CEILING) 22 | -------------------------------------------------------------------------------- /algorithms/implementation/the-grid-search/input.txt: -------------------------------------------------------------------------------- 1 | 2 2 | 10 10 3 | 7283455864 4 | 6731158619 5 | 8988242643 6 | 3830589324 7 | 2229505813 8 | 5633845374 9 | 6473530293 10 | 7053106601 11 | 0834282956 12 | 4607924137 13 | 3 4 14 | 9505 15 | 3845 16 | 3530 17 | 15 15 18 | 400453592126560 19 | 114213133098692 20 | 474386082879648 21 | 522356951189169 22 | 887109450487496 23 | 252802633388782 24 | 502771484966748 25 | 075975207693780 26 | 511799789562806 27 | 404007454272504 28 | 549043809916080 29 | 962410809534811 30 | 445893523733475 31 | 768705303214174 32 | 650629270887160 33 | 2 2 34 | 99 35 | 99 36 | -------------------------------------------------------------------------------- /algorithms/string/two-strings/src/main.rs: -------------------------------------------------------------------------------- 1 | use std::collections::HashSet; 2 | use std::io::prelude::*; 3 | use std::io; 4 | 5 | fn read_input() -> Vec<(String, String)> { 6 | let stdin = io::stdin(); 7 | let content = stdin.lock() 8 | .lines() 9 | .skip(1) 10 | .map(|line| { line.expect("Input not readable") }) 11 | .collect::>(); 12 | 13 | content.chunks(2).map(|x| (x[0].clone(), x[1].clone())).collect::>() 14 | } 15 | 16 | fn main() { 17 | for (a, b) in read_input() { 18 | let chars_set = a.chars().collect::>(); 19 | if b.chars().find(|&x| chars_set.contains(&x)).is_some() { 20 | println!("YES"); 21 | } else { 22 | println!("NO"); 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /reload_compile.sh: -------------------------------------------------------------------------------- 1 | #! /bin/bash 2 | 3 | # Set a watcher on the current directory. If the source files are modified, 4 | # recompile the project. 5 | 6 | PROJECT_DIR="$(pwd)" 7 | SOURCE_DIR="$PROJECT_DIR/src" 8 | 9 | echo $SOURCE_DIR 10 | 11 | GREEN='\033[0;32m' 12 | RED='\033[0;31m' 13 | NC='\033[0m' # No Color 14 | 15 | function wait_for_change { 16 | inotifywait -r -qq \ 17 | -e modify,move,create,delete \ 18 | $SOURCE_DIR 19 | } 20 | 21 | function build { 22 | echo -e "\n$GREEN *** STARTING BUILD ***$NC" 23 | cd $PROJECT_DIR; cargo build 24 | if [ $? -eq 0 ]; then 25 | echo -e "$GREEN *** BUILD COMPLETE ***$NC" 26 | else 27 | echo -e "$RED *** BUILD FAILED ***$NC" 28 | fi 29 | } 30 | 31 | build 32 | 33 | while wait_for_change; do 34 | sleep 0.25; 35 | build 36 | done 37 | -------------------------------------------------------------------------------- /algorithms/implementation/kaprekar-numbers/src/main.rs: -------------------------------------------------------------------------------- 1 | use std::io; 2 | use std::io::prelude::*; 3 | 4 | fn is_kp(n: &i64) -> bool { 5 | let d = format!("{}", n).len(); 6 | let sq = format!("{}", n*n); 7 | let r = &sq[sq.len()-d..]; 8 | let l = &sq[..(sq.len()-d)]; 9 | r.parse::().unwrap() + l.parse::().unwrap_or(0) == *n 10 | } 11 | 12 | fn main() { 13 | let stdin = io::stdin(); 14 | let mut lines = stdin.lock().lines().map(|x| x.unwrap()); 15 | 16 | let start = lines.next().unwrap().parse::().unwrap(); 17 | let end = lines.next().unwrap().parse::().unwrap(); 18 | 19 | let kn = (start..(end+1)).filter(|n| is_kp(n)).collect::>(); 20 | 21 | if kn.is_empty() { 22 | println!("INVALID RANGE"); 23 | return; 24 | } 25 | 26 | for n in kn { 27 | print!("{} ", n); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /algorithms/search/pair/src/main.rs: -------------------------------------------------------------------------------- 1 | use std::collections::HashSet; 2 | use std::io::prelude::*; 3 | use std::io; 4 | 5 | fn read_input() -> (i32, Vec) { 6 | let stdin = io::stdin(); 7 | let mut content = stdin.lock() 8 | .lines() 9 | .map(|line| { 10 | line.expect("Input not readable") 11 | .split(" ") 12 | .map(|n| n.parse::().expect("Can't parse integer")) 13 | .collect::>() 14 | }); 15 | (content.next().unwrap()[1], content.next().unwrap()) 16 | } 17 | 18 | fn main() { 19 | let (k, seq) = read_input(); 20 | let mut set = HashSet::new(); 21 | let mut res = 0; 22 | 23 | for a in seq { 24 | if set.contains(&(a - k)) { 25 | res += 1; 26 | } 27 | if set.contains(&(a + k)) { 28 | res += 1; 29 | } 30 | set.insert(a); 31 | } 32 | 33 | println!("{}", res); 34 | } 35 | -------------------------------------------------------------------------------- /algorithms/greedy/crush/src/main.rs: -------------------------------------------------------------------------------- 1 | use std::cmp::max; 2 | use std::io::prelude::*; 3 | use std::io; 4 | 5 | fn read_input() -> Vec<(i64, i8, i64)> { 6 | let stdin = io::stdin(); 7 | let content = stdin.lock() 8 | .lines() 9 | .skip(1) 10 | .map(|line| { 11 | line.expect("Input not readable") 12 | .split(" ") 13 | .map(|n| n.parse::().expect("Can't parse integer")) 14 | .collect::>() 15 | }); 16 | let mut steps = Vec::new(); 17 | for line in content { 18 | steps.push((line[0], 0, line[2])); // step up 19 | steps.push((line[1], 1, -line[2])); // step down 20 | } 21 | steps.sort_by(|a, b| (a.0, a.1).cmp(&(b.0, b.1))); 22 | steps 23 | } 24 | 25 | fn main() { 26 | let res = read_input().iter() 27 | .fold((std::i64::MIN, 0), |(res, curr), &(_, _, v)| (max(res, curr + v), curr + v)).0; 28 | println!("{}", res); 29 | } 30 | -------------------------------------------------------------------------------- /algorithms/dynamic_programming/stockmax/src/main.rs: -------------------------------------------------------------------------------- 1 | use std::io::prelude::*; 2 | use std::io; 3 | 4 | fn read_input() -> Vec> { 5 | let stdin = io::stdin(); 6 | let content = stdin.lock() 7 | .lines() 8 | .skip(1) 9 | .enumerate() 10 | .filter(|&(n, _)| n % 2 == 1) 11 | .map(|(_, line)| { 12 | line.expect("Input not readable") 13 | .split(" ") 14 | .map(|n| n.parse::().expect("Can't parse integer")) 15 | .collect::>() 16 | }); 17 | content.collect() 18 | } 19 | 20 | fn calc_test(v: &[i64]) -> i64 { 21 | if v.is_empty() { 22 | return 0; 23 | } 24 | 25 | let last_max = v.iter().enumerate().fold(0, |m, (n, &x)| if x >= v[m] { n } else { m }); 26 | let spent = v.iter().take(last_max).fold(0, |acc, n| acc + n); 27 | let earned = last_max as i64 * v[last_max]; 28 | 29 | earned - spent + calc_test(&v[last_max+1..]) 30 | } 31 | 32 | fn main() { 33 | for input in read_input() { 34 | println!("{}", calc_test(&input)); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /algorithms/string/sherlock-and-anagrams/src/main.rs: -------------------------------------------------------------------------------- 1 | use std::collections::HashMap; 2 | use std::io::prelude::*; 3 | use std::io; 4 | 5 | fn read_input() -> Vec { 6 | let stdin = io::stdin(); 7 | let lines = stdin.lock().lines().skip(1).map(|x| x.expect("Input not readable")); 8 | lines.collect::>() 9 | } 10 | 11 | 12 | fn positions(length: usize) -> Vec<(usize, usize, usize, usize)> { 13 | let mut pos = Vec::new(); 14 | 15 | for l in 1..length { 16 | for i in 0..(length-l) { 17 | for j in (i+1)..(length-l+1) { 18 | pos.push((i, i+l, j, j+l)); 19 | } 20 | } 21 | } 22 | 23 | pos 24 | } 25 | 26 | fn counter(s: &[u8]) -> HashMap { 27 | let mut map = HashMap::new(); 28 | 29 | for &b in s { *map.entry(b).or_insert(0) += 1 } 30 | 31 | map 32 | } 33 | 34 | fn main() { 35 | for line in read_input() { 36 | let bytes = line.as_bytes(); 37 | let tot = positions(bytes.len()).iter() 38 | .filter(|&&(a, b, c, d)| counter(&bytes[a..b]) == counter(&bytes[c..d])) 39 | .count(); 40 | println!("{}", tot); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /algorithms/dynamic_programming/candies/src/main.rs: -------------------------------------------------------------------------------- 1 | use std::cmp::max; 2 | use std::io::prelude::*; 3 | use std::io; 4 | 5 | fn read_input() -> Vec { 6 | let stdin = io::stdin(); 7 | let numbers = stdin.lock() 8 | .lines() 9 | .map(|x| { 10 | x.expect("Input not readable") 11 | .parse::() 12 | .expect("Integer not readable") 13 | }) 14 | .skip(1) 15 | .collect::>(); 16 | numbers 17 | } 18 | 19 | fn rank(v: &Vec) -> Vec { 20 | let mut ranking = Vec::new(); 21 | let mut seq = 1; 22 | 23 | ranking.push(1); 24 | for i in 1..v.len() { 25 | seq = match v[i] > v[i - 1] { 26 | true => seq + 1, 27 | false => 1, 28 | }; 29 | ranking.push(seq); 30 | } 31 | 32 | ranking 33 | } 34 | 35 | fn rev_clone(v: &Vec) -> Vec { 36 | v.iter().rev().cloned().collect::>() 37 | } 38 | 39 | fn main() { 40 | let v = read_input(); 41 | let r1 = rank(&v); 42 | let r2 = rev_clone(&rank(&rev_clone(&v))); 43 | let sol = r1.iter().zip(&r2).map(|(v1, v2)| max(v1, v2)).fold(0, |acc, n| acc + n); 44 | println!("{}", sol); 45 | } 46 | -------------------------------------------------------------------------------- /algorithms/dynamic_programming/coin-change/src/main.rs: -------------------------------------------------------------------------------- 1 | use std::collections::HashMap; 2 | use std::io::prelude::*; 3 | use std::io; 4 | 5 | fn read_input() -> (i64, Vec) { 6 | let stdin = io::stdin(); 7 | let lines = stdin.lock().lines().map(|x| x.expect("Input not readable")); 8 | let content = lines.map(|line| { 9 | line.split(' ') 10 | .filter(|n| !n.is_empty()) 11 | .map(|n| n.parse::().unwrap()) 12 | .collect::>() 13 | }) 14 | .collect::>(); 15 | (content[0][0], content[1].clone()) 16 | } 17 | 18 | fn calc(tot: i64, coins: &[i64], index: usize, cache: &mut HashMap<(i64, usize), i64>) -> i64 { 19 | if tot == 0 { 20 | return 1; 21 | } 22 | if tot < 0 || coins.is_empty() || index == coins.len() { 23 | return 0; 24 | } 25 | 26 | if let Some(&n) = cache.get(&(tot, index)) { 27 | return n; 28 | } 29 | 30 | let sol = calc(tot - coins[index], coins, index, cache) + calc(tot, coins, index + 1, cache); 31 | cache.insert((tot, index), sol); 32 | sol 33 | } 34 | 35 | fn main() { 36 | let (tot, coins) = read_input(); 37 | let mut cache = HashMap::new(); 38 | println!("{}", calc(tot, &coins, 0, &mut cache)); 39 | } 40 | -------------------------------------------------------------------------------- /algorithms/dynamic_programming/p-sequences/src/main.rs: -------------------------------------------------------------------------------- 1 | use std::collections::HashMap; 2 | use std::io::prelude::*; 3 | 4 | const MOD: u64 = 1000000007; 5 | 6 | use std::io; 7 | 8 | fn read_input() -> (u64, u64) { 9 | let stdin = io::stdin(); 10 | let content = stdin.lock() 11 | .lines() 12 | .map(|line| { 13 | line.expect("Input not readable") 14 | .split(" ") 15 | .map(|n| n.parse::().expect("Can't parse integer")) 16 | .collect::>() 17 | }) 18 | .collect::>(); 19 | (content[0][0], content[1][0]) 20 | } 21 | 22 | fn calc_mem(n: i64, p: u64, prev: u64, cache: &mut Vec>) -> u64 { 23 | let mut tot = 0; 24 | let l = p/prev; 25 | 26 | if n == 0 { 27 | return 1; 28 | } 29 | 30 | if let Some(&res) = cache[n as usize].get(&l) { 31 | return res; 32 | } 33 | 34 | for i in 1..(l+1) { 35 | tot += calc_mem(n-1, p, i, cache); 36 | tot %= MOD; 37 | } 38 | 39 | cache[n as usize].insert(l, tot); 40 | 41 | tot 42 | } 43 | 44 | 45 | fn main() { 46 | let (n, p) = read_input(); 47 | let mut caches = vec![HashMap::new(); (n+1) as usize]; 48 | 49 | println!("{}", calc_mem(n as i64, p, 1, &mut caches)); 50 | } 51 | -------------------------------------------------------------------------------- /algorithms/greedy/sherlock-and-minimax/src/main.rs: -------------------------------------------------------------------------------- 1 | use std::io::prelude::*; 2 | use std::io; 3 | 4 | fn read_input() -> (Vec, i32, i32) { 5 | let stdin = io::stdin(); 6 | let content = stdin.lock() 7 | .lines() 8 | .map(|line| { 9 | line.expect("Input not readable") 10 | .split(" ") 11 | .map(|n| n.parse::().expect("Can't parse integer")) 12 | .collect::>() 13 | }) 14 | .collect::>(); 15 | (content[1].clone(), content[2][0], content[2][1]) 16 | } 17 | 18 | fn find_closest(vec: &Vec, n: i32) -> i32 { 19 | vec.iter().min_by_key(|&x| (n - x).abs()).cloned().unwrap() 20 | } 21 | 22 | fn main() { 23 | let (mut vec, p, q) = read_input(); 24 | vec.sort(); 25 | 26 | let mut pairs = vec.iter() 27 | .zip(vec.iter().skip(1)) 28 | .map(|(&a, &b)| ((a + b) / 2, (b - a) / 2)) 29 | .filter(|&(m, _)| p <= m && m <= q) 30 | .collect::>(); 31 | 32 | pairs.push((p, (p - find_closest(&vec, p)).abs())); 33 | pairs.push((q, (q - find_closest(&vec, q)).abs())); 34 | 35 | pairs.sort_by_key(|&(_, dist)| -dist); 36 | 37 | let sol = pairs.iter() 38 | .filter(|&&(_, dist)| dist == pairs[0].1) 39 | .min_by_key(|&&(m, _)| m); 40 | 41 | println!("{}", sol.unwrap().0); 42 | } 43 | -------------------------------------------------------------------------------- /algorithms/string/common-child/src/main.rs: -------------------------------------------------------------------------------- 1 | use std::cmp::max; 2 | use std::io::prelude::*; 3 | use std::io; 4 | 5 | const MAX_SIZE: usize = 5000; 6 | 7 | fn read_input() -> (Vec, Vec) { 8 | let stdin = io::stdin(); 9 | let content = stdin.lock() 10 | .lines() 11 | .map(|line| line.expect("Input not readable").as_bytes().to_vec()) 12 | .collect::>(); 13 | (content[0].clone(), content[1].clone()) 14 | } 15 | 16 | struct Finder { 17 | cache: Vec>, 18 | } 19 | 20 | impl Finder { 21 | fn new() -> Finder { 22 | Finder { cache: vec![None; MAX_SIZE*MAX_SIZE] } 23 | } 24 | 25 | fn find_common(&mut self, a: &[u8], b: &[u8], pos_a: usize, pos_b: usize) -> u16 { 26 | if pos_a == a.len() || pos_b == b.len() { 27 | return 0; 28 | } 29 | 30 | if let Some(c) = self.cache[pos_a * MAX_SIZE + pos_b] { 31 | return c; 32 | } 33 | 34 | let res = if a[pos_a] == b[pos_b] { 35 | 1 + self.find_common(a, b, pos_a + 1, pos_b + 1) 36 | } else { 37 | max(self.find_common(a, b, pos_a + 1, pos_b), 38 | self.find_common(a, b, pos_a, pos_b + 1)) 39 | }; 40 | 41 | self.cache[pos_a * MAX_SIZE + pos_b] = Some(res); 42 | res 43 | } 44 | } 45 | 46 | 47 | fn main() { 48 | let (str_a, str_b) = read_input(); 49 | 50 | let mut finder = Finder::new(); 51 | let result = finder.find_common(&str_a, &str_b, 0, 0); 52 | println!("{}", result); 53 | } 54 | -------------------------------------------------------------------------------- /algorithms/dynamic_programming/red-john-is-back/src/main.rs: -------------------------------------------------------------------------------- 1 | use std::collections::HashMap; 2 | use std::io::prelude::*; 3 | use std::io; 4 | 5 | fn read_input() -> Vec { 6 | let stdin = io::stdin(); 7 | let content = stdin.lock() 8 | .lines() 9 | .skip(1) 10 | .map(|line| { 11 | line.expect("Input not readable") 12 | .parse::().expect("Can't parse integer") 13 | }); 14 | content.collect() 15 | } 16 | 17 | fn calc(x: i64, cache: &mut HashMap) -> i64 { 18 | if x == 0 { 19 | return 1; 20 | } 21 | 22 | if let Some(&res) = cache.get(&x) { 23 | return res; 24 | } 25 | 26 | let res = calc(x-1, cache) + if x >= 4 { calc(x-4, cache) } else { 0 }; 27 | cache.insert(x, res); 28 | res 29 | } 30 | 31 | // Modified version of http://code.activestate.com/recipes/117119/ 32 | fn sieve_counter(n: i64) -> i64 { 33 | let mut q = 2; 34 | let mut d = HashMap::new(); 35 | let mut res = 0; 36 | 37 | loop { 38 | if let Some(p) = d.remove(&q) { 39 | let mut x = p + q; 40 | while d.contains_key(&x) { 41 | x += p; 42 | } 43 | d.insert(x, p); 44 | } else { 45 | if q > n { 46 | return res; 47 | } 48 | d.insert(q * q, q); 49 | res += 1; 50 | } 51 | q += 1; 52 | } 53 | } 54 | 55 | fn main() { 56 | let sizes = read_input(); 57 | let mut cache = HashMap::new(); 58 | 59 | for size in sizes { 60 | println!("{}", sieve_counter(calc(size, &mut cache))); 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /algorithms/sorting/almost-sorted/src/main.rs: -------------------------------------------------------------------------------- 1 | use std::io; 2 | use std::io::prelude::*; 3 | 4 | fn read_array() -> Vec { 5 | let stdin = io::stdin(); 6 | let line = stdin.lock().lines().skip(1).next().unwrap().unwrap(); 7 | line.split(" ").map(|s| s.parse::().unwrap()).collect::>() 8 | } 9 | 10 | fn find_indexes(array: &Vec) -> Option<(usize, usize)> { 11 | for f in 0..(array.len()-1) { 12 | if array[f] > array[f+1] { 13 | for l in ((f+1)..array.len()).rev() { 14 | if array[l] < array[l-1] { 15 | return Some((f, l)) 16 | } 17 | } 18 | break; 19 | } 20 | } 21 | return None 22 | } 23 | 24 | fn is_sorted(array: &Vec) -> bool { 25 | for i in 0..(array.len()-1) { 26 | if array[i] > array[i+1] { 27 | return false; 28 | } 29 | } 30 | true 31 | } 32 | 33 | fn try_swap(array: &Vec, (f, l): (usize, usize)) -> bool { 34 | let mut new = array.clone(); 35 | new.swap(f, l); 36 | is_sorted(&new) 37 | } 38 | 39 | fn try_reverse(array: &Vec, (f, l): (usize, usize)) -> bool { 40 | let mut new = array.clone(); 41 | new[f..(l+1)].reverse(); 42 | is_sorted(&new) 43 | } 44 | 45 | fn main() { 46 | let array = read_array(); 47 | 48 | if let Some(indexes) = find_indexes(&array) { 49 | if try_swap(&array, indexes) { 50 | println!("yes\nswap {} {}", indexes.0+1, indexes.1+1); 51 | return; 52 | } 53 | if try_reverse(&array, indexes) { 54 | println!("yes\nreverse {} {}", indexes.0+1, indexes.1+1); 55 | return; 56 | } 57 | } 58 | println!("no"); 59 | } 60 | -------------------------------------------------------------------------------- /algorithms/dynamic_programming/coin-change/src/alternative.rs: -------------------------------------------------------------------------------- 1 | use std::collections::HashMap; 2 | use std::collections::HashSet; 3 | use std::io::prelude::*; 4 | use std::io; 5 | 6 | fn read_input() -> (i32, HashSet) { 7 | let stdin = io::stdin(); 8 | let lines = stdin.lock().lines().map(|x| x.expect("Input not readable")); 9 | let content = lines.map(|line| { 10 | line.split(' ') 11 | .filter(|n| !n.is_empty()) 12 | .map(|n| n.parse::().unwrap()) 13 | .collect::>() 14 | }) 15 | .collect::>(); 16 | (content[0][0], content[1].iter().cloned().collect()) 17 | } 18 | 19 | fn calc(tot: i32, coins: &HashSet, cache: &mut HashMap>>) -> HashSet> { 20 | let mut res = HashSet::new(); 21 | 22 | if let Some(s) = cache.get(&tot) { 23 | return s.clone(); 24 | } 25 | 26 | if coins.contains(&tot) { 27 | let mut sol = Vec::new(); 28 | sol.push(tot); 29 | res.insert(sol); 30 | } 31 | 32 | for sub in 1..(tot/2 + 1) { 33 | let subset1 = calc(sub, coins, cache); 34 | if subset1.is_empty() { 35 | continue; 36 | } 37 | let subset2 = calc(tot-sub, coins, cache); 38 | for e1 in &subset1 { 39 | for e2 in &subset2 { 40 | let mut sol = Vec::new(); 41 | sol.extend(e1); 42 | sol.extend(e2); 43 | sol.sort(); 44 | res.insert(sol); 45 | } 46 | } 47 | } 48 | 49 | cache.insert(tot, res.clone()); 50 | res 51 | } 52 | 53 | fn main() { 54 | let (tot, coins) = read_input(); 55 | let mut cache: HashMap>> = HashMap::new(); 56 | 57 | println!("{}", calc(tot, &coins, &mut cache).len()); 58 | } 59 | -------------------------------------------------------------------------------- /algorithms/graph_theory/bfsshortreach/Cargo.lock: -------------------------------------------------------------------------------- 1 | [root] 2 | name = "bfsshortreach" 3 | version = "0.1.0" 4 | dependencies = [ 5 | "clippy 0.0.63 (registry+https://github.com/rust-lang/crates.io-index)", 6 | ] 7 | 8 | [[package]] 9 | name = "clippy" 10 | version = "0.0.63" 11 | source = "registry+https://github.com/rust-lang/crates.io-index" 12 | dependencies = [ 13 | "quine-mc_cluskey 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", 14 | "regex-syntax 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", 15 | "semver 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", 16 | "toml 0.1.30 (registry+https://github.com/rust-lang/crates.io-index)", 17 | "unicode-normalization 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", 18 | ] 19 | 20 | [[package]] 21 | name = "nom" 22 | version = "1.2.3" 23 | source = "registry+https://github.com/rust-lang/crates.io-index" 24 | 25 | [[package]] 26 | name = "quine-mc_cluskey" 27 | version = "0.2.2" 28 | source = "registry+https://github.com/rust-lang/crates.io-index" 29 | 30 | [[package]] 31 | name = "regex-syntax" 32 | version = "0.3.2" 33 | source = "registry+https://github.com/rust-lang/crates.io-index" 34 | 35 | [[package]] 36 | name = "rustc-serialize" 37 | version = "0.3.19" 38 | source = "registry+https://github.com/rust-lang/crates.io-index" 39 | 40 | [[package]] 41 | name = "semver" 42 | version = "0.2.3" 43 | source = "registry+https://github.com/rust-lang/crates.io-index" 44 | dependencies = [ 45 | "nom 1.2.3 (registry+https://github.com/rust-lang/crates.io-index)", 46 | ] 47 | 48 | [[package]] 49 | name = "toml" 50 | version = "0.1.30" 51 | source = "registry+https://github.com/rust-lang/crates.io-index" 52 | dependencies = [ 53 | "rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)", 54 | ] 55 | 56 | [[package]] 57 | name = "unicode-normalization" 58 | version = "0.1.2" 59 | source = "registry+https://github.com/rust-lang/crates.io-index" 60 | 61 | -------------------------------------------------------------------------------- /algorithms/graph_theory/dijkstrashortreach/Cargo.lock: -------------------------------------------------------------------------------- 1 | [root] 2 | name = "dijkstrashortreach" 3 | version = "0.1.0" 4 | dependencies = [ 5 | "clippy 0.0.63 (registry+https://github.com/rust-lang/crates.io-index)", 6 | ] 7 | 8 | [[package]] 9 | name = "clippy" 10 | version = "0.0.63" 11 | source = "registry+https://github.com/rust-lang/crates.io-index" 12 | dependencies = [ 13 | "quine-mc_cluskey 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", 14 | "regex-syntax 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", 15 | "semver 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", 16 | "toml 0.1.30 (registry+https://github.com/rust-lang/crates.io-index)", 17 | "unicode-normalization 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", 18 | ] 19 | 20 | [[package]] 21 | name = "nom" 22 | version = "1.2.3" 23 | source = "registry+https://github.com/rust-lang/crates.io-index" 24 | 25 | [[package]] 26 | name = "quine-mc_cluskey" 27 | version = "0.2.2" 28 | source = "registry+https://github.com/rust-lang/crates.io-index" 29 | 30 | [[package]] 31 | name = "regex-syntax" 32 | version = "0.3.2" 33 | source = "registry+https://github.com/rust-lang/crates.io-index" 34 | 35 | [[package]] 36 | name = "rustc-serialize" 37 | version = "0.3.19" 38 | source = "registry+https://github.com/rust-lang/crates.io-index" 39 | 40 | [[package]] 41 | name = "semver" 42 | version = "0.2.3" 43 | source = "registry+https://github.com/rust-lang/crates.io-index" 44 | dependencies = [ 45 | "nom 1.2.3 (registry+https://github.com/rust-lang/crates.io-index)", 46 | ] 47 | 48 | [[package]] 49 | name = "toml" 50 | version = "0.1.30" 51 | source = "registry+https://github.com/rust-lang/crates.io-index" 52 | dependencies = [ 53 | "rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)", 54 | ] 55 | 56 | [[package]] 57 | name = "unicode-normalization" 58 | version = "0.1.2" 59 | source = "registry+https://github.com/rust-lang/crates.io-index" 60 | 61 | -------------------------------------------------------------------------------- /algorithms/greedy/team-formation/src/main.rs: -------------------------------------------------------------------------------- 1 | use std::cell::Cell; 2 | use std::collections::BTreeMap; 3 | use std::cmp::min; 4 | use std::io::prelude::*; 5 | use std::io; 6 | 7 | fn read_input() -> Vec>> { 8 | let stdin = io::stdin(); 9 | let content = stdin.lock() 10 | .lines() 11 | .skip(1) 12 | .map(|line| { 13 | line.expect("Input not readable") 14 | .split(" ") 15 | .skip(1) 16 | .map(|n| n.parse::().expect("Can't parse integer")) 17 | .collect::>() 18 | }); 19 | let mut test_cases = Vec::new(); 20 | for line in content { 21 | let mut counters = BTreeMap::new(); 22 | for n in line { 23 | let count = counters.entry(n).or_insert(Cell::new(0)); 24 | count.set(count.get() + 1); 25 | } 26 | test_cases.push(counters); 27 | } 28 | 29 | test_cases 30 | } 31 | 32 | fn calc_seq(counters: &BTreeMap>, curr: i32, curr_counter: &Cell) -> i32 { 33 | curr_counter.set(curr_counter.get() - 1); 34 | 35 | match counters.get(&(curr + 1)) { 36 | None => 1, 37 | Some(next_counter) => { 38 | if next_counter.get() <= curr_counter.get() { 39 | 1 40 | } else { 41 | 1 + calc_seq(counters, curr + 1, next_counter) 42 | } 43 | } 44 | } 45 | } 46 | 47 | fn calc(mut counters: BTreeMap>) -> i32 { 48 | if counters.is_empty() { 49 | return 0; 50 | } 51 | 52 | let mut res = std::i32::MAX; 53 | while !counters.is_empty() { 54 | if counters.iter().nth(0).unwrap().1.get() == 0 { 55 | let &k = counters.iter().nth(0).unwrap().0; 56 | counters.remove(&k); 57 | continue; 58 | } 59 | let (&k, v) = counters.iter().nth(0).unwrap(); 60 | res = min(res, calc_seq(&counters, k, v)); 61 | } 62 | res 63 | } 64 | 65 | fn main() { 66 | for test_case in read_input() { 67 | println!("{:?}", calc(test_case)); 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /algorithms/bit_manipulation/xoring-ninja/src/main.rs: -------------------------------------------------------------------------------- 1 | use std::io::prelude::*; 2 | use std::io; 3 | 4 | const MOD: u64 = 1000000007; 5 | 6 | fn read_input() -> Vec> { 7 | let stdin = io::stdin(); 8 | let content = stdin.lock() 9 | .lines() 10 | .skip(1) 11 | .enumerate() 12 | .filter(|&(n, _)| n % 2 == 1) 13 | .map(|(_ , line)| { 14 | line.expect("Input not readable") 15 | .split(" ") 16 | .map(|n| n.parse::().expect("Can't parse integer")) 17 | .collect::>() 18 | }); 19 | content.collect::>() 20 | } 21 | 22 | fn count_ones(v: &Vec, pos: u32) -> u32 { 23 | let mask = 1u32 << pos; 24 | v.iter().filter(|&n| n & mask != 0).count() as u32 25 | } 26 | 27 | // From: http://stackoverflow.com/a/24500377/1025899 28 | fn bin_coeff(n: u64, k: u64, m: u64) -> u64 { 29 | let mut coef = 1; 30 | let mut sieve = vec![false; (n+1) as usize]; 31 | 32 | for p in 2..(n + 1) { 33 | if sieve[p as usize] { 34 | continue; 35 | } 36 | let mut i = p * p; 37 | while i <= n { 38 | sieve[i as usize] = true; 39 | i += p; 40 | } 41 | let mut pow = p; 42 | while pow <= n { 43 | let cnt = n / pow - k / pow - (n - k) / pow; 44 | for _ in 0..cnt { 45 | coef *= p; 46 | coef %= m; 47 | } 48 | pow *= p; 49 | } 50 | } 51 | coef 52 | } 53 | 54 | fn mod_pow(exp: u64) -> u64 { 55 | let mut c = 1; 56 | for _ in 1..(exp+1) { 57 | c = (c * 2u64) % MOD; 58 | } 59 | c 60 | } 61 | 62 | fn calc(input: &Vec) -> u64 { 63 | let mut tot = 0; 64 | let n = input.len() as u32; 65 | for pos in 0..31 { 66 | let p = count_ones(&input, pos); 67 | let mut res = 0; 68 | for k in (1..(p + 1)).filter(|n| n % 2 == 1) { 69 | res += mod_pow((n-p) as u64) * bin_coeff(p as u64, k as u64, MOD); 70 | res %= MOD; 71 | } 72 | tot += (res * mod_pow(pos as u64)) % MOD; 73 | tot %= MOD; 74 | } 75 | tot 76 | } 77 | 78 | fn main() { 79 | for input in read_input() { 80 | println!("{}", calc(&input)); 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /algorithms/string/common-child/src/first_attempt.rs: -------------------------------------------------------------------------------- 1 | use std::collections::HashMap; 2 | use std::io::prelude::*; 3 | use std::io; 4 | 5 | fn read_input() -> (Vec, Vec) { 6 | let stdin = io::stdin(); 7 | let content = stdin.lock() 8 | .lines() 9 | .map(|line| { 10 | line.expect("Input not readable").as_bytes().to_vec() 11 | }) 12 | .collect::>(); 13 | (content[0].clone(), content[1].clone()) 14 | } 15 | 16 | type Chain = Vec>; 17 | 18 | fn char_chain(s: &Vec, ref_char: u8) -> Chain { 19 | let mut last_pos = None; 20 | let mut chain = vec![None; s.len()]; 21 | for i in (0..s.len()).rev() { 22 | if s[i] == ref_char { 23 | last_pos = Some(i); 24 | } 25 | chain[i] = last_pos; 26 | } 27 | chain 28 | } 29 | 30 | struct Finder { 31 | best: usize, 32 | best_map: HashMap<(usize, usize), usize>, 33 | } 34 | 35 | impl Finder { 36 | fn new() -> Finder { 37 | Finder { best: 0, best_map: HashMap::new() } 38 | } 39 | 40 | fn find_common(&mut self, a: &Vec, chains_b: &HashMap, pos_a: usize, pos_b: usize, len: usize) { 41 | if self.best < len { 42 | self.best = len; 43 | } 44 | 45 | if pos_a >= a.len() || pos_b >= a.len() { 46 | return; 47 | } 48 | 49 | match self.best_map.get(&(pos_a, pos_b)) { 50 | None => { self.best_map.insert((pos_a, pos_b), 0); }, 51 | Some(&n) => { 52 | if len > n { 53 | self.best_map.insert((pos_a, pos_b), len); 54 | } else { 55 | return; 56 | } 57 | }, 58 | }; 59 | 60 | if let Some(next_b) = chains_b.get(&a[pos_a]).unwrap()[pos_b] { 61 | self.find_common(a, chains_b, pos_a + 1, next_b + 1, len + 1); 62 | } 63 | 64 | self.find_common(a, chains_b, pos_a + 1, pos_b, len); 65 | } 66 | } 67 | 68 | fn main() { 69 | let (str_a, str_b) = read_input(); 70 | 71 | let chains = (65..91) 72 | .map(|c| (c, char_chain(&str_b, c))) 73 | .collect::>(); 74 | 75 | let mut finder = Finder::new(); 76 | finder.find_common(&str_a, &chains, 0, 0, 0); 77 | println!("{}", finder.best); 78 | } 79 | -------------------------------------------------------------------------------- /algorithms/sorting/insertion-sort/src/main.rs: -------------------------------------------------------------------------------- 1 | /// https://www.hackerrank.com/challenges/insertion-sort/ 2 | /// 3 | /// Solution: sort each input using merge sort. In the merge phase, 4 | /// if the current element of the right part is smaller than the current 5 | /// element if the left part, it means that it has to be inserted first, 6 | /// and all the remaining elements of the left part will be shifted by one 7 | /// position. 8 | /// 9 | /// if left[i] <= right[j] 10 | /// add left[i] to sorted array 11 | /// no shift required 12 | /// else: 13 | /// add right[j] to sorted array 14 | /// all the len(left) - i elements remaining in left count as shifted 15 | /// 16 | 17 | use std::io::prelude::*; 18 | use std::io; 19 | 20 | fn read_input() -> Vec> { 21 | let stdin = io::stdin(); 22 | let content = stdin.lock() 23 | .lines() 24 | .skip(1) 25 | .enumerate() 26 | .filter(|&(n, _)| n % 2 == 1) 27 | .map(|(_, line)| { 28 | line.expect("Input not readable") 29 | .split(" ") 30 | .map(|n| n.parse::().expect("Can't parse integer")) 31 | .collect::>() 32 | }); 33 | content.collect() 34 | } 35 | 36 | fn merge((left, ls): (Vec, usize), (right, rs): (Vec, usize)) -> (Vec, usize) 37 | where T: Clone + PartialOrd { 38 | let mut v = Vec::with_capacity(left.len() + right.len()); 39 | let (mut i, mut j, mut shifts) = (0, 0, ls + rs); 40 | while i < left.len() && j < right.len() { 41 | if left[i] <= right[j] { 42 | v.push(left[i].clone()); 43 | i += 1; 44 | } else { 45 | v.push(right[j].clone()); 46 | j += 1; 47 | shifts += left.len() - i; // This is the important bit :) 48 | } 49 | } 50 | while i < left.len() { v.push(left[i].clone()); i += 1 }; 51 | while j < right.len() { v.push(right[j].clone()); j += 1 }; 52 | (v, shifts) 53 | } 54 | 55 | fn merge_sort(input: &[T]) -> (Vec, usize) 56 | where T: Clone + PartialOrd { 57 | let l = input.len(); 58 | if l > 1 { 59 | merge(merge_sort(&input[0..l/2]), merge_sort(&input[l/2..l])) 60 | } else { 61 | (input.to_vec(), 0) 62 | } 63 | } 64 | 65 | fn main() { 66 | for input in read_input() { 67 | println!("{}", merge_sort(input.as_slice()).1); 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /algorithms/implementation/the-grid-search/src/main.rs: -------------------------------------------------------------------------------- 1 | use std::io; 2 | use std::io::prelude::*; 3 | 4 | struct Map { 5 | height: usize, 6 | width: usize, 7 | data: Vec, 8 | } 9 | 10 | impl Map { 11 | fn new(height: usize, width: usize) -> Map { 12 | Map { width: width, height: height, data: vec![0; width * height] } 13 | } 14 | } 15 | 16 | fn read_map>(lines: &mut T) -> Map { 17 | let line = lines.next().unwrap(); 18 | let nums = line.split(" ").collect::>(); 19 | let height = nums[0].parse::().unwrap(); 20 | let width = nums[1].parse::().unwrap(); 21 | let mut map = Map::new(height, width); 22 | for y in 0..height { 23 | let line = lines.next().unwrap(); 24 | for x in 0..width { 25 | map.data[y * width + x] = line[x..x+1].parse::().unwrap(); 26 | } 27 | } 28 | map 29 | } 30 | 31 | fn read_maps() -> Vec<(Map, Map)> { 32 | let mut maps = Vec::new(); 33 | let stdin = io::stdin(); 34 | let mut lines_iter = stdin.lock().lines().map(|x| x.unwrap()); 35 | let pairs_num = lines_iter.next().unwrap().parse::().unwrap(); 36 | for _ in 0..pairs_num { 37 | let outer_map = read_map(&mut lines_iter); 38 | let inner_map = read_map(&mut lines_iter); 39 | maps.push((outer_map, inner_map)); 40 | } 41 | maps 42 | } 43 | 44 | fn search_map(outer: &Map, inner: &Map) -> bool { 45 | for oy in 0..(outer.height - inner.height + 1) { 46 | for ox in 0..(outer.width - inner.width + 1) { 47 | let mut found = true; 48 | 'search: for iy in 0..inner.height { 49 | for ix in 0..inner.width { 50 | let outer_data = outer.data[(oy + iy) * outer.width + ox + ix]; 51 | let inner_data = inner.data[iy * inner.width + ix]; 52 | if outer_data != inner_data { 53 | found = false; 54 | break 'search; 55 | } 56 | } 57 | } 58 | if found { 59 | return true 60 | } 61 | } 62 | } 63 | false 64 | } 65 | 66 | fn main() { 67 | for (outer, inner) in read_maps() { 68 | if search_map(&outer, &inner) { 69 | println!("YES"); 70 | } else { 71 | println!("NO"); 72 | } 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /algorithms/implementation/matrix-rotation-algo/src/main.rs: -------------------------------------------------------------------------------- 1 | use std::io; 2 | use std::io::prelude::*; 3 | use std::cmp; 4 | 5 | struct Matrix { 6 | data: Vec>, 7 | height: usize, 8 | width: usize, 9 | } 10 | 11 | impl Matrix { 12 | fn new(height: usize, width: usize) -> Matrix { 13 | Matrix{height: height, width: width, data: vec![vec![0; width]; height]} 14 | } 15 | 16 | fn print(&self) { 17 | for y in 0..self.height { 18 | for x in 0..self.width { print!("{} ", self.data[y][x]); } 19 | println!(""); 20 | } 21 | } 22 | } 23 | 24 | fn read_matrix>(lines: &mut T) -> (Matrix, usize) { 25 | let line = lines.next().unwrap(); 26 | let size = line.split(" ").collect::>(); 27 | let height = size[0].parse::().unwrap(); 28 | let width = size[1].parse::().unwrap(); 29 | let moves = size[2].parse::().unwrap(); 30 | let mut matrix = Matrix::new(height, width); 31 | for y in 0..height { 32 | let line = lines.next().unwrap(); 33 | let nums = line.split(" ").collect::>(); 34 | for x in 0..width { 35 | matrix.data[y][x] = nums[x].parse::().unwrap(); 36 | } 37 | } 38 | (matrix, moves) 39 | } 40 | 41 | fn create_coords(start_y: usize, end_y: usize, start_x: usize, end_x: usize) -> Vec<(usize, usize)> { 42 | let mut coords = Vec::new(); 43 | 44 | coords.extend((start_y..end_y).map(|y| (y, start_x))); 45 | coords.extend((start_x..end_x).map(|x| (end_y, x))); 46 | coords.extend((start_y..(end_y+1)).rev().map(|y| (y, end_x))); 47 | coords.extend(((start_x+1)..end_x).rev().map(|x| (start_y, x))); 48 | coords 49 | } 50 | 51 | fn rotate(matrix: &Matrix, moves: usize) -> Matrix { 52 | let mut out_matrix = Matrix::new(matrix.height, matrix.width); 53 | for n in 0..(cmp::min(matrix.height, matrix.width)/2) { 54 | let coords = create_coords(n, matrix.height-n-1, n, matrix.width-n-1); 55 | for (n, &(y, x)) in coords.iter().enumerate() { 56 | let (dy, dx) = coords[(n + moves) % coords.len()]; 57 | out_matrix.data[dy][dx] = matrix.data[y][x]; 58 | } 59 | } 60 | out_matrix 61 | } 62 | 63 | fn main() { 64 | let stdin = io::stdin(); 65 | let mut lines_iter = stdin.lock().lines().map(|x| x.unwrap()); 66 | let (matrix, moves) = read_matrix(&mut lines_iter); 67 | rotate(&matrix, moves).print(); 68 | } 69 | -------------------------------------------------------------------------------- /algorithms/greedy/accessory-collection/src/main.rs: -------------------------------------------------------------------------------- 1 | use std::collections::HashSet; 2 | use std::io::prelude::*; 3 | use std::io; 4 | 5 | fn read_input() -> Vec<(u32, u32, u32, u32)> { 6 | let stdin = io::stdin(); 7 | let content = stdin.lock() 8 | .lines() 9 | .skip(1) 10 | .map(|line| { 11 | line.expect("Input not readable") 12 | .split(" ") 13 | .map(|n| n.parse::().expect("Can't parse integer")) 14 | .collect::>() 15 | }) 16 | .map(|row| {(row[0], row[1], row[2], row[3])}) 17 | .collect::>(); 18 | content 19 | } 20 | 21 | fn subsets(v: &[u32], n: u32) -> Vec> { 22 | if v.is_empty() || n == 0 { 23 | return vec![Vec::new()] 24 | } 25 | 26 | let mut ret = Vec::new(); 27 | 28 | if v.len() > (n as usize) { 29 | for sub in subsets(&v[1..], n) { 30 | ret.push(sub); 31 | } 32 | } 33 | 34 | for mut sub in subsets(&v[1..], n-1) { 35 | sub.push(v[0]); 36 | ret.push(sub); 37 | } 38 | 39 | ret 40 | } 41 | 42 | fn subsets_rep(v: &Vec, n: u32) -> Vec> { 43 | if v.is_empty() || n == 0 { 44 | return vec![Vec::new()] 45 | } 46 | 47 | let mut ret = Vec::new(); 48 | 49 | for sub in subsets_rep(v, n-1) { 50 | for &i in v { 51 | let mut copy = sub.clone(); 52 | copy.push(i); 53 | ret.push(copy); 54 | } 55 | } 56 | 57 | ret 58 | } 59 | 60 | fn brute_force(l: u32, a: u32, n: u32, d: u32) -> (u32, Vec) { 61 | let v = (1..(a+1)).collect::>(); 62 | 63 | let mut best = (0, Vec::new()); 64 | for sol in subsets_rep(&v, l) { 65 | let mut valid = true; 66 | for subset in subsets(&sol, n) { 67 | let types = subset.iter().collect::>(); 68 | if types.len() < (d as usize) { 69 | valid = false; 70 | break; 71 | } 72 | } 73 | let tot = sol.iter().fold(0, |acc, e| acc + e); 74 | if tot > best.0 && valid { 75 | best = (tot, sol.clone()); 76 | } 77 | } 78 | 79 | best 80 | } 81 | 82 | fn main() { 83 | for (l, a, n, d) in read_input() { 84 | let best = brute_force(l, a, n ,d); 85 | if best.0 == 0 { 86 | println!("SAD"); 87 | } else { 88 | println!("{} {:?}", best.0, best.1); 89 | } 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /algorithms/sorting/insertion-sort/Cargo.lock: -------------------------------------------------------------------------------- 1 | [root] 2 | name = "insertion-sort" 3 | version = "0.1.0" 4 | dependencies = [ 5 | "rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)", 6 | "time 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)", 7 | ] 8 | 9 | [[package]] 10 | name = "kernel32-sys" 11 | version = "0.2.2" 12 | source = "registry+https://github.com/rust-lang/crates.io-index" 13 | dependencies = [ 14 | "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", 15 | "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", 16 | ] 17 | 18 | [[package]] 19 | name = "libc" 20 | version = "0.2.15" 21 | source = "registry+https://github.com/rust-lang/crates.io-index" 22 | 23 | [[package]] 24 | name = "rand" 25 | version = "0.3.14" 26 | source = "registry+https://github.com/rust-lang/crates.io-index" 27 | dependencies = [ 28 | "libc 0.2.15 (registry+https://github.com/rust-lang/crates.io-index)", 29 | ] 30 | 31 | [[package]] 32 | name = "time" 33 | version = "0.1.35" 34 | source = "registry+https://github.com/rust-lang/crates.io-index" 35 | dependencies = [ 36 | "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", 37 | "libc 0.2.15 (registry+https://github.com/rust-lang/crates.io-index)", 38 | "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", 39 | ] 40 | 41 | [[package]] 42 | name = "winapi" 43 | version = "0.2.8" 44 | source = "registry+https://github.com/rust-lang/crates.io-index" 45 | 46 | [[package]] 47 | name = "winapi-build" 48 | version = "0.1.1" 49 | source = "registry+https://github.com/rust-lang/crates.io-index" 50 | 51 | [metadata] 52 | "checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d" 53 | "checksum libc 0.2.15 (registry+https://github.com/rust-lang/crates.io-index)" = "23e3757828fa702a20072c37ff47938e9dd331b92fac6e223d26d4b7a55f7ee2" 54 | "checksum rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)" = "2791d88c6defac799c3f20d74f094ca33b9332612d9aef9078519c82e4fe04a5" 55 | "checksum time 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)" = "3c7ec6d62a20df54e07ab3b78b9a3932972f4b7981de295563686849eb3989af" 56 | "checksum winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a" 57 | "checksum winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2d315eee3b34aca4797b2da6b13ed88266e6d612562a0c46390af8299fc699bc" 58 | -------------------------------------------------------------------------------- /algorithms/graph_theory/even-tree/src/main.rs: -------------------------------------------------------------------------------- 1 | use std::cell::Cell; 2 | use std::collections::HashMap; 3 | use std::io::prelude::*; 4 | use std::io; 5 | 6 | struct Node { 7 | parent: i32, 8 | count: Cell, 9 | children: Vec, 10 | } 11 | 12 | impl Node { 13 | fn new() -> Node { 14 | Node { parent: -1, count: Cell::new(0), children: Vec::new() } 15 | } 16 | } 17 | 18 | struct Tree { 19 | nodes: HashMap, 20 | root: i32, 21 | } 22 | 23 | impl Tree { 24 | fn new() -> Tree { 25 | Tree { nodes: HashMap::new(), root: -1 } 26 | } 27 | 28 | fn get_node(&self, id: i32) -> &Node { 29 | self.nodes.get(&id).expect("Node doesn't exit") 30 | } 31 | 32 | fn get_or_add_node(&mut self, id: i32) -> &mut Node { 33 | self.nodes.entry(id).or_insert(Node::new()) 34 | } 35 | 36 | fn add_edge(&mut self, start: i32, end: i32) { 37 | self.get_or_add_node(start).children.push(end); 38 | self.get_or_add_node(end).parent = start; 39 | } 40 | } 41 | 42 | fn read_input() -> Tree { 43 | let stdin = io::stdin(); 44 | let content = stdin.lock() 45 | .lines() 46 | .skip(1) 47 | .map(|line| { 48 | line.expect("Input not readable") 49 | .split(" ") 50 | .map(|n| n.parse::().expect("Can't parse integer")) 51 | .collect::>() 52 | }); 53 | 54 | let mut tree = Tree::new(); 55 | for v in content { 56 | tree.add_edge(v[1], v[0]); 57 | } 58 | tree.root = *tree.nodes.iter().find(|&(_, n)| n.parent == -1).expect("This is not a tree").0; 59 | 60 | tree 61 | } 62 | 63 | fn calc_count(tree: &Tree, node_id: i32) -> i32 { 64 | let node = tree.get_node(node_id); 65 | let count = 1 + node.children.iter().fold(0, |acc, &c_id| acc + calc_count(tree, c_id)); 66 | node.count.set(count); 67 | count 68 | } 69 | 70 | fn calc_cuts(tree: &Tree, node_id: i32, group_size: i32) -> i32 { 71 | let node = tree.get_node(node_id); 72 | if group_size % 2 == 1 { 73 | return 0; 74 | } 75 | 76 | let mut res = 0; 77 | for &c_id in &node.children { 78 | let child = tree.get_node(c_id); 79 | res += match child.count.get() % 2 { 80 | 0 => 1 + calc_cuts(tree, c_id, child.count.get()), 81 | _ => calc_cuts(tree, c_id, group_size), 82 | } 83 | } 84 | res 85 | } 86 | 87 | fn main() { 88 | let tree = read_input(); 89 | calc_count(&tree, tree.root); 90 | println!("{}", calc_cuts(&tree, tree.root, tree.get_node(tree.root).count.get())); 91 | } 92 | -------------------------------------------------------------------------------- /algorithms/dynamic_programming/stockmax/src/alternatives.rs: -------------------------------------------------------------------------------- 1 | // Recursive version, goes through all possibilities 2 | fn calc(shares: i64, index: usize, v: &[i64], cache: &mut HashMap<(i64, usize), i64>) -> i64 { 3 | if index == v.len() { 4 | return 0; 5 | } 6 | 7 | if let Some(&n) = cache.get(&(shares, index)) { 8 | return n; 9 | } 10 | 11 | let mut res = -v[index] + calc(shares + 1, index + 1, v, cache); 12 | res = max(res, calc(shares, index + 1, v, cache)); 13 | res = max(res, (v[index] * shares) + calc(0, index + 1, v, cache)); 14 | 15 | cache.insert((shares, index), res); 16 | res 17 | } 18 | 19 | // Same algorithm, iterative 20 | fn calc_iter(v: &[i64], cache: &mut HashMap<(i64, usize), i64>) -> i64 { 21 | let mut stack = Vec::new(); 22 | stack.push((0, 0, 0, Cell::new(0), Cell::new(0))); 23 | 24 | while !stack.is_empty() { 25 | let &(shares, index, incr, _, _) = stack.last().unwrap(); 26 | let phase = stack.last().unwrap().4.get(); 27 | if let Some(n) = cache.get(&(shares, index)) { 28 | let parent_phase = stack[stack.len()-2].4.get(); 29 | stack[stack.len()-2].4.set(parent_phase + 1); 30 | let parent_best = stack[stack.len()-2].3.get(); 31 | if parent_best < incr + n { 32 | stack[stack.len()-2].3.set(incr + n); 33 | } 34 | stack.pop(); 35 | continue; 36 | } 37 | if index == v.len() { 38 | let parent_phase = stack[stack.len()-2].4.get(); 39 | stack[stack.len()-2].4.set(parent_phase + 1); 40 | let parent_best = stack[stack.len()-2].3.get(); 41 | if parent_best < incr { 42 | stack[stack.len()-2].3.set(incr); 43 | } 44 | cache.insert((shares, index), incr); 45 | stack.pop(); 46 | continue; 47 | } 48 | match phase { 49 | 0 => stack.push((shares, index + 1, 0, Cell::new(0), Cell::new(0))), 50 | 1 => stack.push((shares + 1, index + 1, -v[index], Cell::new(0), Cell::new(0))), 51 | 2 => stack.push((0, index + 1, v[index]*shares, Cell::new(0), Cell::new(0))), 52 | _ => { 53 | if stack.len() == 1 { 54 | break; 55 | } 56 | let parent_phase = stack[stack.len()-2].4.get(); 57 | stack[stack.len()-2].4.set(parent_phase + 1); 58 | let my_best = stack[stack.len()-1].3.get(); 59 | let parent_best = stack[stack.len()-2].3.get(); 60 | if parent_best < incr + my_best { 61 | stack[stack.len()-2].3.set(incr + my_best); 62 | } 63 | cache.insert((shares, index), incr + my_best); 64 | stack.pop(); 65 | } 66 | } 67 | } 68 | 69 | match stack.len() { 70 | 0 => 0, 71 | 1 => stack[0].3.get(), 72 | _ => panic!("This shouldn't happen"), 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /algorithms/graph_theory/bfsshortreach/src/main.rs: -------------------------------------------------------------------------------- 1 | use std::collections::HashMap; 2 | use std::collections::LinkedList; 3 | use std::io::prelude::*; 4 | use std::io; 5 | 6 | struct Graph { 7 | edges: HashMap>, 8 | n_nodes: i32, 9 | start: i32, 10 | } 11 | 12 | impl Graph { 13 | fn new(n_nodes: i32) -> Graph { 14 | Graph { 15 | edges: HashMap::new(), 16 | start: -1, 17 | n_nodes: n_nodes, 18 | } 19 | } 20 | 21 | fn add_edge(&mut self, node: i32, neighbor: i32) { 22 | let node_edges = self.edges.entry(node).or_insert_with(Vec::new); 23 | node_edges.push(neighbor); 24 | } 25 | 26 | fn get_edges(&self, node: i32) -> &Vec { 27 | self.edges.get(&node).expect(&format!("Node {} doesn't exist", node)) 28 | } 29 | } 30 | 31 | fn read_graphs() -> Vec { 32 | let stdin = io::stdin(); 33 | let lines = stdin.lock().lines().map(|x| x.expect("Input not readable")); 34 | let mut graphs = Vec::new(); 35 | let mut content = lines.map(|line| { 36 | line.split(' ') 37 | .filter(|n| !n.is_empty()) 38 | .map(|n| n.parse::().unwrap()) 39 | .collect::>() 40 | }); 41 | let n_graphs = content.next().unwrap()[0]; 42 | for _ in 0..n_graphs { 43 | let line = content.next().unwrap(); 44 | let mut graph = Graph::new(line[0]); 45 | for _ in 0..line[1] { 46 | let line = content.next().unwrap(); 47 | graph.add_edge(line[0], line[1]); 48 | graph.add_edge(line[1], line[0]); 49 | } 50 | if graph.n_nodes > 0 { 51 | let start = content.next().unwrap()[0]; 52 | graph.start = start; 53 | graph.add_edge(start, start); 54 | } 55 | graphs.push(graph); 56 | } 57 | graphs 58 | } 59 | 60 | fn bfs(graph: &Graph) -> HashMap { 61 | let mut dist = HashMap::new(); 62 | let mut q = LinkedList::new(); 63 | 64 | q.push_back(graph.start); 65 | dist.insert(graph.start, 0); 66 | 67 | while !q.is_empty() { 68 | let node = q.pop_front().unwrap(); 69 | let node_dist = dist[&node]; 70 | for &n in graph.get_edges(node) { 71 | if dist.contains_key(&n) { 72 | continue; 73 | } 74 | dist.insert(n, node_dist + 1); 75 | q.push_back(n); 76 | } 77 | } 78 | 79 | dist 80 | } 81 | 82 | fn main() { 83 | let graphs = read_graphs(); 84 | 85 | for graph in &graphs { 86 | if graph.n_nodes == 0 { 87 | println!(""); 88 | continue; 89 | } 90 | let dist = bfs(graph); 91 | for n in (1..(graph.n_nodes + 1)).filter(|&x| x != graph.start) { 92 | let d = match dist.get(&n) { 93 | Some(x) => x * 6, 94 | None => -1, 95 | }; 96 | print!("{} ", d); 97 | } 98 | println!(""); 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /algorithms/dynamic_programming/covering-the-stains/src/brute_force.rs: -------------------------------------------------------------------------------- 1 | use std::collections::btree_map::BTreeMap; 2 | use std::cmp::max; 3 | use std::cmp::min; 4 | use std::io::prelude::*; 5 | use std::io; 6 | use std::cell::Cell; 7 | use std::borrow::Borrow; 8 | 9 | const MIN_S: i32 = 0; 10 | const MAX_S: i32 = 100000; 11 | 12 | #[derive(Debug, Eq, PartialEq)] 13 | struct Stain(i32, i32, Cell); 14 | 15 | fn read_input() -> (i32, Vec) { 16 | let stdin = io::stdin(); 17 | let mut content = stdin.lock() 18 | .lines() 19 | .map(|line| { 20 | line.expect("Input not readable") 21 | .split(" ") 22 | .map(|n| n.parse::().expect("Can't parse integer")) 23 | .collect::>() 24 | }); 25 | let clean = content.next().unwrap()[1]; 26 | let stains = content.map(|v| Stain(v[0], v[1], Cell::new(false))).collect(); 27 | (clean, stains) 28 | } 29 | 30 | #[derive(Debug, Copy, Clone, PartialEq)] 31 | struct Borders { 32 | startx: i32, 33 | endx: i32, 34 | starty: i32, 35 | endy: i32, 36 | } 37 | 38 | impl Borders { 39 | fn new(startx: i32, endx: i32, starty: i32, endy: i32) -> Borders { 40 | Borders { 41 | startx: startx, 42 | endx: endx, 43 | starty: starty, 44 | endy: endy, 45 | } 46 | } 47 | 48 | fn from_stains>(stains: &Vec) -> Borders { 49 | let mut b = Borders::new(MAX_S, MIN_S, MAX_S, MIN_S); 50 | 51 | for stain in stains { 52 | let stain = stain.borrow(); 53 | if stain.2.get() { 54 | continue; 55 | } 56 | b.startx = min(stain.0, b.startx); 57 | b.endx = max(stain.0, b.endx); 58 | b.starty = min(stain.1, b.starty); 59 | b.endy = max(stain.1, b.endy); 60 | } 61 | 62 | b 63 | } 64 | } 65 | 66 | fn combinations(v: &[T]) -> Vec> { 67 | if v.is_empty() { 68 | return vec![vec![]]; 69 | } 70 | 71 | let mut res = Vec::new(); 72 | for comb in combinations(&v[1..]) { 73 | res.push(comb.clone()); 74 | let mut tmp = comb.clone(); 75 | tmp.push(&v[0]); 76 | res.push(tmp); 77 | } 78 | 79 | res 80 | } 81 | 82 | fn take_n(v: &[T], n: i32) -> Vec> { 83 | if v.is_empty() || n == 0 { 84 | return vec![vec![]]; 85 | } 86 | 87 | let mut res = Vec::new(); 88 | res.extend(take_n(&v[1..], n).iter().cloned()); 89 | for c in take_n(&v[1..], n-1) { 90 | let mut tmp = c.clone(); 91 | tmp.push(&v[0]); 92 | res.push(tmp); 93 | } 94 | 95 | res 96 | } 97 | 98 | fn main() { 99 | let (clean, stains) = read_input(); 100 | let borders = Borders::from_stains(&stains); 101 | 102 | let mut res = 0; 103 | 104 | for comb in take_n(&stains, clean) { 105 | if comb.len() as i32 != clean { 106 | continue; 107 | } 108 | for stain in &comb { 109 | stain.2.set(true); 110 | } 111 | if Borders::from_stains(&stains) != borders { 112 | // println!("> {:?}", comb); 113 | res += 1; 114 | } 115 | for stain in &comb { 116 | stain.2.set(false); 117 | } 118 | } 119 | 120 | println!("{}", res); 121 | } 122 | -------------------------------------------------------------------------------- /algorithms/dynamic_programming/covering-the-stains/src/main.rs: -------------------------------------------------------------------------------- 1 | use std::cmp::max; 2 | use std::cmp::min; 3 | use std::io::prelude::*; 4 | use std::io; 5 | use std::cell::Cell; 6 | use std::borrow::Borrow; 7 | 8 | const MIN_S: i32 = 0; 9 | const MAX_S: i32 = 100000; 10 | const MOD: i64 = 1000000007; 11 | 12 | #[derive(Debug, Eq, PartialEq)] 13 | struct Stain(i32, i32, Cell); 14 | 15 | fn read_input() -> (i32, Vec) { 16 | let stdin = io::stdin(); 17 | let mut content = stdin.lock() 18 | .lines() 19 | .map(|line| { 20 | line.expect("Input not readable") 21 | .split(" ") 22 | .map(|n| n.parse::().expect("Can't parse integer")) 23 | .collect::>() 24 | }); 25 | let clean = content.next().unwrap()[1]; 26 | let stains = content.map(|v| Stain(v[0], v[1], Cell::new(false))).collect(); 27 | (clean, stains) 28 | } 29 | 30 | #[derive(Debug, Copy, Clone, PartialEq)] 31 | struct Borders { 32 | startx: i32, 33 | endx: i32, 34 | starty: i32, 35 | endy: i32, 36 | } 37 | 38 | impl Borders { 39 | fn new(startx: i32, endx: i32, starty: i32, endy: i32) -> Borders { 40 | Borders { 41 | startx: startx, 42 | endx: endx, 43 | starty: starty, 44 | endy: endy, 45 | } 46 | } 47 | 48 | fn from_stains>(stains: &Vec) -> Borders { 49 | let mut b = Borders::new(MAX_S, MIN_S, MAX_S, MIN_S); 50 | 51 | for stain in stains { 52 | let stain = stain.borrow(); 53 | if stain.2.get() { 54 | continue; 55 | } 56 | b.startx = min(stain.0, b.startx); 57 | b.endx = max(stain.0, b.endx); 58 | b.starty = min(stain.1, b.starty); 59 | b.endy = max(stain.1, b.endy); 60 | } 61 | 62 | b 63 | } 64 | } 65 | 66 | fn take_n(v: &[T], n: i32) -> Vec> { 67 | if v.is_empty() || n == 0 { 68 | return vec![vec![]]; 69 | } 70 | 71 | let mut res = Vec::new(); 72 | res.extend(take_n(&v[1..], n).iter().cloned()); 73 | for c in take_n(&v[1..], n - 1) { 74 | let mut tmp = c.clone(); 75 | tmp.push(&v[0]); 76 | res.push(tmp); 77 | } 78 | 79 | res 80 | } 81 | 82 | // From: http://stackoverflow.com/a/24500377/1025899 83 | fn bin_coeff(n: i64, k: i64, m: i64) -> i64 { 84 | let mut coef = 1; 85 | let mut sieve = vec![false; (n+1) as usize]; 86 | 87 | for p in 2..(n + 1) { 88 | if sieve[p as usize] { 89 | continue; 90 | } 91 | let mut i = p * p; 92 | while i <= n { 93 | sieve[i as usize] = true; 94 | i += p; 95 | } 96 | let mut pow = p; 97 | while pow <= n { 98 | let cnt = n / pow - k / pow - (n - k) / pow; 99 | for _ in 0..cnt { 100 | coef *= p; 101 | coef %= m; 102 | } 103 | pow *= p; 104 | } 105 | } 106 | coef 107 | } 108 | 109 | fn main() { 110 | let (clean, stains) = read_input(); 111 | let borders = Borders::from_stains(&stains); 112 | 113 | let mut res = 0; 114 | let rim = stains.iter() 115 | .filter(|s| { 116 | s.0 == borders.startx || s.0 == borders.endx || s.1 == borders.starty || 117 | s.1 == borders.endy 118 | }) 119 | .collect::>(); 120 | 121 | // take_n is slow :( 122 | for comb in take_n(&rim, clean) { 123 | if comb.len() == 0 { 124 | continue; 125 | } 126 | for stain in &comb { 127 | stain.2.set(true); 128 | } 129 | if Borders::from_stains(&stains) != borders { 130 | let still_to_clean = clean as i64 - comb.len() as i64; 131 | let internal_stains = (stains.len() - rim.len()) as i64; 132 | if internal_stains >= still_to_clean { 133 | res += bin_coeff(internal_stains, still_to_clean, MOD); 134 | } 135 | res %= MOD; 136 | } 137 | for stain in &comb { 138 | stain.2.set(false); 139 | } 140 | } 141 | 142 | println!("{}", res); 143 | } 144 | -------------------------------------------------------------------------------- /algorithms/graph_theory/dijkstrashortreach/src/main.rs: -------------------------------------------------------------------------------- 1 | use std::cmp::min; 2 | use std::cmp::Ord; 3 | use std::cmp::Ordering; 4 | use std::collections::BinaryHeap; 5 | use std::collections::HashMap; 6 | use std::collections::HashSet; 7 | use std::io::prelude::*; 8 | use std::io; 9 | 10 | struct Graph { 11 | edges: HashMap>, 12 | n_nodes: i32, 13 | start: i32, 14 | } 15 | 16 | impl Graph { 17 | fn new(n_nodes: i32) -> Graph { 18 | Graph { 19 | edges: HashMap::new(), 20 | start: -1, 21 | n_nodes: n_nodes, 22 | } 23 | } 24 | 25 | fn get_nodes(&self) -> Vec<&i32> { 26 | self.edges.keys().collect::>() 27 | } 28 | 29 | fn add_edge(&mut self, node: i32, neighbor: i32, weight: i32) { 30 | let node_edges = self.edges.entry(node).or_insert_with(Vec::new); 31 | node_edges.push((neighbor, weight)); 32 | } 33 | 34 | fn get_edges(&self, node: i32) -> &Vec<(i32, i32)> { 35 | self.edges.get(&node).expect(&format!("Node {} doesn't exist", node)) 36 | } 37 | } 38 | 39 | fn read_graphs() -> Vec { 40 | let stdin = io::stdin(); 41 | let lines = stdin.lock().lines().map(|x| x.expect("Input not readable")); 42 | let mut graphs = Vec::new(); 43 | let mut content = lines.map(|line| { 44 | line.split(' ') 45 | .filter(|n| !n.is_empty()) 46 | .map(|n| n.parse::().unwrap()) 47 | .collect::>() 48 | }); 49 | let n_graphs = content.next().unwrap()[0]; 50 | for _ in 0..n_graphs { 51 | let line = content.next().unwrap(); 52 | let mut graph = Graph::new(line[0]); 53 | let mut min_weight = HashMap::new(); 54 | for _ in 0..line[1] { 55 | let line = content.next().unwrap(); 56 | let min = match min_weight.get(&(line[0], line[1])) { 57 | Some(&n) => min(n, line[2]), 58 | None => line[2], 59 | }; 60 | min_weight.insert((line[0], line[1]), min); 61 | } 62 | for ((n1, n2), w) in min_weight { 63 | graph.add_edge(n1, n2, w); 64 | graph.add_edge(n2, n1, w); 65 | } 66 | graph.start = content.next().unwrap()[0]; 67 | graphs.push(graph); 68 | } 69 | graphs 70 | } 71 | 72 | #[derive(PartialEq, Eq, Debug)] 73 | struct HeapNode(i32, i32); 74 | 75 | impl Ord for HeapNode { 76 | fn cmp(&self, other: &Self) -> Ordering { 77 | self.1.cmp(&other.1).reverse() 78 | } 79 | } 80 | 81 | impl PartialOrd for HeapNode { 82 | fn partial_cmp(&self, other: &Self) -> Option { 83 | Some(self.cmp(other)) 84 | } 85 | } 86 | 87 | fn dijkstra(graph: &Graph) -> HashMap { 88 | let mut q = HashSet::new(); 89 | let mut dist = HashMap::new(); 90 | let mut heap = BinaryHeap::new(); 91 | 92 | for &node in graph.get_nodes() { 93 | dist.insert(node, std::i32::MAX); 94 | heap.push(HeapNode(node, std::i32::MAX)); 95 | q.insert(node); 96 | } 97 | 98 | dist.insert(graph.start, 0); 99 | heap.push(HeapNode(graph.start, 0)); 100 | 101 | while !q.is_empty() { 102 | let mut node = -1; 103 | while !q.contains(&node) { 104 | if let Some(HeapNode(n, _)) = heap.pop() { 105 | node = n; 106 | } 107 | } 108 | q.remove(&node); 109 | if dist[&node] == std::i32::MAX { 110 | break; 111 | } 112 | 113 | for &(neighbor, distance) in graph.get_edges(node) { 114 | let updated_dist = dist[&node] + distance; 115 | if updated_dist < dist[&neighbor] { 116 | dist.insert(neighbor, updated_dist); 117 | heap.push(HeapNode(neighbor, updated_dist)); 118 | } 119 | } 120 | } 121 | dist 122 | } 123 | 124 | fn main() { 125 | for graph in &read_graphs() { 126 | let dist = dijkstra(graph); 127 | for n in (1..(graph.n_nodes + 1)).filter(|&x| x != graph.start) { 128 | let &d = dist.get(&n).unwrap_or(&std::i32::MAX); 129 | if d == std::i32::MAX { 130 | print!("-1 "); 131 | } else { 132 | print!("{} ", d); 133 | } 134 | } 135 | println!(""); 136 | } 137 | } 138 | -------------------------------------------------------------------------------- /algorithms/graph_theory/jeanies-route/src/main.rs: -------------------------------------------------------------------------------- 1 | use std::cell::Cell; 2 | use std::cmp::Ord; 3 | use std::cmp::Ordering; 4 | use std::collections::BinaryHeap; 5 | use std::collections::HashMap; 6 | use std::collections::HashSet; 7 | use std::io::prelude::*; 8 | use std::io; 9 | 10 | struct Graph { 11 | edges: HashMap>, 12 | } 13 | 14 | impl Graph { 15 | fn new() -> Graph { 16 | Graph{edges: HashMap::new()} 17 | } 18 | 19 | fn add_edge(&mut self, node: i32, neighbor: i32, weight: i32) { 20 | let edges = self.edges.entry(node).or_insert(Vec::new()); 21 | edges.push((neighbor, weight)); 22 | } 23 | 24 | fn get_nodes(&self) -> Vec<&i32> { 25 | self.edges.keys().collect::>() 26 | } 27 | 28 | fn get_edges(&self, node: i32) -> &Vec<(i32, i32)> { 29 | self.edges.get(&node).expect(&format!("Node {} doesn't exist", node)) 30 | } 31 | } 32 | 33 | fn read_input() -> (Graph, Vec) { 34 | let stdin = io::stdin(); 35 | let lines = stdin.lock().lines().map(|x| x.unwrap()); 36 | let content = lines 37 | .map(|line| line.split(" ").map(|n| n.parse::().unwrap()).collect::>()) 38 | .collect::>(); 39 | let mut graph = Graph::new(); 40 | for x in content[2..].iter() { 41 | graph.add_edge(x[0], x[1], x[2]); 42 | graph.add_edge(x[1], x[0], x[2]); 43 | } 44 | (graph, content[1].clone()) 45 | } 46 | 47 | #[derive(PartialEq, Eq)] 48 | struct HeapNode(i32, i32); 49 | 50 | impl Ord for HeapNode { 51 | fn cmp(&self, other: &Self) -> Ordering { 52 | self.1.cmp(&other.1).reverse() 53 | } 54 | } 55 | 56 | impl PartialOrd for HeapNode { 57 | fn partial_cmp(&self, other: &Self) -> Option { 58 | Some(self.cmp(other)) 59 | } 60 | } 61 | 62 | fn dijkstra(graph: &Graph, source: i32, dest: &HashSet) -> Option<(i32, i32)> { 63 | let mut q = HashSet::new(); 64 | let mut dist = HashMap::new(); 65 | let mut heap = BinaryHeap::new(); 66 | 67 | for &node in graph.get_nodes() { 68 | dist.insert(node, std::i32::MAX); 69 | heap.push(HeapNode(node, std::i32::MAX)); 70 | q.insert(node); 71 | } 72 | 73 | dist.insert(source, 0); 74 | heap.push(HeapNode(source, 0)); 75 | 76 | while !q.is_empty() { 77 | let mut node = -1; 78 | while !q.contains(&node) { 79 | if let Some(HeapNode(n, _)) = heap.pop(){ 80 | node = n; 81 | } 82 | } 83 | if dest.contains(&node) { 84 | return Some((node, dist[&node])); 85 | } 86 | q.remove(&node); 87 | 88 | for &(neighbor, distance) in graph.get_edges(node) { 89 | let updated_dist = dist[&node] + distance; 90 | if updated_dist < dist[&neighbor] { 91 | dist.insert(neighbor, updated_dist); 92 | heap.push(HeapNode(neighbor, updated_dist)); 93 | } 94 | } 95 | } 96 | 97 | None 98 | } 99 | 100 | 101 | fn search(distances: &HashMap>, letters: &Vec, curr_node: i32, curr_dist: i32, best: &Cell) { 102 | if letters.is_empty() { 103 | if curr_dist < best.get() { 104 | best.set(curr_dist); 105 | } 106 | } 107 | 108 | for i in 0..letters.len() { 109 | let new_dist = curr_dist + distances[&curr_node][&letters[i]]; 110 | if new_dist > best.get() { 111 | continue; 112 | } 113 | let mut tmp = Vec::new(); 114 | tmp.extend_from_slice(&letters[0..i]); 115 | tmp.extend_from_slice(&letters[i+1..]); 116 | 117 | search(distances, &tmp, letters[i], new_dist, best); 118 | } 119 | } 120 | 121 | fn main() { 122 | let (graph, letters) = read_input(); 123 | // let mut distances = HashMap::new(); 124 | let mut letters_set = HashSet::new(); 125 | 126 | for &l in &letters { 127 | letters_set.insert(l); 128 | } 129 | 130 | for &l in &letters { 131 | let mut new_set = letters_set.clone(); 132 | let mut dist = 0; 133 | let mut curr = l; 134 | while new_set.len() > 1 { 135 | new_set.remove(&curr); 136 | let res = dijkstra(&graph, curr, &new_set).unwrap(); 137 | curr = res.0; 138 | dist += res.1; 139 | } 140 | println!(">> {}", dist); 141 | } 142 | 143 | // for &l in &letters { 144 | // distances.insert(l, dijkstra(&graph, l)); 145 | // } 146 | 147 | // let best = Cell::new(std::i32::MAX); 148 | // for i in 0..letters.len() { 149 | // let mut others = Vec::new(); 150 | // others.extend_from_slice(&letters[0..i]); 151 | // others.extend_from_slice(&letters[i+1..]); 152 | // search(&distances, &others, letters[i], 0, &best); 153 | // } 154 | // println!("{}", best.get()); 155 | } 156 | -------------------------------------------------------------------------------- /algorithms/graph_theory/jeanies-route/input05.txt: -------------------------------------------------------------------------------- 1 | 500 11 2 | 311 360 429 116 477 133 442 307 187 232 387 3 | 76 445 5 4 | 261 39 1 5 | 497 489 5 6 | 302 268 9 7 | 481 134 9 8 | 218 298 4 9 | 387 485 2 10 | 455 424 10 11 | 29 78 5 12 | 214 1 1 13 | 364 373 10 14 | 299 87 8 15 | 469 170 9 16 | 143 114 9 17 | 118 266 1 18 | 80 198 6 19 | 307 120 2 20 | 73 478 2 21 | 335 146 10 22 | 287 47 4 23 | 353 176 3 24 | 354 318 5 25 | 327 155 5 26 | 376 132 9 27 | 148 275 3 28 | 437 5 6 29 | 173 101 9 30 | 193 173 1 31 | 492 286 3 32 | 5 408 1 33 | 26 25 9 34 | 200 238 3 35 | 458 393 10 36 | 256 22 2 37 | 270 354 2 38 | 440 231 10 39 | 147 55 10 40 | 150 140 3 41 | 279 499 4 42 | 385 179 9 43 | 119 226 4 44 | 239 457 3 45 | 191 2 3 46 | 284 211 6 47 | 88 275 8 48 | 424 241 2 49 | 150 159 10 50 | 101 177 1 51 | 446 102 4 52 | 430 383 2 53 | 313 239 5 54 | 178 271 4 55 | 122 117 1 56 | 103 157 1 57 | 219 408 1 58 | 465 97 5 59 | 85 43 7 60 | 160 368 2 61 | 209 70 8 62 | 233 365 10 63 | 61 406 9 64 | 414 473 8 65 | 117 100 10 66 | 483 70 1 67 | 349 314 6 68 | 237 425 5 69 | 351 68 6 70 | 242 234 6 71 | 274 205 10 72 | 322 449 5 73 | 307 114 10 74 | 293 11 8 75 | 393 454 2 76 | 267 367 1 77 | 137 169 1 78 | 41 244 2 79 | 98 96 5 80 | 329 185 11 81 | 341 498 11 82 | 213 30 5 83 | 237 473 6 84 | 417 369 10 85 | 250 311 3 86 | 90 189 4 87 | 280 315 5 88 | 12 204 5 89 | 222 142 3 90 | 149 345 3 91 | 123 342 9 92 | 41 90 10 93 | 22 102 6 94 | 223 126 5 95 | 94 49 5 96 | 343 447 2 97 | 194 65 11 98 | 57 123 4 99 | 325 174 11 100 | 257 364 5 101 | 392 113 1 102 | 295 249 1 103 | 164 432 2 104 | 207 359 1 105 | 84 156 6 106 | 110 303 5 107 | 283 252 2 108 | 233 36 10 109 | 297 211 1 110 | 295 461 4 111 | 34 184 2 112 | 48 195 4 113 | 356 423 6 114 | 248 61 6 115 | 166 302 6 116 | 80 446 5 117 | 450 172 5 118 | 246 68 6 119 | 397 230 8 120 | 410 143 7 121 | 139 476 6 122 | 465 409 1 123 | 477 487 1 124 | 411 240 1 125 | 417 430 8 126 | 396 58 1 127 | 161 394 4 128 | 178 344 10 129 | 478 155 6 130 | 9 113 1 131 | 131 77 11 132 | 24 192 3 133 | 72 370 6 134 | 127 15 10 135 | 389 355 11 136 | 444 125 11 137 | 157 299 3 138 | 158 187 10 139 | 328 374 5 140 | 403 208 10 141 | 243 30 9 142 | 411 100 7 143 | 341 229 9 144 | 254 479 7 145 | 165 296 4 146 | 327 101 11 147 | 480 316 9 148 | 395 99 8 149 | 467 74 5 150 | 140 388 3 151 | 438 300 7 152 | 427 111 7 153 | 399 254 8 154 | 243 92 8 155 | 312 28 10 156 | 28 285 8 157 | 58 48 1 158 | 422 135 5 159 | 198 136 2 160 | 403 169 10 161 | 413 461 1 162 | 428 7 8 163 | 498 151 7 164 | 339 262 10 165 | 253 210 4 166 | 202 27 2 167 | 379 482 11 168 | 267 290 4 169 | 203 310 10 170 | 495 79 1 171 | 167 251 5 172 | 395 86 1 173 | 281 95 4 174 | 227 444 1 175 | 91 480 1 176 | 188 270 10 177 | 40 368 5 178 | 276 144 8 179 | 248 490 6 180 | 361 383 7 181 | 363 124 4 182 | 324 188 5 183 | 453 486 3 184 | 438 337 5 185 | 342 103 7 186 | 142 496 7 187 | 56 216 8 188 | 10 17 11 189 | 358 401 10 190 | 372 130 5 191 | 18 389 5 192 | 64 343 7 193 | 303 326 4 194 | 67 493 4 195 | 268 432 6 196 | 477 392 4 197 | 410 404 4 198 | 69 490 9 199 | 339 8 2 200 | 301 224 5 201 | 110 352 2 202 | 156 466 5 203 | 405 288 6 204 | 172 484 8 205 | 257 316 11 206 | 493 253 10 207 | 283 416 10 208 | 330 276 8 209 | 360 67 7 210 | 300 195 9 211 | 333 335 7 212 | 45 183 7 213 | 346 3 6 214 | 215 400 2 215 | 163 36 1 216 | 43 449 1 217 | 317 25 3 218 | 60 177 8 219 | 34 127 4 220 | 469 148 7 221 | 108 75 1 222 | 161 331 3 223 | 445 144 9 224 | 483 212 1 225 | 129 488 9 226 | 369 323 11 227 | 456 357 7 228 | 35 277 11 229 | 54 163 7 230 | 225 45 5 231 | 145 238 1 232 | 168 470 6 233 | 126 73 7 234 | 37 374 6 235 | 213 331 4 236 | 476 13 9 237 | 370 459 4 238 | 367 62 2 239 | 69 388 9 240 | 486 372 1 241 | 471 46 8 242 | 466 399 7 243 | 391 464 6 244 | 192 201 1 245 | 500 199 2 246 | 496 57 6 247 | 79 357 9 248 | 334 497 3 249 | 108 443 11 250 | 263 160 11 251 | 89 162 6 252 | 289 358 4 253 | 282 128 7 254 | 111 419 8 255 | 447 306 4 256 | 321 479 5 257 | 407 146 7 258 | 474 125 11 259 | 334 320 1 260 | 138 332 9 261 | 232 381 4 262 | 402 32 2 263 | 435 162 11 264 | 386 232 2 265 | 321 159 7 266 | 452 336 9 267 | 241 463 7 268 | 474 29 7 269 | 491 442 4 270 | 4 250 1 271 | 21 306 8 272 | 264 434 2 273 | 97 475 2 274 | 37 382 10 275 | 124 38 9 276 | 245 226 11 277 | 304 323 7 278 | 298 348 8 279 | 361 394 1 280 | 157 309 11 281 | 371 191 6 282 | 227 46 5 283 | 128 121 7 284 | 381 166 8 285 | 377 174 2 286 | 180 273 2 287 | 17 87 2 288 | 443 6 7 289 | 359 194 2 290 | 418 261 1 291 | 249 83 10 292 | 7 421 5 293 | 456 269 9 294 | 53 379 8 295 | 433 175 6 296 | 401 259 1 297 | 457 376 2 298 | 204 230 5 299 | 431 53 6 300 | 441 129 5 301 | 4 431 9 302 | 463 274 11 303 | 181 182 2 304 | 93 332 8 305 | 107 303 10 306 | 42 55 9 307 | 154 297 5 308 | 308 109 11 309 | 365 398 11 310 | 75 200 8 311 | 62 322 2 312 | 308 402 11 313 | 12 363 2 314 | 133 190 9 315 | 27 429 5 316 | 442 407 9 317 | 240 56 1 318 | 340 154 7 319 | 492 284 11 320 | 292 115 3 321 | 165 422 7 322 | 351 258 3 323 | 222 423 10 324 | 382 494 10 325 | 344 362 3 326 | 26 366 1 327 | 220 20 11 328 | 272 179 5 329 | 223 83 4 330 | 23 329 2 331 | 104 9 10 332 | 258 418 6 333 | 59 72 3 334 | 305 328 11 335 | 499 347 10 336 | 425 325 8 337 | 352 416 10 338 | 385 377 1 339 | 489 440 5 340 | 212 19 4 341 | 485 400 11 342 | 349 33 9 343 | 187 380 1 344 | 348 436 8 345 | 491 189 1 346 | 23 487 9 347 | 175 6 6 348 | 355 290 8 349 | 171 309 11 350 | 481 224 7 351 | 217 415 7 352 | 279 350 10 353 | 151 273 8 354 | 336 404 11 355 | 228 141 5 356 | 120 340 6 357 | 285 86 4 358 | 434 63 1 359 | 459 337 1 360 | 319 180 7 361 | 207 265 4 362 | 378 2 9 363 | 71 153 2 364 | 453 21 2 365 | 315 132 9 366 | 345 319 8 367 | 221 199 8 368 | 89 139 5 369 | 292 482 3 370 | 350 153 10 371 | 185 88 3 372 | 260 78 8 373 | 294 378 3 374 | 32 168 9 375 | 14 15 1 376 | 390 206 3 377 | 451 86 3 378 | 183 495 9 379 | 133 196 3 380 | 362 206 1 381 | 278 115 3 382 | 31 202 2 383 | 460 92 10 384 | 137 318 8 385 | 66 235 6 386 | 324 116 5 387 | 421 121 7 388 | 360 106 2 389 | 167 333 6 390 | 231 304 2 391 | 99 387 9 392 | 94 190 8 393 | 138 131 1 394 | 291 176 10 395 | 437 247 10 396 | 122 472 1 397 | 436 164 6 398 | 13 93 7 399 | 264 51 6 400 | 296 14 3 401 | 186 60 8 402 | 470 413 6 403 | 214 375 4 404 | 269 317 10 405 | 11 16 5 406 | 262 19 2 407 | 197 420 10 408 | 471 221 8 409 | 152 366 9 410 | 312 412 3 411 | 76 375 6 412 | 38 116 4 413 | 448 396 3 414 | 452 293 1 415 | 51 109 1 416 | 420 65 3 417 | 380 47 6 418 | 414 218 6 419 | 245 77 4 420 | 246 252 3 421 | 209 288 2 422 | 3 278 6 423 | 251 31 8 424 | 263 398 9 425 | 42 346 9 426 | 141 135 7 427 | 265 234 9 428 | 294 338 9 429 | 96 419 6 430 | 427 371 3 431 | 24 98 10 432 | 33 289 9 433 | 281 135 1 434 | 500 439 10 435 | 52 435 8 436 | 107 184 8 437 | 272 426 2 438 | 406 63 5 439 | 71 439 1 440 | 193 106 11 441 | 81 450 10 442 | 236 147 7 443 | 259 451 3 444 | 353 1 5 445 | 475 251 10 446 | 84 412 10 447 | 81 35 9 448 | 313 197 9 449 | 82 134 11 450 | 10 145 8 451 | 219 314 6 452 | 118 472 11 453 | 50 136 3 454 | 16 210 3 455 | 217 384 4 456 | 286 105 7 457 | 338 468 5 458 | 260 64 6 459 | 458 464 1 460 | 229 282 11 461 | 205 429 2 462 | 255 203 3 463 | 347 455 6 464 | 280 82 3 465 | 74 433 3 466 | 266 91 7 467 | 462 305 11 468 | 66 186 3 469 | 208 112 6 470 | 242 49 11 471 | 104 373 3 472 | 216 182 8 473 | 256 44 5 474 | 40 454 1 475 | 271 287 9 476 | 462 105 1 477 | 54 330 7 478 | 59 468 5 479 | 405 397 10 480 | 171 244 1 481 | 152 149 3 482 | 196 20 7 483 | 409 201 9 484 | 320 391 8 485 | 494 428 10 486 | 112 170 8 487 | 255 460 10 488 | 181 448 8 489 | 386 356 1 490 | 52 85 5 491 | 277 215 3 492 | 311 220 5 493 | 236 467 6 494 | 301 326 8 495 | 415 228 10 496 | 247 426 2 497 | 158 338 11 498 | 119 225 9 499 | 441 8 2 500 | 50 235 7 501 | 18 310 9 502 | -------------------------------------------------------------------------------- /algorithms/dynamic_programming/covering-the-stains/input20.txt: -------------------------------------------------------------------------------- 1 | 1000 489 2 | 21 113 3 | 58 69 4 | 169 67 5 | 159 99 6 | 73 142 7 | 115 113 8 | 132 54 9 | 162 69 10 | 84 111 11 | 3 86 12 | 138 0 13 | 8 69 14 | 60 9 15 | 44 99 16 | 163 116 17 | 89 36 18 | 81 139 19 | 114 56 20 | 102 106 21 | 43 166 22 | 97 27 23 | 25 150 24 | 27 28 25 | 80 33 26 | 77 47 27 | 28 7 28 | 75 177 29 | 14 117 30 | 31 115 31 | 105 36 32 | 29 36 33 | 172 137 34 | 25 155 35 | 104 21 36 | 142 1 37 | 123 106 38 | 33 127 39 | 66 101 40 | 116 116 41 | 163 72 42 | 65 146 43 | 69 145 44 | 78 55 45 | 175 158 46 | 114 112 47 | 77 118 48 | 30 161 49 | 66 82 50 | 70 133 51 | 80 55 52 | 129 9 53 | 123 160 54 | 26 164 55 | 106 35 56 | 86 81 57 | 65 66 58 | 125 27 59 | 123 53 60 | 45 110 61 | 55 6 62 | 10 75 63 | 157 88 64 | 22 48 65 | 108 135 66 | 148 140 67 | 61 177 68 | 143 170 69 | 177 13 70 | 8 157 71 | 118 75 72 | 36 68 73 | 139 67 74 | 161 176 75 | 33 53 76 | 167 172 77 | 114 95 78 | 85 10 79 | 123 127 80 | 3 81 81 | 77 166 82 | 61 127 83 | 62 118 84 | 38 162 85 | 57 75 86 | 144 147 87 | 77 168 88 | 101 22 89 | 26 99 90 | 70 132 91 | 25 120 92 | 12 156 93 | 68 99 94 | 28 146 95 | 42 46 96 | 112 75 97 | 49 32 98 | 14 110 99 | 103 83 100 | 104 94 101 | 152 8 102 | 138 177 103 | 165 19 104 | 9 67 105 | 50 168 106 | 175 126 107 | 67 114 108 | 75 107 109 | 56 50 110 | 171 8 111 | 26 176 112 | 13 70 113 | 3 4 114 | 175 147 115 | 129 66 116 | 93 174 117 | 6 75 118 | 63 144 119 | 51 23 120 | 45 93 121 | 92 77 122 | 141 166 123 | 18 104 124 | 30 105 125 | 148 160 126 | 136 69 127 | 69 40 128 | 165 58 129 | 70 14 130 | 159 15 131 | 158 134 132 | 172 0 133 | 83 121 134 | 36 93 135 | 172 23 136 | 102 99 137 | 27 114 138 | 129 153 139 | 80 163 140 | 57 18 141 | 48 74 142 | 68 143 143 | 10 174 144 | 107 44 145 | 24 63 146 | 77 65 147 | 149 137 148 | 99 68 149 | 95 117 150 | 95 136 151 | 52 42 152 | 45 157 153 | 12 103 154 | 80 149 155 | 168 65 156 | 117 65 157 | 59 146 158 | 105 23 159 | 55 136 160 | 117 64 161 | 21 167 162 | 97 10 163 | 172 49 164 | 162 170 165 | 39 130 166 | 29 122 167 | 148 1 168 | 79 119 169 | 166 17 170 | 38 10 171 | 74 31 172 | 108 129 173 | 95 143 174 | 102 127 175 | 46 124 176 | 87 98 177 | 90 147 178 | 73 78 179 | 69 103 180 | 58 152 181 | 82 87 182 | 58 164 183 | 18 100 184 | 8 35 185 | 175 85 186 | 150 46 187 | 17 42 188 | 0 36 189 | 128 112 190 | 50 52 191 | 48 94 192 | 140 139 193 | 129 98 194 | 0 171 195 | 36 122 196 | 137 44 197 | 37 75 198 | 117 149 199 | 163 94 200 | 122 100 201 | 24 164 202 | 24 7 203 | 33 122 204 | 0 118 205 | 17 119 206 | 64 19 207 | 104 4 208 | 65 98 209 | 118 1 210 | 93 105 211 | 156 72 212 | 126 14 213 | 87 127 214 | 25 175 215 | 18 94 216 | 128 11 217 | 173 62 218 | 16 81 219 | 33 49 220 | 76 123 221 | 149 164 222 | 120 23 223 | 30 136 224 | 74 101 225 | 47 130 226 | 160 166 227 | 26 12 228 | 15 24 229 | 36 158 230 | 117 41 231 | 78 134 232 | 27 23 233 | 131 140 234 | 104 120 235 | 142 99 236 | 164 129 237 | 70 29 238 | 105 24 239 | 172 62 240 | 102 63 241 | 158 107 242 | 33 81 243 | 130 162 244 | 51 30 245 | 41 100 246 | 58 71 247 | 172 158 248 | 62 60 249 | 151 54 250 | 25 0 251 | 35 82 252 | 177 138 253 | 70 102 254 | 82 160 255 | 125 105 256 | 128 93 257 | 101 72 258 | 113 49 259 | 126 29 260 | 135 63 261 | 153 127 262 | 126 142 263 | 129 175 264 | 84 49 265 | 127 129 266 | 134 67 267 | 136 151 268 | 97 171 269 | 66 163 270 | 148 31 271 | 14 143 272 | 169 59 273 | 162 74 274 | 144 47 275 | 0 139 276 | 25 58 277 | 28 138 278 | 49 52 279 | 54 90 280 | 25 34 281 | 120 65 282 | 76 10 283 | 31 91 284 | 40 154 285 | 21 55 286 | 94 131 287 | 134 5 288 | 90 45 289 | 100 9 290 | 87 11 291 | 5 116 292 | 108 131 293 | 159 165 294 | 105 117 295 | 99 26 296 | 171 117 297 | 126 22 298 | 95 138 299 | 82 161 300 | 113 77 301 | 149 101 302 | 166 15 303 | 53 28 304 | 52 18 305 | 110 45 306 | 153 76 307 | 159 142 308 | 136 118 309 | 76 17 310 | 67 13 311 | 106 62 312 | 62 81 313 | 72 20 314 | 108 123 315 | 65 40 316 | 144 65 317 | 61 114 318 | 102 125 319 | 112 123 320 | 107 106 321 | 122 45 322 | 67 51 323 | 23 104 324 | 88 0 325 | 13 52 326 | 51 175 327 | 151 41 328 | 114 42 329 | 13 59 330 | 59 145 331 | 5 102 332 | 78 121 333 | 71 62 334 | 177 99 335 | 159 66 336 | 104 75 337 | 31 149 338 | 158 112 339 | 107 58 340 | 158 120 341 | 37 128 342 | 20 10 343 | 119 141 344 | 30 128 345 | 114 44 346 | 11 48 347 | 157 10 348 | 162 52 349 | 27 88 350 | 90 174 351 | 106 60 352 | 177 59 353 | 41 52 354 | 126 117 355 | 147 89 356 | 168 176 357 | 77 140 358 | 56 42 359 | 67 112 360 | 87 82 361 | 72 136 362 | 110 67 363 | 15 54 364 | 19 115 365 | 13 86 366 | 32 57 367 | 13 62 368 | 36 6 369 | 31 20 370 | 57 166 371 | 125 52 372 | 122 42 373 | 32 74 374 | 38 39 375 | 176 44 376 | 66 171 377 | 100 61 378 | 77 11 379 | 147 119 380 | 47 116 381 | 79 5 382 | 14 51 383 | 29 39 384 | 58 51 385 | 0 114 386 | 79 135 387 | 157 20 388 | 5 96 389 | 67 156 390 | 108 60 391 | 26 97 392 | 43 19 393 | 74 141 394 | 117 152 395 | 155 175 396 | 133 155 397 | 176 121 398 | 71 30 399 | 23 94 400 | 53 50 401 | 83 153 402 | 88 28 403 | 176 34 404 | 119 1 405 | 173 146 406 | 101 133 407 | 139 106 408 | 5 151 409 | 57 151 410 | 63 43 411 | 85 108 412 | 0 120 413 | 153 13 414 | 8 87 415 | 129 99 416 | 40 140 417 | 105 128 418 | 89 138 419 | 72 82 420 | 61 166 421 | 43 170 422 | 166 114 423 | 72 13 424 | 13 48 425 | 28 121 426 | 126 126 427 | 165 34 428 | 47 14 429 | 18 20 430 | 157 132 431 | 148 67 432 | 121 47 433 | 107 114 434 | 160 75 435 | 83 127 436 | 39 81 437 | 48 61 438 | 79 21 439 | 77 58 440 | 129 134 441 | 85 87 442 | 49 12 443 | 153 166 444 | 167 134 445 | 5 91 446 | 116 159 447 | 134 172 448 | 15 93 449 | 114 124 450 | 92 100 451 | 23 100 452 | 22 30 453 | 138 18 454 | 149 158 455 | 72 109 456 | 162 102 457 | 48 161 458 | 120 39 459 | 84 100 460 | 83 128 461 | 109 106 462 | 37 146 463 | 168 125 464 | 8 82 465 | 84 81 466 | 37 42 467 | 22 61 468 | 77 131 469 | 116 154 470 | 92 74 471 | 35 70 472 | 45 155 473 | 71 68 474 | 70 151 475 | 142 15 476 | 4 77 477 | 7 139 478 | 119 14 479 | 121 76 480 | 135 165 481 | 12 170 482 | 48 10 483 | 155 89 484 | 158 101 485 | 24 152 486 | 1 168 487 | 18 93 488 | 3 139 489 | 75 41 490 | 101 54 491 | 1 173 492 | 129 152 493 | 115 173 494 | 51 159 495 | 57 108 496 | 53 95 497 | 83 15 498 | 110 169 499 | 93 6 500 | 84 23 501 | 173 171 502 | 1 81 503 | 107 103 504 | 1 116 505 | 55 66 506 | 80 87 507 | 131 116 508 | 40 175 509 | 160 42 510 | 16 95 511 | 81 31 512 | 141 35 513 | 44 83 514 | 76 12 515 | 84 93 516 | 91 13 517 | 152 140 518 | 158 75 519 | 26 6 520 | 101 10 521 | 68 126 522 | 112 160 523 | 115 148 524 | 63 89 525 | 88 42 526 | 54 23 527 | 22 118 528 | 1 11 529 | 69 6 530 | 120 9 531 | 73 2 532 | 3 104 533 | 92 52 534 | 76 153 535 | 152 122 536 | 96 2 537 | 69 121 538 | 131 30 539 | 116 40 540 | 25 112 541 | 122 34 542 | 63 31 543 | 138 63 544 | 157 143 545 | 140 24 546 | 168 51 547 | 31 64 548 | 80 74 549 | 0 53 550 | 56 59 551 | 37 164 552 | 10 66 553 | 96 152 554 | 56 132 555 | 8 143 556 | 94 4 557 | 144 138 558 | 140 32 559 | 22 135 560 | 87 22 561 | 144 177 562 | 24 138 563 | 170 57 564 | 76 23 565 | 28 93 566 | 164 141 567 | 20 18 568 | 47 31 569 | 170 136 570 | 14 140 571 | 140 64 572 | 15 19 573 | 174 64 574 | 50 11 575 | 41 174 576 | 172 155 577 | 165 110 578 | 150 105 579 | 129 33 580 | 151 132 581 | 63 36 582 | 170 23 583 | 14 109 584 | 98 118 585 | 174 87 586 | 63 162 587 | 14 11 588 | 34 19 589 | 28 36 590 | 172 134 591 | 144 176 592 | 21 163 593 | 81 49 594 | 145 11 595 | 0 8 596 | 26 106 597 | 10 51 598 | 101 92 599 | 71 58 600 | 77 4 601 | 94 60 602 | 19 157 603 | 49 48 604 | 36 102 605 | 173 127 606 | 135 37 607 | 118 171 608 | 70 94 609 | 32 126 610 | 14 66 611 | 135 167 612 | 25 110 613 | 57 103 614 | 153 99 615 | 175 122 616 | 94 100 617 | 45 105 618 | 77 38 619 | 174 72 620 | 86 147 621 | 82 92 622 | 15 35 623 | 102 130 624 | 128 162 625 | 126 33 626 | 19 112 627 | 162 33 628 | 164 167 629 | 172 128 630 | 31 38 631 | 36 156 632 | 5 145 633 | 110 18 634 | 129 83 635 | 167 146 636 | 45 50 637 | 159 136 638 | 124 80 639 | 173 50 640 | 11 124 641 | 177 8 642 | 151 4 643 | 153 133 644 | 40 91 645 | 97 99 646 | 34 114 647 | 26 17 648 | 126 109 649 | 57 14 650 | 54 77 651 | 126 116 652 | 9 36 653 | 20 55 654 | 54 80 655 | 161 24 656 | 114 165 657 | 30 132 658 | 11 29 659 | 127 88 660 | 77 89 661 | 160 163 662 | 24 86 663 | 30 60 664 | 38 21 665 | 79 163 666 | 56 71 667 | 80 24 668 | 157 112 669 | 67 170 670 | 109 176 671 | 57 21 672 | 134 48 673 | 173 57 674 | 53 42 675 | 123 2 676 | 97 115 677 | 161 29 678 | 113 153 679 | 30 170 680 | 24 106 681 | 66 149 682 | 54 132 683 | 66 169 684 | 160 1 685 | 136 110 686 | 16 123 687 | 119 92 688 | 78 80 689 | 21 37 690 | 71 162 691 | 41 80 692 | 72 26 693 | 58 108 694 | 32 27 695 | 42 102 696 | 119 91 697 | 16 79 698 | 138 94 699 | 31 49 700 | 6 128 701 | 38 138 702 | 97 141 703 | 64 92 704 | 67 8 705 | 91 113 706 | 31 111 707 | 52 131 708 | 136 51 709 | 13 57 710 | 96 25 711 | 153 115 712 | 138 111 713 | 131 147 714 | 34 7 715 | 22 148 716 | 64 44 717 | 28 157 718 | 117 165 719 | 160 118 720 | 146 16 721 | 91 177 722 | 141 17 723 | 38 46 724 | 104 109 725 | 141 163 726 | 30 95 727 | 24 146 728 | 148 144 729 | 123 139 730 | 21 16 731 | 81 153 732 | 34 65 733 | 10 166 734 | 70 136 735 | 29 154 736 | 36 46 737 | 10 134 738 | 31 172 739 | 60 79 740 | 170 80 741 | 61 101 742 | 13 151 743 | 54 163 744 | 118 94 745 | 152 170 746 | 120 149 747 | 14 54 748 | 71 166 749 | 4 8 750 | 151 131 751 | 173 5 752 | 136 158 753 | 134 135 754 | 52 76 755 | 151 50 756 | 147 115 757 | 66 40 758 | 138 86 759 | 103 148 760 | 93 137 761 | 150 103 762 | 45 95 763 | 153 19 764 | 36 132 765 | 126 138 766 | 167 137 767 | 176 38 768 | 81 175 769 | 40 9 770 | 104 81 771 | 73 133 772 | 165 140 773 | 11 3 774 | 119 95 775 | 147 151 776 | 111 139 777 | 94 148 778 | 103 97 779 | 122 143 780 | 128 94 781 | 19 49 782 | 87 117 783 | 145 68 784 | 2 50 785 | 8 31 786 | 172 46 787 | 141 26 788 | 99 13 789 | 66 35 790 | 99 131 791 | 159 10 792 | 29 54 793 | 90 131 794 | 146 147 795 | 49 75 796 | 20 66 797 | 173 53 798 | 136 62 799 | 115 171 800 | 120 21 801 | 156 171 802 | 19 101 803 | 5 27 804 | 163 158 805 | 159 143 806 | 105 40 807 | 132 133 808 | 105 163 809 | 96 67 810 | 14 70 811 | 44 117 812 | 131 37 813 | 158 48 814 | 114 62 815 | 48 34 816 | 123 115 817 | 7 71 818 | 78 67 819 | 50 33 820 | 124 134 821 | 28 104 822 | 39 9 823 | 23 71 824 | 19 177 825 | 161 23 826 | 21 31 827 | 113 31 828 | 150 139 829 | 138 118 830 | 177 29 831 | 100 30 832 | 81 81 833 | 167 46 834 | 7 88 835 | 120 171 836 | 114 23 837 | 75 138 838 | 83 52 839 | 155 70 840 | 72 37 841 | 112 40 842 | 79 13 843 | 147 145 844 | 92 162 845 | 102 17 846 | 40 176 847 | 144 12 848 | 14 137 849 | 148 105 850 | 0 0 851 | 40 138 852 | 169 3 853 | 114 8 854 | 17 61 855 | 175 56 856 | 165 92 857 | 9 57 858 | 90 88 859 | 20 165 860 | 11 41 861 | 122 137 862 | 133 97 863 | 16 40 864 | 37 116 865 | 56 7 866 | 46 167 867 | 175 41 868 | 75 129 869 | 30 16 870 | 80 138 871 | 130 64 872 | 148 50 873 | 54 19 874 | 119 149 875 | 96 117 876 | 155 43 877 | 31 163 878 | 92 46 879 | 108 84 880 | 113 66 881 | 116 115 882 | 29 156 883 | 119 43 884 | 79 101 885 | 137 101 886 | 14 126 887 | 30 116 888 | 27 125 889 | 54 18 890 | 142 93 891 | 151 74 892 | 137 150 893 | 58 68 894 | 142 171 895 | 5 100 896 | 161 144 897 | 48 139 898 | 120 116 899 | 47 48 900 | 69 86 901 | 39 116 902 | 138 109 903 | 78 158 904 | 74 39 905 | 49 176 906 | 118 102 907 | 147 6 908 | 99 22 909 | 0 6 910 | 143 28 911 | 173 121 912 | 8 91 913 | 19 90 914 | 44 152 915 | 30 172 916 | 72 133 917 | 95 144 918 | 139 28 919 | 126 46 920 | 11 97 921 | 65 57 922 | 172 38 923 | 116 33 924 | 157 107 925 | 110 42 926 | 9 98 927 | 164 36 928 | 53 170 929 | 36 143 930 | 153 21 931 | 2 164 932 | 41 175 933 | 70 75 934 | 51 11 935 | 69 87 936 | 19 30 937 | 89 61 938 | 51 138 939 | 32 59 940 | 76 24 941 | 16 152 942 | 98 92 943 | 22 4 944 | 168 150 945 | 92 83 946 | 112 92 947 | 150 136 948 | 98 33 949 | 50 17 950 | 10 91 951 | 5 62 952 | 161 92 953 | 71 158 954 | 9 136 955 | 82 157 956 | 9 50 957 | 55 151 958 | 100 136 959 | 116 155 960 | 157 75 961 | 164 89 962 | 156 156 963 | 166 155 964 | 145 6 965 | 119 5 966 | 163 79 967 | 41 13 968 | 73 29 969 | 105 58 970 | 24 59 971 | 12 38 972 | 151 125 973 | 85 162 974 | 73 154 975 | 176 97 976 | 49 172 977 | 18 132 978 | 8 54 979 | 57 164 980 | 145 20 981 | 81 159 982 | 146 37 983 | 89 77 984 | 97 156 985 | 111 2 986 | 173 83 987 | 95 122 988 | 31 108 989 | 106 95 990 | 95 141 991 | 147 94 992 | 32 2 993 | 158 86 994 | 31 164 995 | 64 157 996 | 23 116 997 | 176 64 998 | 45 98 999 | 15 38 1000 | 59 100 1001 | 25 102 1002 | --------------------------------------------------------------------------------