Advertisement
Guest User

Untitled

a guest
May 23rd, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.71 KB | None | 0 0
  1. /*
  2. ID: iraqibu1
  3. PROG: calfflac
  4. LANG: C++
  5. */
  6.  
  7.  
  8. #include <cstdio>
  9. #include <algorithm>
  10. #include <queue>
  11. #include <string.h>
  12. #include <ctype.h>
  13. using namespace std;
  14.  
  15. #define MAX 20005
  16.  
  17. char str[MAX], cpy[MAX], res[MAX], bal[100];
  18. int max_palin, start, end;
  19.  
  20. int palin_len(int from, int to)
  21. {
  22.     int i, j;
  23.     int flag = 1, count = 0;
  24.    
  25.     for(i=from,j=to; i<j; )
  26.     {
  27.         if(isalpha(cpy[i]) && isalpha(cpy[j]))
  28.         {
  29.             if(cpy[i] == cpy[j])
  30.             {
  31.                 //printf("%c %c\n", cpy[i], cpy[j]);
  32.                 count++;
  33.                 i++;
  34.                 j--;
  35.             }
  36.             else
  37.             {
  38.                 flag = 0;
  39.                 break;
  40.             }
  41.         }
  42.         else if(isalpha(cpy[i]))
  43.             j--;
  44.         else if(isalpha(cpy[j]))
  45.             i++;
  46.         else
  47.         {
  48.             i++;
  49.             j--;
  50.         }
  51.     }
  52.     if(!flag) return 0;
  53.     if(flag) return ((i==j)? 2*count+1 : 2*count);
  54. }
  55.  
  56. int main()
  57. {
  58.     freopen("calfflac.in", "r", stdin);
  59.     freopen("calfflac.out", "w", stdout);
  60.     int len, temp;
  61.     int i, j;
  62.     char ch;
  63.     //gets(str);
  64.     i = 0;
  65.     /*while((ch = getchar()) != EOF)
  66.     {
  67.         if(ch != '\n')
  68.             str[i++] = ch;
  69.     }
  70.     str[i] = '\0';
  71.     */
  72.     //gets(str);
  73.     while(gets(bal))
  74.         strcat(str, bal);
  75.        
  76.  
  77.     for(i=0 ; str[i] ; i++)
  78.     {
  79.         if(isalpha(str[i]))
  80.             cpy[i] = tolower(str[i]);
  81.         else cpy[i] = str[i];
  82.     }
  83.     cpy[i] = '\0';
  84.     //puts(cpy);
  85.     len = strlen(cpy);
  86.     max_palin = 0;
  87.     for(i=0; i<len; i++)
  88.     {
  89.         if(isalpha(cpy[i]))
  90.         {
  91.             for(j=i; ((j<=(i+2000)) && cpy[j]); j++)
  92.             {
  93.                 temp = palin_len(i, j);
  94.                 //printf("%d\n", temp);
  95.                 if(temp > max_palin)
  96.                 {
  97.                     start = i;
  98.                     end = j;
  99.                     max_palin = temp;
  100.                 }
  101.                 //printf("call hosse!!!\n");
  102.             }
  103.         }
  104.     }
  105.     printf("%d\n", max_palin);
  106.     j = 0;
  107.     for(i=start; i<=end; i++)
  108.         res[j++] = str[i];
  109.     res[j] = 0;
  110.     puts(res);
  111.    
  112.     //system("pause");
  113.     return 0;
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement