11475 - Extend to Palindrome

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

Moderator: Board moderators

lighted
Guru
Posts: 587
Joined: Wed Jun 11, 2014 9:56 pm
Location: Kyrgyzstan, Bishkek

Re: 11475 - Extend to Palindrome

Post by lighted »

Input

Code: Select all

wqeeewee
eeeeeeereeeee
Acc Output

Code: Select all

wqeeeweeeqw
eeeeeeereeeeeee
A person who sees the good in things has good thoughts. And he who has good thoughts receives pleasure from life... Bediuzzaman
Muha94
New poster
Posts: 1
Joined: Wed Mar 04, 2015 10:02 pm

Re: 11475 - Extend to Palindrome

Post by Muha94 »

Please can someone help
i tied my code on every case i could think of and the results was rights according to UVa toolkit
but i keep getting WA
this is my code and i don't whats wrong
thanks in advance
http://ideone.com/bLEDzL
SR7
New poster
Posts: 4
Joined: Fri Aug 14, 2015 6:36 pm

Re: 11475 - Extend to Palindrome WA!!

Post by SR7 »

what's wrong with this??
i've tried a lot of I/O.
help is appreciated.Thanks :wink:

Code: Select all

#include<bits/stdc++.h>
#include<string.h>
#include<stdlib.h>

using namespace std;

int palin(char s[])
{
    int len = strlen(s);
    int i,j;
    for(i=0,j=len-1; i<len/2; i++,j--){
        if(s[i]!=s[j]) return 0;
    }
    return 1;
}
void rev(char s[])
{
    int i,j;
    char temp;
    int len=strlen(s);
    for(i=0,j=len-1; i<len/2; i++,j--){
        temp=s[i];
        s[i]=s[j];
        s[j]=temp;
    }
}

int main()
{
    char s[100005],r[100005],ch;
    int i,j,k,in;
    while(scanf("%s",&s)==1){
        if(palin(s)==1) {
            printf("%s\n",s);
            continue;
        }
        else {
            int len=strlen(s);
            for(i=0,j=len-1,k=0; i<j; i++){
                if(i==0 && s[i]==s[j]) {
                    while(s[i+1]==s[j-1]){
                        i++;
                        j--;
                    }
                }
                if(s[i]!=s[j]){
                    r[k++]=s[i];
                    //printf("%c\n",r[k-1]);
                }
                else {
                    ch=s[i];
                    in=i;
                    while(s[i+1]==s[j-1] && i<=j){
                        i++,j--;
                    }
                    if(i>j) break;
                else {
                        r[k++]=ch;
                        i=in;
                }
                }
            }
            r[k]='\0';
            rev(r);
            strcat(s,r);
            printf("%s\n",s);
        }
    }
    return 0;
}
Post Reply

Return to “Volume 114 (11400-11499)”