Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- repeated_sequences.c
- Task:
- A 8-years old boy was learning to play Piano.
- He played several musical notes that are combination of ‘C’, ‘D’, ‘E’, ‘F’, ‘G’.
- For example, he played "CCGCCGGCCG" on the Piano.
- He wants to identify repeated sequences within his played musical notes.
- Given a string s that represents a sequence of musical notes,
- write a program to return all the 10-letter-long sequences (substrings)
- that occur more than once in the s. You may return the answer in any order.
- Example 1:
- Input: s = "CCCCCDDDDDCCCCCDDDDDCCCCCFFGG"
- Output: CCCCCDDDDD CCCCDDDDDC CCCDDDDDCC CCDDDDDCCC CDDDDDCCCC DDDDDCCCCC
- Example 2:
- Input: s = "CCCCCCCCCCCCC"
- Output: CCCCCCCCCC
- You can find all my C programs at Dragan Milicev's pastebin:
- https://pastebin.com/u/dmilicev
- */
- #include <stdio.h>
- #include <string.h>
- #define SUB_LEN 10
- int main(void){
- char s[] = "CCCCCDDDDDCCCCCDDDDDCCCCCFFGG";
- //char s[] = "CCCCCCCCCCCCC";
- char subs[100][SUB_LEN];
- int i, j, k, s_len=strlen(s), total_sub_count=0, sub_count=0;
- printf("\n SUB_LEN = %d , s_len = %d , s = |%s| \n\n",SUB_LEN,s_len,s);
- printf("\n s[j] subs[i] result \n\n");
- for(i=0;i<s_len-SUB_LEN+1;i++){
- k=0;
- for(j=i;j<i+SUB_LEN;j++){
- printf("%c",s[j]);
- subs[i][k++]=s[j];
- }
- subs[i][k]='\0'; // finish the substring
- total_sub_count++;
- printf("|%s|",subs[i]);
- if( strstr(&s[i+1],subs[i]) ){
- printf("%s",subs[i]);
- sub_count++;
- }
- printf("\n");
- }
- printf("\n There are total %d substrings of length %d. \n", total_sub_count, SUB_LEN);
- printf("\n There are %d substrings of length %d that occur more than once in the s. \n\n",
- sub_count, SUB_LEN);
- return 0;
- } // main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement