九度機試 題目1165:字串匹配 2008年北京航空航天大學電腦研究生機試真題

來源:互聯網
上載者:User

標籤:

題目1165:字串匹配

時間限制:1 秒

記憶體限制:32 兆

特殊判題:否

提交:2497

解決:858

題目描寫敘述:

    讀入資料string[ ]。然後讀入一個短字串。要求尋找string[ ]中和短字串的全部匹配。輸出行號、匹配字串。匹配時不區分大寫和小寫,而且能夠有一個用中括弧表示的模式比對。

如“aa[123]bb”。就是說aa1bb、aa2bb、aa3bb都算匹配。

輸入:

輸入有多組資料。
每組資料第一行輸入n(1<=n<=1000)。從第二行開始輸入n個字串(不含空格)。接下來輸入一個匹配字串。

輸出:

輸出匹配到的字串的行號和該字串(匹配時不區分大寫和小寫)。

例子輸入:
4Aaba2BabABBa[a2b]b
例子輸出:
1 Aab2 a2B4 ABB
來源:
2008年北京航空航天大學電腦研究生機試真題

#include <iostream>#include #include #include #include #include using namespace std;int main(){    int n=0;    while(cin>>n){        string *s=new string[n+1];        string *tmp=new string[n+1];        for(int i=0;i<=n;i++){            cin>>s[i];            tmp[i]=s[i].substr(0,s[i].length());        }        for(int i=0;i<=n;i++){            for(int j=0;j<s[i].length();j++){                if(s[i][j]!=‘[‘&&s[i][j]!=‘]‘)                    s[i][j]=tolower(s[i][j]);            }        }        int pos=0;        int start=s[n].find(‘[‘);        int end=s[n].find(‘]‘);        string head=s[n].substr(0,start);        string tail=s[n].substr(end+1,s[n].length()-end);        string *result=new string[end-start-1];        for(int i=start+1;i<end;i++){            result[pos]=head+s[n][i]+tail;            for(int j=0;j<n;j++){                if(result[pos].compare(s[j])==0){                    cout<<j+1<<" "<<tmp[j]<<endl;                    break;                }            }            pos++;        }    }    return 0;} /**************************************************************    Problem: 1165    User: 愛理momoko    Language: C++    Result: Accepted    Time:0 ms    Memory:1520 kb****************************************************************/

九度機試 題目1165:字串匹配 2008年北京航空航天大學電腦研究生機試真題

聯繫我們

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