結果

問題 No.273 回文分解
ユーザー kk
提出日時 2015-09-26 02:42:16
言語 PHP
(843.2)
結果
AC  
実行時間 43 ms / 2,000 ms
コード長 961 bytes
コンパイル時間 2,551 ms
コンパイル使用メモリ 32,016 KB
実行使用メモリ 32,656 KB
最終ジャッジ日時 2024-06-25 13:30:24
合計ジャッジ時間 5,089 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 32
権限があれば一括ダウンロードができます
コンパイルメッセージ
No syntax errors detected in Main.php

ソースコード

diff #

<?php

function str_continuity($str){
	//------------------------
	//連続する文字列を数える
	//------------------------
	$p = array();
	
	for ($s=0; $s<count($str); $s++){
	  $p[$s] = 1;
	  
	  for ($nxt=$s+1; $nxt<count($str); $nxt++){
	  	if ($str[$s]==$str[$nxt]){
	  		$p[$s]++;
	  	}else{
	  		break;
	  	}
	  }
	}
	
	return $p;
}

$str = str_split(trim(fgets(STDIN)));
$pt  = str_continuity($str);

$ans = 0;

for ($s=0; $s<count($str); $s++){
	$mx = 0;
	
	//右の数
	$r  = (count($str)-1)-($s+($pt[$s]-1));
	
	if ($ans<$pt[$s]){
	    $ans = $pt[$s];
	    //連続数
	    $cntnue = $pt[$s];
	}
	
	if ($s>=$r){
		$mx = $r;
	}else{
		$mx = $s;
	}

	for ($m=1; $m<=$mx; $m++){
		if ($str[$s-$m]==$str[$s+($pt[$s]-1)+$m]){
		    $tmp = $pt[$s]+($m*2);
		    
			if ($ans<$tmp){
				$ans = $tmp;
			}
		}else{
			break;
		}
	}
}

if ($cntnue==count($str)){
	$ans = $ans - 1;
}else if (count($str)==$ans){
	$ans = $ans - 2;
}

echo $ans . PHP_EOL;
0