編程之美最短摘要產生

來源:互聯網
上載者:User

本來以為這個會很難的,因為自己不是很瞭解這方面的東西。

看了下解釋 大致明白了,就是用最少的句子 ,包含所有的關鍵詞。

ok~看了編程之美的code後 感覺很好,類似kmp演算法,跳過已經比對過的字串,然後向後移動。

曾經做過一個單調隊列的,就是一個數組,移動滑動視窗,判斷視窗裡面的最大值最小值

思路就是

比如 我的關鍵詞為 abde

我的句子  hello  are  you bottom of do the is bot doke astring.

在一個句子裡面找到包含關鍵詞的 最短的句子。當然 關鍵詞可以無序排列。

編程之美的思路 就是

設定一個begin 一個end 起初都指向句子頭

end向後移動一直到包含所有關鍵詞 停止。

記錄長度,同時移動begin,直至不包含某關鍵詞後,再次移動end,直至包含。

總結來說  就是 一遍獲得所有結果。不回溯

#include "stdio.h"#include "string.h"#include "assert.h"#define MAX 1024int isMatchAll(const char *str,const char *key,int begin,int end){int ret =0;char hash[256];int i =0;int lenK = strlen(key);memset(hash,0,sizeof(hash));for(i=begin;i<=end;i++){hash[str[i]]=1;}for(i=0;i<lenK;i++){if(hash[key[i]]==0)break;}if(i == lenK )ret =1;return ret;}void find(const char *str,const char *key){int lenS = strlen(str);int lenK = strlen(key);int begin = 0;int end = 0;int minLength = 0x7FFFFFFF;int mstart = 0;int mend =0;assert(str&&key);for(;;){while(!isMatchAll(str,key,begin,end)&&end<lenS){end++;}while(isMatchAll(str,key,begin,end)){if(end-begin+1 <minLength){minLength = end-begin+1;mstart =begin;mend =end;}begin++;}if(end>=lenS)break;}printf("%d\n",minLength);for(;mstart<=mend;mstart++)printf("%c",str[mstart]);}int main(){char str[MAX];char key[MAX];gets(str);gets(key);find(str,key);return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.