-
Notifications
You must be signed in to change notification settings - Fork 0
/
SPOJ_PALIN.CPP
113 lines (102 loc) · 1.75 KB
/
SPOJ_PALIN.CPP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include<bits/stdc++.h>
using namespace std;
char k[1000005];
int l,i,j,mid;
bool chkNine()
{
cout<<"";
for(i=0;i<l;i++)
{
if( k[i]!='9' ) return false;
}
}
void nextPalin()
{
int f=0;
l=strlen(k);
if(chkNine()) {
memset(k,'0',l);
k[0] ='1';
k[l]='1';
k[l+1]='\0';
return;
}
// calc mid,i,j
if(l%2==0)
{
i= ( l/2 )-1;
j= l/2;
mid= (l/2) -1;
}
else
{
i= ( l/2 )-1;
j= (l/2 )+1;
mid=l/2;
}
int a = i, b =j;
//iteration of k[i] & k[j]
for(; i>=0; i--,j++)
{
if(k[i] > k[j])
{
f=1;
break;
}
else if ( k[i] < k[j] )
{
f=2;
break;
}
}
if(f==1)
{
for(; i>=0; i--,j++)
{
k[j]=k[i];
}
}
else if(f==2||f==0)
{
int temp,c=1;
if(l%2==0)
{
for(i=a,j=b; i>=0; i--,j++)
{
temp = (k[i]-48)+c ;
c= temp /10;
temp= temp%10;
k[i]= temp+48;
k[j]=k[i];
}
}
else if(l%2!=0)
{
temp = (k[mid]-48)+c ;
c= temp /10;
temp= temp%10;
k[mid]= temp+48;
for(i=a,j=b; i>=0; i--,j++)
{
temp = (k[i]-48)+c ;
c= temp /10;
temp= temp%10;
k[i]= temp+48;
k[j]=k[i];
}
}
}
}
int main()
{
int t;
scanf("%d",&t);
getchar();
while(t--)
{
scanf("%[^\n]%*c",k);
nextPalin();
printf("%s\n",k);
}
return 0;
}