translation
给定一个字符串S,一个单词的列表words,全是相同的长度。找到的子串(多个)以s即每个词的字串联恰好一次并没有任何插入的字符所有的起始索引。
Original
You aregivenAstring, S, andAList of words,words, thatIs all of theSamelength. Find All starting Indices ofSUBSTRING (s)inchS that isa concatenation ofeachWord inch wordsExactly once and withoutAny interveningcharacters. For example,given: s:"Barfoothefoobarman"words: ["foo","Bar"]you shouldreturn theindices: [0,9]. (Orderdoes notmatter).
Code
Difficulty for me is still too big, but the first to send a blog to occupy a position, otherwise the order will not be connected to the series ...
//For sort the |words|intSTRCMP1 (Const void* P1,Const void* p2) {return strcmp(*(Char* *) p1, * (Char* *) p2);}//To handle duplicate words in |words|structword_t {Char* W;intCountint* POS;intcur;};intWORD_T_CMP (Const void* P1,Const void* p2) {return strcmp(((Const structword_t*) p1)->w, ((Const structword_t*) p2)->w);}/** * Return An array of size *returnsize. * Note:the returned array must is malloced, assume caller calls free (). * * It is a variant of the longest-substring-without-repetition problem */int* Findsubstring (Char* S,Char* * words,intWordssize,int* returnsize) {if(!s | | wordssize = =0||strlen(s) < wordssize*strlen(words[0]) {*returnsize =0;returnNULL; }if(strlen(s) = =0&&strlen(words[0]) ==0) {*returnsize =1;int* ret = (int*)malloc(sizeof(int)); ret[0] =0;returnRet }intn =strlen(s);intK =strlen(words[0]);//Sort |words| First, prepare for binary searchQsort (words, wordssize,sizeof(Char*), STRCMP1);//construct array of word_t //@note construction, |wts| is already sorted structword_t* WTS = (structword_t*)malloc(Wordssize *sizeof(structword_t));intWtssize =0; for(inti =0; i < wordssize;) {Char* w = words[i];intCount =1; while(++i < wordssize &&!)strcmp(W, Words[i])) count++;structword_t* wt_ptr = WTS + wtssize++; Wt_ptr->w = W; Wt_ptr->count = count; Wt_ptr->pos = (int*)malloc(Count *sizeof(int)); Wt_ptr->cur =0; }//Store one word structword_t wt; WT.W = (Char*)malloc((k +1) *sizeof(Char));//return value intCap =Ten;int* ret = (int*)malloc(Cap *sizeof(int));intSize =0; for(intoffset =0; Offset < K; offset++) {//init word_t array for(inti =0; i < wtssize; i++) {structword_t* wt_ptr = WTS + i; for(intj =0; J < wt_ptr->count; J + +) wt_ptr->pos[j] =-1; Wt_ptr->cur =0; }intstart = offset;//Start pos of current substring intLen =0;//Number of words encountered for(inti = offset; I <= n-k; i + = k) {strncpy(WT.W, S+i, K); WT.W[K] =' + ';structword_t* p = (structword_t*) bsearch (&WT, WTS, Wtssize,sizeof(structword_t), word_t_cmp);if(!p) {start = i + K; Len =0;Continue; }//@note The following five lines covers extra occurrence of //(possible duplicate) Word, can draw some special case //On a paper if it's hard to understand why it could cover //All boundary conditions //|p->cur| and |p->pos| implement a simple rounded queue, //And |p->cur| always point to the smallest index position intpos = p->pos[p->cur]; p->pos[p->cur++] = i;if(P->cur >= p->count) p->cur-= p->count;if(Pos < start) {//Valid occurrence of this word in the current substring started at |start|len++;if(len = wordssize) {//All words encounterd, add to result set if(size = = cap) {//Extend the arrayCap =2*cap;int* TMP = (int*)malloc(Cap *sizeof(int));memcpy(TMP, RET, size*sizeof(int)); Free(ret); RET = tmp; } ret[size++] = start;//Move substring forward by one word lengthStart + = k; len--; } }Else{//extra occurence of (possible duplicat) Word encountered, update |start| and |len|Start = pos + K; Len = (i-start)/k +1; } } }//Cleanup for(inti =0; i < wtssize; i++) Free(Wts[i].pos); Free(WTS); *returnsize = size;if(Size = =0) { Free(ret); ret = NULL; }returnRET;}
Copyright NOTICE: This article is nomasp Couvant original article, without permission is prohibited reprint! Welcome to my blog: http://blog.csdn.net/nomasp
Leetcode Substring with concatenation of all Words (concatenation of all text strings) (*)