File tree Expand file tree Collapse file tree 2 files changed +55
-2
lines changed Expand file tree Collapse file tree 2 files changed +55
-2
lines changed Original file line number Diff line number Diff line change 11#include < stdio.h>
22#include < stdlib.h>
3+ #include < string.h>
4+ #include < math.h>
5+
6+ int m_pos (const char * nptr, int c)
7+ {
8+ int pos = 0 ;
9+
10+ while (*nptr)
11+ {
12+ if (*nptr == c)
13+ return pos;
14+ nptr++;
15+ pos++;
16+ }
17+
18+ return pos;
19+ }
20+
21+ int m_atoi (const char * nptr)
22+ {
23+ bool isNegative = false ;
24+ int i = 0 ;
25+ int mul = strlen (nptr) - 1 ;
26+
27+ if (index (nptr, ' .' ))
28+ mul = m_pos (nptr, ' .' ) - 1 ;
29+
30+ if (*nptr == ' -' )
31+ isNegative = true ;
32+ else
33+ i += (*nptr - 48 ) * pow (10 , mul);
34+
35+ nptr++;
36+ mul--;
37+
38+ while (*nptr)
39+ {
40+ if (*nptr == ' .' )
41+ break ;
42+ i += (*nptr - 48 ) * pow (10 , mul);
43+ nptr++;
44+ mul--;
45+ }
46+
47+ if (isNegative)
48+ return 0 - i;
49+
50+ return i;
51+ }
352
453unsigned int reverse (unsigned int num)
554{
@@ -38,6 +87,9 @@ int main(int argc, char* argv[])
3887{
3988 unsigned int num = 616 ;
4089
90+ printf (" %d\n " , argv[1 ] ? m_atoi (argv[1 ]) : num);
91+ printf (" %d\n " , argv[1 ] ? atoi (argv[1 ]) : num);
92+
4193 printf (" %d\n " , argv[1 ] ? reverse (atoi (argv[1 ])) : reverse (num));
4294
4395 printf (" %d\n " , argv[1 ] ? isPalindrome (atoi (argv[1 ])) : isPalindrome (num));
Original file line number Diff line number Diff line change @@ -17,12 +17,13 @@ std::string m_preProcess(std::string s)
1717std::string longestPalindrome (std::string s)
1818{
1919 std::string T = m_preProcess (s);
20+ std::cout << " PreProcessed: " << T << std::endl;
2021 int n = T.length ();
21- int * P = new int [n];
22+ int * P = new int [n];
2223 int C = 0 , R = 0 ;
2324 for (int i = 1 ; i < n - 1 ; i++)
2425 {
25- int i_mirror = 2 * C - i; // equals to i' = C - (i- C)
26+ int i_mirror = 2 * C - i; // equals to i' = C - (i - C)
2627
2728 P[i] = (R > i) ? std::min (R-i, P[i_mirror]) : 0 ;
2829
You can’t perform that action at this time.
0 commit comments