FI2

来源:互联网 发布:人物设定本子淘宝 编辑:程序博客网 时间:2024/04/29 16:07
HelloHellodata = "thisisastring" 13search = "is" => 2search = "isa" => 4search = "cat" => -1int findStringInData(string &data, string &search){    for(int i=0; i < data.size()-search.size() + 1; i++)    {        int tmpi = i;        int j=0;        for(; j < search.size(); tmpi++, j++)        {            if(data[tmpi]!=data[j]) break;        }        if(j==search.size()) return i;    }    return -1; // error code}const string data = "anowijflker...." 10 million characters// search has a maximum length of 20 charactersint searchService(string &search) {    return findStringInData(data, search);}BoggleXXXX  THADXXXX  ISMC123X  EFEPXXXX  ZLUWTHIS -> yesSHIS -> noEFE -> yesword.size() == w8^w * 16 = O(2^n)bool wordExists(vector<string> &board, string &word){    if(board.size()==0) return false;        for(int i=0; i<board.size(); i++)    {        for(int j=0; j<board[0].size(); j++)        {            if(wordExistsRec(board, word, bstate, found, nx, ny) return true;        }    }    return false;}bool isLegal(vector<string> &board, int x, int y);bool wordExistsRec(vector<string> &board, string &word, vector<vector<bool> > &bstate, int found, int x, int y){    // Judge found?    if(found==word.size()) return true;        // Iterate next state    int[] delta = {-1, 0, +1};        for(int i=0; i<3; i++)    {        for(int j=0; j<3; j++)        {            int nx = x + delta[i];            int ny = y + delta[j];            if(nx==x && xy==y) continue;                        if(isLegal(board, nx, ny) && bstate[nx][ny]==false && word[found]==board[nx][ny])            {                bstate[nx][ny]=true;                found++;                if(wordExistsRec(board, word, bstate, found, nx, ny)) return true;                                found--;                bstate[nx][ny]=false;            }        }    }        // Final answer        return false;}


原创粉丝点击