标签:
给定一个序列S以及它的一个子序列T,求S的所有包含T的子序列。例:
void PrintDistinctSubByFlags(char* seq, int seq_len, bool* seq_flags) { printf("\r\n"); char buf[] = " "; for (int i = 0; i < seq_len; ++i) { if (seq_flags[i]) { buf[0] = seq[i]; printf(buf); } } } void DistinctSubInner(char* seq, int seq_len, bool* seq_flags, int seq_flags_idx) { if (seq_flags_idx >= seq_len) { PrintDistinctSubByFlags(seq, seq_len, seq_flags); return; } seq_flags[seq_flags_idx] = false; DistinctSubInner(seq, seq_len, seq_flags, seq_flags_idx + 1); seq_flags[seq_flags_idx] = true; DistinctSubInner(seq, seq_len, seq_flags, seq_flags_idx + 1); } void DistinctSub(char* whole_seq) { if(!whole_seq || !*whole_seq) { return; } bool* seq_flags = new bool[strlen(whole_seq) + 1]; DistinctSubInner(whole_seq, strlen(whole_seq), seq_flags, 0); delete seq_flags; }
void DistinctSubInner(char* whole_seq, char* sub_seq, int sub_seq_len) { if (!*whole_seq) { PrintDistinctSub(sub_seq_len); return; } sub_seq[sub_seq_len] = *whole_seq; DistinctSubInner(whole_seq + 1, sub_seq, sub_seq_len + 1); // output head of S DistinctSubInner(whole_seq + 1, sub_seq, sub_seq_len); // not ouput head of S } void DistinctSub(char* whole_seq) { if(!whole_seq || !*whole_seq) { return; } sub_seq = new char[strlen(whole_seq)]; DistinctSubInner(whole_seq, sub_seq, 0); delete sub_seq; }
void DistinctSubInner(char* whole_seq, char* min_seq, char* sub_seq, int sub_seq_len) { if (!*whole_seq) { if(!*min_seq) { PrintDistinctSub(sub_seq, sub_seq_len); } else { // unmatch sub sequence } return; } sub_seq[sub_seq_len] = *whole_seq; if (*whole_seq == *min_seq) { // 1. output head of S and match head of T DistinctSubInner(whole_seq + 1, min_seq + 1, sub_seq, sub_seq_len + 1); } // 2. output head of S but do not match head of T DistinctSubInner(whole_seq + 1, min_seq, sub_seq, sub_seq_len + 1); // 3. do not ouput head of S DistinctSubInner(whole_seq + 1, min_seq, sub_seq, sub_seq_len); } void DistinctSub(char* whole_seq, char* min_seq) { if(!whole_seq || !*whole_seq || !min_seq || !*min_seq) { return; } char* sub_seq = new char[strlen(whole_seq) + 1]; DistinctSubInner(whole_seq, min_seq, sub_seq, 0); delete sub_seq; }
void DistinctSubInner(char* whole_seq, int sub_seq_len) { if (!*whole_seq) { if(!*min_seq) { PrintDistinctSub(sub_seq_len); } return; } sub_seq[sub_seq_len] = *whole_seq; if (*whole_seq == *min_seq) { DistinctSubInner(whole_seq + 1, min_seq + 1, sub_seq_len + 1); } else { DistinctSubInner(whole_seq + 1, min_seq, sub_seq_len + 1); } DistinctSubInner(whole_seq + 1, sub_seq_len); }
void DistinctSubInner(char* whole_seq, int sub_seq_len) { if (!*whole_seq) { if(!*min_seq) { PrintDistinctSub(sub_seq_len); } return; } sub_seq[sub_seq_len] = *whole_seq; if (*whole_seq == *min_seq) { DistinctSubInner(whole_seq + 1, min_seq + 1, sub_seq_len + 1); } else { DistinctSubInner(whole_seq + 1, min_seq, sub_seq_len + 1); } if(sub_seq[sub_seq_len] != sub_seq[sub_seq_len - 1]) { DistinctSubInner(whole_seq + 1, min_seq, sub_seq_len); } }
int main(int argc, _TCHAR* argv[]) { char* whole_seqs[] = { "1", "124", "1234", "1124", "1224", "1244", "12324", "135635624", "1323245", "13523524",}; for (int i = 0; i < sizeof(whole_seqs) / sizeof(char*); ++i) { printf("\r\n\r\ndistinct sub sequence of \"%s\" for \"%s\" : ===============", whole_seqs[i], "124"); DistinctSub(whole_seqs[i], "124"); } getchar(); }
算法题:求一个序列S中所有包含T的子序列(distinct sub sequence)
标签:
原文地址:http://www.cnblogs.com/yedaoq/p/5840917.html