pages:
  • 1
Demo Version SAYS

Hello Dear Coders

This is my code in C ... I tried to make it optimise as i can ...

I have troubles with the inputs like 299,999 time 1 and one 2 or 121212121212121212121212121212121212..... to do lexicographically comparison...

I know my algorithm do 299,999 * 300,000 comparison at the worst input... and its too big ...

Wish any one could help?

#include<stdio.h>
int main(){
    long int i,j,k,len;
    char inp[300033],last;
    freopen("in.txt","r",stdin);
    while(scanf("%s",inp)!=EOF){
        j=len=0;

        while(inp[len])len++;
        last=(inp[len-1]>inp[0]?inp[0]-inp[len-1]+8:inp[0]-inp[len-1])+'0';
        for(i=0;i<len-1;i++){
            inp[i]=(inp[i]>inp[i+1]?inp[i+1]-inp[i]+8:inp[i+1]-inp[i])+'0';
        }
        inp[len-1]=last;
        for(i=1;i<len;i++){
            if(inp[i]==inp[0])break;
        }
        if(i==len){
            printf("%s\n",inp);
            continue;
        }
        for(i=1;i<len;i++){
            if(inp[i]<inp[j]){
                j=i;
            }else if(inp[i]==inp[j]){
                    k=1;
                    while(inp[(i+k)%len]==inp[(j+k)%len]){
                        k+=1;
                        if(k>len)break;
                    }
                if(inp[(i+k)%len]<inp[(j+k)%len])j=i;
            }

        }
        printf("%s",&inp[j]);
        inp[j]='\0';
        printf("%s\n",inp);

    }
    return 0;
}
Demo Version SAYS

I got accepted dudes ...

Thanks for your all support ...

I should only check if its on the middle of a pattern :D

If any one need code .... Ill put it here ...