Bài 1 nó nằm ở đây nè, giải thuật cũng đơn giản, vòng lặp thôi không có gì hết, nếu dãy không liên tục thì mới mệt T_T( QHD 99% ). Hồi đầu đọc cái đề hấp ta hấp tấp nên....hichic.
ps : Nếu muốn học thực sự, thì nên suy nghĩ trước khi đọc lời giải.
PHP Code:
#include <iostream>
#include <fstream> //input file
#include <vector> //vector string
#include <string> //string
#include <cctype> //isalpha, toupper
using namespace std;
//function prototype
bool IsPalindromeString(string ss);
int main()
{
vector<std::string> lines;
/*each line from the file gets read into this,
before being added to `lines'*/
string line;
std::ifstream client_file("a.txt");
if(!client_file || !client_file.is_open())
{
cerr << "Couldn't open file!";
return 1;
}
while(getline(client_file,line))
{
lines.push_back(line);
}
int max_length = 0;
for(int pos = 0; pos <= line.length(); pos++)
{
for(int _q = line.length(); _q > pos; _q--)
{
if(IsPalindromeString(line.substr(pos, _q)))
{
if(_q - pos > max_length)
max_length = _q - pos;
}
}
}
std::cout << "The maximum Palindrome String is : "
<< max_length << std::endl;
return 0;
}
bool IsPalindromeString(string ss)
{
string sub_string = ss;
int position = 0;
for(int x = 0; x < ss.length(); x++)
{
if (isalpha(ss[x]))
sub_string[position++] = toupper(ss[x]);
}
ss = sub_string.substr(0, position);
for(int x = 0; x < position/2; x++)
{
if (ss[x] != sub_string[position-1-x] )
return false;
return true;
}
}