Programming Algorithm, programming algorithm manual for beginners

Source: Internet
Author: User

Programming Algorithm, programming algorithm manual for beginners
Suffix Tree code (C)


Address: http://blog.csdn.net/caroline_wendy


Give you a long string s and a collection of many short strings {T1, T2 ,...}, design a method to query T1, T2 ,..., locate the position of Ti in s.


Code:

/* * main.cpp * *  Created on: 2014.7.20 *      Author: Spike *//*eclipse cdt, gcc 4.8.1*/#include <iostream>#include <vector>#include <map>using namespace std;class SuffixTreeNode {map<char, SuffixTreeNode*> children;char value;vector<int> indexes;public:SuffixTreeNode() {}void insertString(string s, int index) {indexes.push_back(index);if (s.length() > 0) {value = s[0];SuffixTreeNode* child = NULL;if (children.find(value) != children.end()) {child = children[value];} else {child = new SuffixTreeNode();children[value] = child;}string remainder = s.substr(1);child->insertString(remainder, index);}}vector<int> getIndexes(string s) {if (s.length() == 0)return indexes;else {char first = s[0];if (children.find(first) != children.end()) {string remainder = s.substr(1);return children[first]->getIndexes(remainder);} else {vector<int> empty;return empty;}}}~SuffixTreeNode() {map<char, SuffixTreeNode*>::iterator it;for (it!=children.begin(); it!=children.end(); ++it)delete it->second;}};class SuffixTree {SuffixTreeNode* root;public:SuffixTree(string s) {root = new SuffixTreeNode;for (int i=0; i<s.length(); ++i) {string suffix = s.substr(i);root->insertString(suffix,i);}}vector<int> getIndexes(string s) {return root->getIndexes(s);}~SuffixTree() {}};int main(void){string testString = "mississippi";string stringList[] = {"is", "sip", "hi", "sis"};SuffixTree tree (testString);for (int i=0; i<4; i++) {vector<int> li = tree.getIndexes(stringList[i]);if (li.size() != 0) {cout << stringList[i] << "  ";for (int j=0; j<li.size(); ++j)cout << li[j] << " ";cout << endl;}}return 0;}

Output:

is  1 4 sip  6 sis  3 





Programming Algorithm Problems

Typical problems with Dynamic Planning of backpacks
1 search method: exhaustive query of all possible fetch Conditions
2. Dynamic Planning
Are you a newbie? Because the data size is very small, you can simply use the search method. It is easier to write programs because it is easier to understand the data ~~
You can find out about your backpack and provide detailed answers everywhere ~~~
Zhidao.baidu.com/question/25002746.html
Similar to this problem, you only need to change the question and code.
After the change, the idea is as follows to help you replace it with the C language ~~~~

There is a box with a capacity of v (positive integer, o ≤ v ≤ 20000) and n items (o ≤ n ≤ 30). Each item has a volume (positive integer ). From n items, if thousands of items are loaded into the box, the remaining space of the box must be zero. (Here v is your s, and here n is your n)

L search method
Void search (int k, int v) {searches for the k-th item, and the remaining space is v}
{
Int I, j;
If (v <best) best = v;
If v-(s [n]-s [k-1])> = best return; // s [n] indicates the weight and
If (k <= n ){
If (v> w [k]) search (k + 1, v-w [k]); // w [n] indicates the weight of the nth item
Search (k + 1, v );
}
}

Best is the global variable, indicating the minimum value of the remaining space of the box. It is good to set the initial value to a very large positive number.
Therefore, if the best value is 0 after search (n, v), it indicates that there is a solution.

2 DP Dynamic Planning (iterative method)
F [I, j] is a boolean type that selects a number of signs from the first I items to make the size exactly j.
Implementation: converts optimization problems into judgment problems

F [I] [j] = (F [I-1] [j-w [I] | F [I-1] [j]) (w [I] <= j <= v) Boundary: f [0] [0] = true;
For (I = 1; I <= n; I ++)
For (j = w [I]; j> = v; j --)
F [I] [j] = (F [I-1] [j-w [I] | F [I-1] [j]);

After calculation, if F [n] [v] is true, there is a solution.

3 DP Dynamic Planning (Iterative Optimization)
Optimization: the current status is only related to the status of the previous stage and can be reduced to one dimension.
F [0] = true;
For (I = 1; I <= n; I ++)
For (j = w [I]; j> = v; j --)
F [j] = (F [j-w [I] | F [j]);

After calculation, if F [v] is true, there is a solution.

4 DP Dynamic Planning (memorandum algorithm)
F is a two-dimensional array. int F [n] [v] is initialized to 0 at the beginning.
Bool dp (int n, int v)
{
If (n = 0 & v = 0) return 1;
If (n = 0 & v! = 0) return-1;
If (F [n] [v]! = 0) return F [n] [v];
If (dp (n-1, v-w [n]) return F [n] [v] = 1;
If (v> = w [n] & dp (n-1, v-w [n]) return F [n] [v] = 1;
Return F [n] [v] =-1;
} ...... Remaining full text>

What is the use of programming algorithms?

Studying other people's algorithms allows you to think on the shoulders of giants.
In fact, we are always in touch with algorithms. On the one hand, we can improve our thinking ability, and on the other hand, we can improve the code quality.
Good algorithms are not obscure, but amazing.

I hope my answers will help you a little. I wish you success!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.