dimanche 29 mars 2015

Timeout occurs. 2s Available only



You are given two strings X and Y, both of equal length and contains only lower case Latin characters ('a'-'z'). String Y will be lexicographically greater than or equal to string X. You have to output the total number of strings which are lexicographically greater than or equal to string X and lexicographically less than or equal to string Y, has length same as string X and contains only lower case latin characters ('a'-'z'). As such strings can be large in number, output your answer modulo 109+7.



#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
unsigned long long l=1000000007;

unsigned long long p(int z){
unsigned long long s=1;
for(int k=1;k<=z;k++)
s=(s*26)%l;
return s;
}

int main() {

int n,i,j,k;
unsigned long long sum=0;
scanf("%d",&n);
char X[1000001];
char Y[1000001];
scanf(" %[^\t\n]s",X);
scanf(" %[^\t\n]s",Y);
for(i=0;i<n;i++){
if(X[i]!=Y[i]){
sum=p(n-i);
for(j=i;j<n;j++){
sum=(sum-p(n-j-1)*(X[j]-97)-p(n-j-1)*(122-Y[j])+100*l)%l;
}
break;
}
}

printf("%llu",sum);
return 0;


}


Why timeout occurs and how to make the program run under 2s for large inputs?




Aucun commentaire:

Enregistrer un commentaire