题解 | #密码截取#
密码截取
https://www.nowcoder.com/practice/3cd4621963e8454594f00199f4536bb1
<?php $str = trim(fgets(STDIN)); $len = strlen($str); $max = 0; for ($i = 0; $i < $len; $i++) { // 奇数 $res = getStr($i, $i, $str); if (strlen($res) > $max) { $max = strlen($res); } // 偶数 $res = getStr($i, $i+1, $str); if (strlen($res) > $max) { $max = strlen($res); } } echo $max; function getStr($left = 0, $right = 0, $data) { while($data[$left] == $data[$right] && $right < strlen($data) && $left >= 0) { $left--; $right++; } return substr($data, $left+1, $right-$left-1); }