当前位置:网站首页>PHP design function getmaxstr to find the longest symmetric string in a string - [original]
PHP design function getmaxstr to find the longest symmetric string in a string - [original]
2022-06-26 04:20:00 【Telkobe】
This is the first time I have encountered this problem , So handwriting is almost impossible for me , Because I have to debug on the machine repeatedly to get the final answer , Since I came back, I tried to write it on the computer , If there are shortcomings or simpler methods , Welcome to correct . The purpose of this problem is to write a function to find the symmetrical part of a string , Then get the longest string that meets the conditions , Like strings ‘ssabcddcba’, Then what meets the conditions is ‘abcddcba’, Let's go straight to the code .
$str='aa6aaslolsbcdeggedcbaiokabccbanh';
function getMaxStr($str){
$s_data=str_split($str);
$row=[];
foreach($s_data as $key=>$vol){
foreach($s_data as $k=>$v){
if($k<$key) continue;
if($vol==$v && $key!=$k){
if(($key+$k)==1){
$row[]=substr($str,$key,2);
}else{
$px=$key==0?$k+1:$k-$key;
$px=($px%2)==0?$px:($k-$key+1);
$px=$px/2;
for($s=1;$s<$px;$s++){
if($s_data[$key+$s]!=$s_data[$k-$s]) continue 2;
}
$row[]=substr($str,$key,($k-$key+1));
}
}
}
}
return $row;
}
$r=getMaxStr($str);
print_r($r);
边栏推荐
- 1064 (42000) error occurred when installing MySQL and modifying root password
- Zhubo Huangyu: all the precious metals you want to know are here
- Database related knowledge
- Force buckle 515 Find the maximum value in each tree row
- Composer version rollback version switching
- Report on demand situation and development trend of China's OTC industry from 2022 to 2028
- The open software of win10 system is too small. How to make it larger (effective through personal test)
- WPF value conversion
- In 2022, what professional competitions can college students majoring in automation, electrical engineering and automation participate in?
- Detailed explanation of widget construction process of fluent
猜你喜欢

Tp6 controller does not exist: app\index\controller\index

Chrome page recording and playback function

SQL related knowledge - DDL

Judge the same value of two sets 𞓜 different values

Verrouillage de lecture et d'écriture pour la synchronisation des fils

Install cenos in the virtual machine

How much do you make by writing a technical book? To tell you the truth, 2million is easy!

Computer network high frequency interview questions

What preparation should I make before learning SCM?

钉钉开放平台-小程序开发实战(钉钉小程序服务器端)
随机推荐
Spark - 一文搞懂 parquet
Li Kou 79 word search
What should I do if the 51 SCM board cannot find the device in keil
SQL related knowledge - DDL
Video label forbids downloading. The test is valid. Hide button. The test is valid at three points
pip 批量完全卸载包
Detailed explanation of widget construction process of fluent
Microsoft prohibits Russian users from downloading and installing win10/11
Zeromq from getting started to mastering
After four years of outsourcing, people are directly abandoned...
Report on demand situation and development trend of China's OTC industry from 2022 to 2028
SQL related knowledge - constraints
WPF value conversion
[Qunhui] Internet access + custom port
Install dbeaver and connect Clickhouse
6、 Project practice --- identifying cats and dogs
Mobile terminal pull-down loading pull-down loading data
Resolve PHP is not an internal or external command
CTF crypto (I) some simple encoding and encryption
What preparations should be made to develop an app from scratch