Created
September 27, 2011 19:54
-
-
Save allen501pc/1246049 to your computer and use it in GitHub Desktop.
找最長的回文子字串,並顯示出其長度
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Ex: Given | |
10 | |
1 2 3 4 5 8 4 3 2 1 | |
Note that the first line is the counts of numbers in the second lines. | |
Then, we can find that the max_seq = '1 2 3 4 5 4 3 2 1', whose length = 9. | |
*/ | |
#include <iostream> | |
#include <cstdlib> | |
#include <vector> | |
#include <algorithm> | |
using namespace std; | |
bool fnInc(const pair<int, int> & Obj1, const pair<int, int> & Obj2) | |
{ | |
if(Obj1.second == Obj2.second) | |
{ | |
return Obj1.first < Obj1.first; | |
} | |
return Obj1.second < Obj2.second; | |
} | |
bool fnDesc(const pair<int, int> & Obj1, const pair<int, int> & Obj2) | |
{ | |
if(Obj1.second == Obj2.second) | |
{ | |
return Obj1.first > Obj1.first; | |
} | |
return Obj1.second > Obj2.second; | |
} | |
int main(int argc, char * argv[]) | |
{ | |
string GetStr; | |
int MaxNum = 0; | |
getline(cin,GetStr); | |
if(GetStr.size() == 0 ) | |
return 0; | |
do | |
{ | |
if( MaxNum=atoi(GetStr.c_str()) ) | |
{ | |
/* Get input */ | |
getline(cin,GetStr); | |
vector<pair<int,int> > InputVector; | |
char * ptrGetSubStr = NULL; | |
char CharStr[1024] = ""; | |
int Location = 1; | |
strcpy(CharStr,GetStr.c_str()); | |
ptrGetSubStr = strtok(CharStr," "); | |
do | |
{ | |
InputVector.push_back(pair<int, int>(Location++,atoi(ptrGetSubStr))); | |
}while((ptrGetSubStr= strtok(NULL," "))!=NULL); | |
sort(InputVector.begin(), InputVector.end(), fnInc); | |
vector<pair<int,int> > IncVector(InputVector); | |
sort(InputVector.begin(), InputVector.end(), fnDesc); | |
vector<pair<int,int> > DecVector(InputVector); | |
int MaxLength = 1; | |
int CurrentValue =0,StartMax = IncVector[0].second, StartMaxLocation = 0; | |
for(int CurrentLocation=1; CurrentLocation<IncVector.size();++CurrentLocation) | |
{ | |
if(IncVector[CurrentLocation].second-1 <= (DecVector.size()-IncVector[CurrentLocation].first) | |
&& IncVector[CurrentLocation].second > StartMax | |
&& IncVector[CurrentLocation].first > StartMaxLocation) | |
{ | |
int Count = 0, TempLocation=0, TempValue = 0; | |
for(int Idx=0; Idx<DecVector.size(); ++ Idx) | |
{ | |
if(DecVector[Idx].second < IncVector[CurrentLocation].second && | |
DecVector[Idx].first > IncVector[CurrentLocation].first) | |
{ | |
if(Count ==0) | |
{ | |
TempLocation = DecVector[Idx].first; | |
TempValue = DecVector[Idx].second; | |
++ Count; | |
} | |
else if( TempLocation < DecVector[Idx].first && TempValue > DecVector[Idx].second) | |
{ | |
TempLocation = DecVector[Idx].first; | |
TempValue = DecVector[Idx].second; | |
++ Count; | |
} | |
if(Count == MaxLength) | |
{ | |
++ MaxLength; | |
StartMax= IncVector[CurrentLocation].second; | |
StartMaxLocation = IncVector[CurrentLocation].first; | |
break; | |
} | |
} | |
} | |
} | |
} | |
cout << 2*(MaxLength-1)+1 << endl; | |
} | |
}while(getline(cin, GetStr) && GetStr.size()!=0); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment