GfG QA is closing soon... Please refer PRACTICE or GfG now on wards !!

conctination and comparing of string(it produce some garbage value while concatination)

Marc is young lover of strings who wants your help with the following problem. Given a string, S, of lowercase English alphabetic letters and an integer, m , calculate the number of string 's such that:

  • Each T consists of lowercase English alphabetic letters
  • 1<=length(t)<=m
  • S.T=T.S, meaning their concatenation is commutative.

Print the number of string 's T modulo 10^9+7 .

Input Format

The first line contains a   string denoting  S.
The second line contains an integer denoting  m .

Constraints

  • 1<=|S|<=5*10^5
  • consists of lowercase English alphabetic letters only (i.e., to ).
  • 1<=m<=10^9

Output Format

Print the number of string 'S satisfying the conditions above, modulo .

Sample Input

abc
6

Sample Output

2

Explanation

Given abc, we have two possible string 's satisfying and :

  1. abc
  2. abcabc.

Thus, we print on a new line. 2

my code

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#define mod 1000000007

int compare_strings(char *a, char *b)
{
   int c = 0;
 
   while (a[c] == b[c]) {
      if (a[c] == '\0' || b[c] == '\0')
         break;
      c++;
   }
 
   if (a[c] == '\0' && b[c] == '\0')
      return 1;
   else
      return -1;
}

void concat(char s1[], char s2[]) {
   int i, j;
 
   i = strlen(s1);
 
   for (j = 0; s2[j] != '\0'; i++, j++) {
      s1[i] = s2[j];
   }
 
   s1[i] = '\0';
}

char * concatenated(char array1[], char array2[])
{
    char *array3;
    array3 = malloc( (strlen(array1)+strlen(array2) + 1)*sizeof(char) );

    concat(array3, array1);
    concat(array3, array2);

    return array3;
}




int size(char *s)
    {  int count=0,i=0;
    while(s[i]!='\0')
        {
        count++;
        i++;
    }
return count;}

void substring(char s[], char sub[], int p, int l) {
   int c = 0;
 
   while (c < l) {
      sub[c] = s[p+c-1];
      c++;
   }
   sub[c] = '\0';
}


int main() {

    char a[500000];
    long m;
    scanf("%s",a);
    scanf("%ld",&m);
    char t[m];
    int i;
 
    for(i=1;i<=size(a);i++)
        {    char t[i];
            substring(a,t,1,i);
           //printf("%s \n",t);
         char *comboString = malloc( (size(a)+size(t) + 1)*sizeof(char));
         char *comboString1 = malloc( (size(t)+size(a) + 1)*sizeof(char));
         comboString = concatenated(a,t);
         comboString1 = concatenated(t,a);


          /*
            puts(comboString);
          printf("\n"); 
        puts(comboString1);
           printf("\n");
        printf("%d ",compare_strings(comboString,comboString1));
          printf("\n");*/



          if(compare_strings(comboString,comboString1)==0) {
            long k=m/size(t);
            printf("%ld",k%mod);
            break;
            }
        else{
           free(comboString);
            free(comboString1);
          memset(t,0,size(t));
         }
    
    
    
    }
    
    
    
    
    
    
    
      
    return 0;
}

 

 

 

asked Sep 16, 2016 by intakhab
...