編程演算法,編程演算法新手自學手冊

來源:互聯網
上載者:User

編程演算法,編程演算法新手自學手冊
尾碼樹(Suffix Tree) 代碼(C)


本文地址: http://blog.csdn.net/caroline_wendy


給你一個長字串s與很多短字串集合{T1,, T2, ...}, 設計一個方法在s中查詢T1, T2, ..., 要求找出Ti在s中的位置.


代碼:

/* * 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;}

輸出:

is  1 4 sip  6 sis  3 





編程演算法問題

典型的動態規劃背包問題
1 搜尋法:窮舉所有可能的取數情況
2 動態規劃
你是新手吧?由於資料規模很小,直接用搜尋法就行,比動歸好理解,程式也容易寫~~
你找一下背包問題,到處都有詳細解答~~~
zhidao.baidu.com/question/25002746.html
跟這個問題類似 只要稍微改一下題目和代碼就行
改了之後思路如下 幫你換成c語言了 給分啊~~~~

有一個箱子容量為v(正整數,o≤v≤20000),同時有n個物品(o≤n≤30),每個物品有一個體積(正整數)。要求從 n 個物品中,任取若千個裝入箱內,使箱子的剩餘空間為零。(這裡的v就是你的s,這裡的n就是你的n)

l 搜尋方法
void search(int k, int v) {搜尋第k個物品,剩餘空間為v}
{
int i, j;
if(v<best) best=v;
if v-(s[n]-s[k-1])>=best return; // s[n]為前n個物品的重量和
if(k<=n){
if(v>w[k]) search(k+1,v-w[k]); // w[n]為第n個物品的重量
search(k+1,v);
}
}

best為全域變數,表示箱子的剩餘空間的最小值,初始值為設為很大的正數就好
所以 search(n,v)後 best為0則表示有解

2 DP 動態規劃(迭代法)
F[I,j]為前i個物品中選擇若干個放入使其體積正好為j的標誌,為布爾型。
實現:將最佳化問題轉化為判定性問題

F [I][j] =( F [ I-1 ][ j-w[I] ] || F [ I-1 ][ j ] )(w[I]<=j<=v) 邊界: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 ] ) ;

算完之後 F[n][v]為true則有解

3 DP 動態規劃(迭代法最佳化)
最佳化:目前狀態只與前一階段狀態有關,可降至一維。
F[0]=true;
for(I=1;I<=n;I++)
for(j=w[I];j>=v;j--)
F [j] =( F [ j-w[I] ] || F [ j ] );

算完之後 F[v]為true則有解

4 DP 動態規劃(備忘錄演算法)
F為二維數組int F[n][v] 一開始初始化為0
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 return F[n][v]=1;
return F[n][v]=-1;
}......餘下全文>>
 
編程演算法有什用?

研究學習別人的演算法,能夠讓你站在巨人的肩膀上思考問題。
其實我們身邊無時不刻都在接觸演算法,一方面提高自身思考的能力,一方面可以提升代碼品質。
好的演算法不是晦澀難懂的,而是能夠讓人拍手稱奇的。

希望我的回答能對你有些許協助,謹祝你成功!
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.