## 290 - Palindroms <---> smordnilaP

All about problems in Volume 2. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

t.tahasin
New poster
Posts: 38
Joined: Tue May 28, 2013 11:21 pm

### Re: 290 Palindroms why WA

@brianfry713: Thank you very much. Finally I got AC.

tridorje
New poster
Posts: 8
Joined: Wed Oct 02, 2013 3:23 am

### Re: 290 - Palindroms <---> smordnilaP My code get Runtime error after accepted all test Case :(, can anyone help me?

My code get Runtime error after accepted all test Case , can anyone help me?

Code: Select all

``````#include <math.h>
#include <string.h>
#include <iostream>
#include <fstream>
#include <map>
#include <algorithm>
#include <stdlib.h>
#include <stdio.h>
#include <vector>

#define fi "290.inp"
#define fo "290.out"

using namespace std;

string s;
long long pt;
long long so1[305];
long long so2[305];
string kq[1005];
long long d[1005];
char temp[1005];
char tempCh[1005];

string cvTobase(long long b1, long long b2, string ch){

long long in,ln,mu,i;

ln=ch.size();
mu=ln;
in=0;
long long soo;
char tm;
for(i=0;i<ln;i++){
tm = ch[i];
in += so1[tm]* pow(b1,--mu);
}
if(in==0) return "0";
soo=0;
while(in>0){

temp[soo++]=so2[(in%b2)];
in/=b2;
}
temp[soo]=0;

reverse(temp,temp+strlen(temp));
return temp;
}

bool isPali(string str, int he){

if(str.size()<=0) return 0;

string temp;
long long ln,i;
strcpy(tempCh,temp.c_str());
temp = cvTobase(10,he,str);
ln=temp.size();
strcpy(tempCh,temp.c_str());
for(i=0;i<ln--;i++){
if(temp[i]!=temp[ln]) return 0;
}
return 1;
}

string tongNghich(string s1, int hs){

long long in1,in2,in,so,t;
t=0;

string s2 = s1;
s2 = cvTobase(10,hs,s2);
reverse(s2.begin(),s2.end());
s2 = cvTobase(hs,10,s2);

in1 = atoll(s1.c_str());
in2 = atoll(s2.c_str());

in = in1+in2;

while(in>0){
so = in%10;
tempCh[t++] = so+48;
in/=10;
}
tempCh[t]=0;
reverse(tempCh,tempCh+t);
return tempCh;
}

bool isIllegal(int hs, string x){

long long i,ln;
ln=x.size();
for(i=0;i<ln;i++){
if(so1[x[i]]>=hs) return 1;
}
return 0;
}

void output(int hs){
int i;
string str;
for(i=1;i<=pt;i++){
str = cvTobase(10,hs,kq[i]);
cout<<str<<endl;
}
cout<<endl<<endl;
}

void solve(){

long long i;

string ch,str;
memset(d,-1,sizeof(d));

for(i=15;i>=2;i--){

if(isIllegal(i,s)){
//printf("Base %d illegal ? steps\n",i);
continue;
}

ch=cvTobase(i,10,s);
pt=0;
str=ch;

if(isPali(str,i)){
//output(i);
d[i]=0;
continue;
}

kq[++pt]=str;
//++pt;
while(1){
strcpy(tempCh,str.c_str());
str = tongNghich(str,i);
strcpy(tempCh,str.c_str());
kq[++pt]=str;

if(str.size()<=0) break;

if(isPali(str,i)){
//	output(i);
d[i]=pt-1;
break;
}
}
}

for(i=15;i>=2;i--){
if(i<15) cout<<" ";
d[i]==-1 ? cout<<"?" : cout<<d[i];
}
cout<<endl;
}

int main(){

freopen(fi,"r",stdin);
freopen(fo,"w",stdout);

long long i,dem;
dem=0;

//for(i=1;i<=146;i++) cout<<i<<endl;

for(i='0';i<='9';i++) so1[i]=dem,so2[dem++]=i;
for(i='A';i<='Z'&&dem<15;i++) so1[i]=dem,so2[dem++]=i;

while(cin>>s)  solve();

return 0;
}
``````