codepad
[
create a new paste
]
login
|
about
Language:
C
C++
D
Haskell
Lua
OCaml
PHP
Perl
Plain Text
Python
Ruby
Scheme
Tcl
#!/usr/bin/php <?php ini_set("memory_limit", "256M"); class PatchWorker { private $in_data = ""; private $child_list_temp = array(); private $matrix_width = 0; private $matrix_height = 0; private $mark_map = ""; private $max_score = 0; private $max_score_cells = array(); public function init($filename) { $fp = fopen($filename, "r"); if (!$fp) exit(); while (!feof($fp)) { $line = rtrim(fgets($fp)); $len = strlen($line); if ($len == 0) continue; else if ($this->matrix_width == 0) $this->matrix_width = $len; else if ($this->matrix_width != $len) exit(); $this->in_data .= $line; $this->matrix_height++; } fclose($fp); $this->mark_map = array_fill(0, strlen($this->in_data), false); } public function run() { $this->searchMatrix(); $this->countMatrix(); } private function countMatrix() { $count_map = array_fill(0, $this->matrix_height, 0); foreach ($this->max_score_cells as $pos) { $count_map[$pos[1]] ++; } for ($y = 0; $y < $this->matrix_height; $y++) { print $count_map[$y] . "\n"; } } private function searchMatrix() { for ($y = 0; $y < $this->matrix_height; $y++) { for ($x = 0; $x < $this->matrix_width; $x++) { if (!$this->mark_map[$y * $this->matrix_width + $x]) { $this->searchRootCell($x, $y); } } } } private function searchRootCell($x, $y) { $data = $this->in_data{$y * $this->matrix_width + $x}; $this->child_list_temp = array(); $this->searchCell($x, $y, $data); $score = count($this->child_list_temp); if ($score > $this->max_score) { $this->max_score = $score; $this->max_score_cells = $this->child_list_temp; } else if ($score == $this->max_score) { $this->max_score_cells = array_merge($this->max_score_cells, $this->child_list_temp); } } private function searchCell($x, $y, $data) { if ($this->mark_map[$y * $this->matrix_width + $x]) return; if ($data != $this->in_data{$y * $this->matrix_width + $x}) return; $this->child_list_temp[] = array($x, $y); $this->mark_map[$y * $this->matrix_width + $x] = true; if ($x > 0) $this->searchCell($x - 1, $y, $data); if ($x < $this->matrix_width - 1) $this->searchCell($x + 1, $y, $data); if ($y > 0) $this->searchCell($x, $y - 1, $data); if ($y < $this->matrix_height - 1) $this->searchCell($x, $y + 1, $data); } } $patch_worker = new PatchWorker(); $patch_worker->init($argv[1]); $patch_worker->run();
Private
[
?
]
Run code
Submit