/* * longestSeqPalindr.cpp * * Created on: 22 Nov 2013 * Author: Laura */ //--------------------------------------------------------------------------- #include #include using namespace std; void readData(int &n, string v[100]){ //reads a vector of strings //data: - (reading subalgorithm) //results: a vector of strings and its length cout << "no of eleemnts, n = "; cin >> n; for(int i = 0; i < n; i++){ cout << "element from position " << i + 1 << " is: "; cin >> v[i]; } } void printData(int n, string v[100], int start, int len){ //prints a vector of integers //data: a vector of strings and its length //res: - (printing subalgorithm) cout << "results: "; for(int i = start; i < start + len; i++) cout << v[i] << " "; } bool isPalindrom(string s){ //checks if a string is palindrom or not //data: a string //res: true, if the string is palindrom // false, otherwise bool ok = true; int i = 0; while ((i < s.length()/2) && (ok == true)){ if (s[i] != s[s.length() - i - 1]) ok = false; else i = i + 1; } return ok; } int aSequence(int n, string v[100], int start){ //void aSequence(int n, string v[100], int start, int &l) //search a sequence of prime numbers that start on position //start in vector of integer v //data: a vector and its length and a start position //res: the length of the identified sequence of prime numbers int l = 1; int i = start + 1; while ((i < n) && (isPalindrom(v[i]) == true)){ i = i + 1; l++; } return l; } void longestSeq(int n, string v[100], int &start, int &len){ //identify the longest seq of prime numbers //data: a vector of integers and its length //res: the longest seq o prime numbers int i = 0; len = 0; start = -1; int crtLength; while(i < n){ if (isPalindrom(v[i]) == true) crtLength = aSequence(n, v, i); else crtLength = 0; if (crtLength > len){ len = crtLength; start = i; i = i + crtLength; } else if (crtLength > 0) i = i + crtLength; else i = i + 1; } } int main(){ //main program int noValues; string values[100]; int start = -1; int len = 0; readData(noValues, values); longestSeq(noValues, values, start, len); printData(noValues, values, start, len); _getch(); return 0; } //---------------------------------------------------------------------------