2016年11月10日 星期四

TIOJ 1126 - 尋找對稱字串

http://tioj.infor.org/problems/1126

長度短短的 $n^2$ 硬幹就好了



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include<bits/stdc++.h>
using namespace std;
int main(){
    string s;
    while(cin>>s){
        int mx=0;
        for(int i=0;i<(int)s.length();i++){
            for(int j=0;i-j>=0 && i+j<(int)s.length();j++){
                if(s[i-j]==s[i+j]){
                    mx=max(mx,2*j+1);
                    continue;
                }
                else{
                    mx=max(mx,2*j-1);
                    break;
                }
            }
        }
        for(int i=0;i<(int)s.length();i++){
            for(int j=0;i-j>=0 && i+1+j<(int)s.length();j++){
                if(s[i-j]==s[i+1+j]){
                    mx=max(mx,2*j+2);
                    continue;
                }
                else{
                    mx=max(mx,2*j);
                    break;
                }
            }
        }
        cout<<mx<<'\n';
    }
}

沒有留言:

張貼留言