当前位置:网站首页>7-10 longest symmetric substring (25 points) (violence problem solution) C language
7-10 longest symmetric substring (25 points) (violence problem solution) C language
2022-04-23 05:33:00 【Ice Cream~】
For a given string , This problem requires you to output the length of the longest symmetric substring . for example , Given
Is PAT&TAP symmetric?
, The longest symmetric substring iss PAT&TAP s
, So you should output 11.Input format :
The input is given on a line, and the length is no more than 1000 Non empty string for .
Output format :
Output the length of the longest symmetric substring in one line .
sample input :
Is PAT&TAP symmetric?
No blank lines at the end
sample output :
11
No blank lines at the end
Ideas : End of string j=strlen(s)-1 Go ahead and find the front i=0 Same character , Then judge the previous character and the matching character interval
j-i+1 Whether the characters in the meet the palindrome , If meet , Then replace max The value is the interval value j-i+1. If not, then j Look forward to , After finding one side ,i++ Continue with the previous operation
#include<stdio.h>
#include<string.h>
int main()
{
char s[1002];
gets(s);
int max=1;
for(int i=0;i<strlen(s);i++)
{
for(int j=strlen(s)-1;j>=0;j--)
{
int left=i,right=j;
while(left<=right&&s[left++]==s[right--])// Traverse left~right Internal elements
{
if(left>right)// When left~right The condition is executed after traversing all elements in the scope
{
if(max<j-i+1)
{
max=j-i+1;
}
}
}
}
}
printf("%d",max);
return 0;
}
版权声明
本文为[Ice Cream~]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220534525187.html
边栏推荐
- Cmake basic tutorial (39) pkgconfig
- Double click The jar package cannot run the solution
- Requirements for SQL server to retrieve SQL and user information
- CPT 104_ TTL 09
- Solve the problem of JS calculation accuracy
- Interpretation of common SQL statements
- After NPM was upgraded, there was a lot of panic
- Redis的基本知识
- (十一)vscode代码格式化配置
- 分支与循环语句
猜你喜欢
随机推荐
Data bus realizes the communication between brother components
Frequently asked interview questions - 3 (operating system)
QSS, qdateedit, qcalendarwidget custom settings
Use of ES6 array
Excel 2016 cannot open the file for the first time. Sometimes it is blank and sometimes it is very slow. You have to open it for the second time
提升独立站转化率攻略 | 挽回弃购用户
Golang通过exec模块实现Ping连通性检测案例
Use of uniapp native plug-ins
CPT 104_ TTL 09
如果我是pm之 演出电影vr购票展示
Multiple mainstream SQL queries only take the latest one of the data
Double click The jar package cannot run the solution
Edit, cancel, pull up menu
Several examples of pointer transfer, parameter transfer, value transfer, etc
弘玑Cyclone RPA为国金证券提供技术支撑,超200个业务场景实现流程自动化
Traversal array, object parent-child communication props / $emit
Membarrier (personal learning and understanding)
How to set the initial value of El input number to null
创建进程内存管理copy_mm - 进程与线程(九)
Differences between auto and decltype inference methods (learning notes)