1040 Longest Symmetric String (25 分)_15行代码AC
生活随笔
收集整理的這篇文章主要介紹了
1040 Longest Symmetric String (25 分)_15行代码AC
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
立志用最少的代碼做最高效的表達
PAT甲級最優題解——>傳送門
Given a string, you are supposed to output the length of the longest symmetric sub-string. For example, given Is PAT&TAP symmetric?, the longest symmetric sub-string is s PAT&TAP s, hence you must output 11.
Input Specification:
Each input file contains one test case which gives a non-empty string of length no more than 1000.
Output Specification:
For each test case, simply print the maximum length in a line.
Sample Input:
Is PAT&TAP symmetric?
Sample Output:
11
#include<bits/stdc++.h> using namespace std; int main() {string s; getline(cin, s);int maxLen = 0;for(int i = 0; i < s.size(); i++) { //i為回文中心int j;//j為以i為中心的字符向左向右遍歷。 這個循環是當i為回文串中心時(回文串個數為奇數)for(j=1; i>=j && i+j<s.size() && s[i+j]==s[i-j]; ++j);maxLen = max(maxLen, 2*j-1);//這個循環是當i為回文串中心左側數時(回文串個數為偶數)for(j=0; i>=j && i+j+1<s.size() && s[i-j]==s[i+1+j]; ++j);maxLen = max(maxLen, 2*j);}cout << maxLen << '\n';return 0; }
耗時:
求贊哦~
超強干貨來襲 云風專訪:近40年碼齡,通宵達旦的技術人生總結
以上是生活随笔為你收集整理的1040 Longest Symmetric String (25 分)_15行代码AC的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【题意+解析】1041 Be Uniqu
- 下一篇: 【终极办法!】idea没有import