[ create a new paste ] login | about

Link: http://codepad.org/Fr7NYqYT    [ raw code | fork ]

C, pasted on Feb 12:
#include <stdio.h>
#include <string.h>

int main()
{
        int a[27], k, i, j, len, count = 0, first;
        char t[110];
        scanf(" %d %s", &k, t);
        for(i = 0; i < k; i ++)
                a[i] = 0;
        len = strlen(t);
        for(i = 0; i < len; i ++)
        {
                if(t[i] == '?')
                        count ++;
                else
                        a[t[i] - 'a'] ++;
        }
        for(i = 0; i < k; i ++)
                if(a[i] == 0)
                {
                        first = i;
                        break;
                }
        j = k - 1;
        //printf("%d\n", a[j]);
        for(i = len / 2 - (1 - len % 2); i > -1; i --)
        {
                if(t[i] == '?')
                {
                        if(t[len - i - 1] == '?')
                        {
                                while(a[j] != 0 && j > 0)
                                        j --;
                                //printf("%d\n", j);
                                t[i] = j + 'a';
                                t[len - i - 1] = j + 'a';
                                a[j] ++;
                        }
                        else
                                t[i] = t[len - i - 1];
                }
        }
        for(i = 0; i < len; i ++)
        {
                if(t[i] == '?')
                        t[i] = t[len - i - 1];
        }
        for(i = 0; i < len; i ++)
        {
                if(t[i] != t[len - i - 1])
                {
                        printf("IMPOSSIBLE");
                        return 0;
                }
        }
        for(i = 0; i < k; i ++)
                a[i] = 0;
        for(i = 0; i < len; i ++)
                a[t[i] - 'a'] ++;
        for(i = 0; i < k; i ++)
                if(a[i] == 0)
                {
                        printf("IMPOSSIBLE");
                        return 0;
                }
        printf("%s", t);
        return 0;
}


Create a new paste based on this one


Comments: