Advertisement
dmilicev

repeated_sequences.c

Dec 9th, 2021
673
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.74 KB | None | 0 0
  1. /*
  2.  
  3.     repeated_sequences.c
  4.  
  5.     Task:
  6.  
  7. A 8-years old boy was learning to play Piano.
  8. He played several musical notes that are combination of ‘C’, ‘D’, ‘E’, ‘F’, ‘G’.
  9. For example, he played "CCGCCGGCCG" on the Piano.
  10. He wants to identify repeated sequences within his played musical notes.
  11. Given a string s that represents a sequence of musical notes,
  12. write a program to return all the 10-letter-long sequences (substrings)
  13. that occur more than once in the s. You may return the answer in any order.
  14.  
  15. Example 1:
  16. Input: s = "CCCCCDDDDDCCCCCDDDDDCCCCCFFGG"
  17. Output: CCCCCDDDDD CCCCDDDDDC CCCDDDDDCC CCDDDDDCCC CDDDDDCCCC DDDDDCCCCC
  18.  
  19. Example 2:
  20. Input: s = "CCCCCCCCCCCCC"
  21. Output: CCCCCCCCCC
  22.  
  23.  
  24.     You can find all my C programs at Dragan Milicev's pastebin:
  25.  
  26.     https://pastebin.com/u/dmilicev
  27.  
  28. */
  29.  
  30. #include <stdio.h>
  31. #include <string.h>
  32.  
  33. #define SUB_LEN 10
  34.  
  35. int main(void){
  36.     char s[] = "CCCCCDDDDDCCCCCDDDDDCCCCCFFGG";
  37.     //char s[] = "CCCCCCCCCCCCC";
  38.     char subs[100][SUB_LEN];
  39.     int i, j, k, s_len=strlen(s), total_sub_count=0, sub_count=0;
  40.  
  41.     printf("\n SUB_LEN = %d  ,  s_len = %d  ,  s = |%s| \n\n",SUB_LEN,s_len,s);
  42.     printf("\n   s[j]     subs[i]     result \n\n");
  43.  
  44.  
  45.     for(i=0;i<s_len-SUB_LEN+1;i++){
  46.         k=0;
  47.         for(j=i;j<i+SUB_LEN;j++){
  48.             printf("%c",s[j]);
  49.             subs[i][k++]=s[j];
  50.         }
  51.         subs[i][k]='\0';    // finish the substring
  52.         total_sub_count++;
  53.         printf("|%s|",subs[i]);
  54.         if( strstr(&s[i+1],subs[i]) ){
  55.             printf("%s",subs[i]);
  56.             sub_count++;
  57.         }
  58.         printf("\n");
  59.     }
  60.     printf("\n There are total %d substrings of length %d. \n", total_sub_count, SUB_LEN);
  61.     printf("\n There are %d substrings of length %d that occur more than once in the s. \n\n",
  62.             sub_count, SUB_LEN);
  63.  
  64.  
  65.     return 0;
  66. } // main()
  67.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement