avatar
one question about algorithm# Programming - 葵花宝典
c*g
1
given one article, and a set of keywords
find the shortest length part which contain all the keywords
how to do that?
After translated, it will be: given a sequence, how to
find a shortest substring that contains all the items required. Example: a s
equence "abaccdefgacel" and a set of key words "a" "c", "d" and "e". The sho
rtest substring contianing all of the key words is "accde". Find one of such
shortest substrings.
avatar
j*e
2
Let me try:
step 1: traverse the string once, and count the occurance of each key
characters in the search string: i.e. a:3, c:3, d:1, e:1
step 2 : Recursion
/*"count" maps key characters to the # of occurance*/
/*"stringPath" is the longest string path so far containing all key characters
*/
void checkShortest(deque& stringPath, Map& count)
{
char head = stringPath.front();
char tail = stringPath.back();
/*Head character is not a key character*/
/*Or it is a key character, and h
avatar
s*u
3
are你sure就是一个字母的key?
而且看不懂你的什么意思 -_-

characters

【在 j********e 的大作中提到】
: Let me try:
: step 1: traverse the string once, and count the occurance of each key
: characters in the search string: i.e. a:3, c:3, d:1, e:1
: step 2 : Recursion
: /*"count" maps key characters to the # of occurance*/
: /*"stringPath" is the longest string path so far containing all key characters
: */
: void checkShortest(deque& stringPath, Map& count)
: {
: char head = stringPath.front();

avatar
j*e
4
I just worked off the simplified example in the original post. For string
keyword, the same token applies.

【在 s****u 的大作中提到】
: are你sure就是一个字母的key?
: 而且看不懂你的什么意思 -_-
:
: characters

avatar
s*0
5
Definition: for single keyword X(consists of one or more characters),
*f_start1(X)
means the earliest poistion of the first character of X in the article.
*f_end1(X)
means the latest position of the first character of X in the article.
*f_start2(X)
means the earliest poistion of the last character of X in the article.
*f_end2(X)
means the latest position of the last character of X in the article.
For n keywords X1,X2,X3...,Xn, suppose the starting and ending position
of the shortest sub-article
avatar
i*r
6
One solution: O(N) time complexity
str is the input string,
K is the set of characters to be covered.
1. Create a structure to remember best result
Create a solution set R, which includes 0 solution.
A solution includes: a) starting index b) a bitmap showing which
characters are already covered.
2. scan through the string, for every character ch (with index = i)
2.1 if ch is not in K, do nothing and move on
2.2 if ch is in K, initialize a solution and put in R
s.index = i, ch
avatar
s*u
7
ft,character的key的话,简单的贪心就可以O(n)

【在 i********r 的大作中提到】
: One solution: O(N) time complexity
: str is the input string,
: K is the set of characters to be covered.
: 1. Create a structure to remember best result
: Create a solution set R, which includes 0 solution.
: A solution includes: a) starting index b) a bitmap showing which
: characters are already covered.
: 2. scan through the string, for every character ch (with index = i)
: 2.1 if ch is not in K, do nothing and move on
: 2.2 if ch is in K, initialize a solution and put in R

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。