Cod sursa(job #637290)

Utilizator cristianalex81Cristian Alexandru cristianalex81 Data 20 noiembrie 2011 13:39:57
Problema PalM Scor 0
Compilator cpp Status done
Runda .com 2011 Marime 1.19 kb
#include <fstream>
#include <vector>
#define dim 502

using namespace std;

vector <char> a;
vector <char> s;
int l1[dim];
int l2[dim];

int add_element(char x)
{
    unsigned index = s.size(); //last element in s
    if (index == 0) //no element in s
        s.push_back(x);
    else
    {
        while ((index>0)&&(s[index-1]>x))//if the index-1 is not the last element and x is smaller than the current index
            index--;
        if(index==s.size())//add a new element
            s.push_back(x);
        else
            s[index]=x;
    }
    return index+1;
}

int main()
{
    ifstream f ("palm.in");
    ofstream g ("palm.out");
    int j,i=0,max=0,index;
    char x;
    while (f>>x)
    {
        a.push_back(x);
        index=add_element(x);
        l1[i]=index;
        i++;
    }
    s.clear();
    i=0;
    for (j=a.size()-1;j>=0;j--)
    {
        index = add_element(a[j]);
        l2[j]=index;
        if ((l1[j]==l2[j])&&(l1[j]>max))
            max = l1[j];
        i++;
    }
//    for (i=0;i<a.size();i++)
//        g<<l1[i]<<" ";
//    g<<"\n";
//    for (i=0;i<a.size();i++)
//        g<<l2[i]<<" ";
//    g<<"\n";
    g<<max*2-1<<"\n";
    f.close();
    g.close();
    return 0;
}