当前位置:网站首页>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);
边栏推荐
- Analysis report on the development trend and operation status of China's environmental monitoring instrument industry from 2022 to 2028
- 解析JSON接口并批量插入到数据库中
- Threejs专用天空盒素材,五种天空盒素材免费下载
- Part 4: drawing quadrilateral
- mysql自帶的性能測試工具mysqlslap執行壓力測試
- Gateway can not connect to tcp://127.0.0.1: Connection refused
- SQL related knowledge - DQL
- How do wechat applets delay? Timing? Execute a piece of code after? (kengji)
- Video label forbids downloading. The test is valid. Hide button. The test is valid at three points
- Laravel access error could not be opened
猜你喜欢

OSS CDN alicloud configuration method

6、 Project practice --- identifying cats and dogs

Gateway can not connect to tcp://127.0.0.1: Connection refused

A brain map to summarize the needs analysis (a supplement to the actual situation at work)

捕获数据包(Wireshark)

How does virtual box virtual machine software accelerate the network speed in the virtual system?

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

win10 系统打开的软件太小,如何变大(亲测有效)

CDN with OSS acceleration

Chrome page recording and playback function
随机推荐
C generic
Parse JSON interface and insert it into the database in batch
Matplotlib multi line chart, dot scatter chart
What are the advantages and risks of paper gold investment
Daily tests
The statistics in the MySQL field become strings, and then they are converted into numbers for sorting
What should I do if I don't understand the precious metal indicators
CTF serialization and deserialization
[learn FPGA programming from scratch -45]: vision chapter - integrated circuits help high-quality development in the digital era -2- market forecast
Add, delete, modify and query curd in PHP native SQL
But the Internet began to have a new evolution and began to appear in a new state
Principle and implementation of syn cookie
Threejs专用天空盒素材,五种天空盒素材免费下载
Upload script file (one sentence back door) WAF bypass (PHP)
[QT] resource file import
钉钉开放平台-小程序开发实战(钉钉小程序客户端)
mysql自带的性能测试工具mysqlslap执行压力测试
线程同步之条件变量
[QT] dialog box
Mysql8.0 configuring my SQL in INI file_ mode=NO_ AUTO_ CREATE_ User can start