We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
#include<stdio.h>#include<stdlib.h>#include<string.h>#include<assert.h>#define MAX_CHARACTERS 1005#define MAX_PARAGRAPHS 5char*kth_word_in_mth_sentence_of_nth_paragraph(char****document,intk,intm,intn){returndocument[n-1][m-1][k-1];}char**kth_sentence_in_mth_paragraph(char****document,intk,intm){returndocument[m-1][k-1];}char***kth_paragraph(char****document,intk){returndocument[k-1];}voidresetTempWord(char*tmpWord,int*tmpWordIndex){tmpWord[0]='\0';*tmpWordIndex=0;}char****get_document(char*text){char****document;intwordIndex=-1;intsentenceIndex=-1;intparagraphIndex=-1;intwordOpen=0;intsentenceOpen=0;intparagraphOpen=0;//when to make a new slot// - upon observation of new requirement for a slot (letter)//when to reset the tempword// - when the last word was wrapped up//when to wrap up the current word, sentend and paragraph// - when their delimiters are observedchartempWord[1000000];inttempWordIndex=0;tempWord[0]='\0';intlength=strlen(text);for(inti=0;i<length;i++){charchr=text[i];if(chr=='\0'){//i think we just need to wrap up the paragraphparagraphOpen=0;break;}elseif(chr==' '){tempWord[tempWordIndex]='\0';char*newWord=malloc(tempWordIndex);strcpy(newWord,tempWord);document[paragraphIndex][sentenceIndex][wordIndex]=newWord;resetTempWord(tempWord,&tempWordIndex);wordOpen=0;}elseif(chr=='.'){tempWord[tempWordIndex]='\0';char*newWord=malloc(tempWordIndex);strcpy(newWord,tempWord);document[paragraphIndex][sentenceIndex][wordIndex]=newWord;resetTempWord(tempWord,&tempWordIndex);wordOpen=0;sentenceOpen=0;}elseif(chr=='\n'){paragraphOpen=0;}else{if(!paragraphOpen){paragraphIndex++;sentenceIndex=wordIndex=-1;if(!document){document=calloc(1,sizeof(char***));}else{document=realloc(document,(paragraphIndex+1)*sizeof(char***));}paragraphOpen=1;}//if we need a sentence mem, make it nowif(!sentenceOpen){sentenceIndex++;wordIndex=-1;if(!document[paragraphIndex]){document[paragraphIndex]=calloc(1,sizeof(char**));}else{document[paragraphIndex]=realloc(document[paragraphIndex],(sentenceIndex+1)*sizeof(char**));}sentenceOpen=1;}//if we need a word mem, make it nowif(!wordOpen){wordIndex++;if(!document[paragraphIndex][sentenceIndex]){document[paragraphIndex][sentenceIndex]=calloc(1,sizeof(char*));}else{document[paragraphIndex][sentenceIndex]=realloc(document[paragraphIndex][sentenceIndex],(wordIndex+1)*sizeof(char*));}wordOpen=1;}//add letter to wordtempWord[tempWordIndex++]=chr;}}returndocument;}char*get_input_text(){intparagraph_count;scanf("%d",¶graph_count);charp[MAX_PARAGRAPHS][MAX_CHARACTERS],doc[MAX_CHARACTERS];memset(doc,0,sizeof(doc));getchar();for(inti=0;i<paragraph_count;i++){scanf("%[^\n]%*c",p[i]);strcat(doc,p[i]);if(i!=paragraph_count-1)strcat(doc,"\n");}char*returnDoc=(char*)malloc((strlen(doc)+1)*(sizeof(char)));strcpy(returnDoc,doc);returnreturnDoc;}voidprint_word(char*word){printf("%s",word);}voidprint_sentence(char**sentence){intword_count;scanf("%d",&word_count);for(inti=0;i<word_count;i++){printf("%s",sentence[i]);if(i!=word_count-1)printf(" ");}}voidprint_paragraph(char***paragraph){intsentence_count;scanf("%d",&sentence_count);for(inti=0;i<sentence_count;i++){print_sentence(*(paragraph+i));printf(".");}}intmain(){char*text=get_input_text();char****document=get_document(text);intq;scanf("%d",&q);while(q--){inttype;scanf("%d",&type);if(type==3){intk,m,n;scanf("%d %d %d",&k,&m,&n);char*word=kth_word_in_mth_sentence_of_nth_paragraph(document,k,m,n);print_word(word);}elseif(type==2){intk,m;scanf("%d %d",&k,&m);char**sentence=kth_sentence_in_mth_paragraph(document,k,m);print_sentence(sentence);}else{intk;scanf("%d",&k);char***paragraph=kth_paragraph(document,k);print_paragraph(paragraph);}printf("\n");}}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Querying the Document
You are viewing a single comment's thread. Return to all comments →