( ), , ? , ,
int i = 0;
int j = sizeof(a)/sizeof(int);
for(;i < j;i++, j--)
{
if (a[i] == a[j])
{
update<map_t, 2>(m, a[i]);
}
else
{
update<map_t, 1>(m, a[i]);
update<map_t, 1>(m, a[j]);
}
}
if (i == j)
update<map_t, 1>(m, a[i]);
here updating is a simple function because i'm too lazy to type the same code ...
template <typename M, int DEF>
void update(M& m, int v)
{
typename M::iterator it = m.find(v);
if (it != m.end())
it->second += DEF;
else
{
m.insert(pair<int, int>(v, DEF));
}
}
everything else remains unchanged, i.e. your code is good and only minor improvements are possible ...
source
share