zoj 2840 File Searching

來源:互聯網
上載者:User

題目地址:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2612

題解:  1 只用找到星號的位置,然後比較兩端

            2使用algorithm中的反轉比較方便,不用管第二段開始的位置

            3 容易忽略的地方,至少b要和a去掉星號後一樣長啊,否則  a*a 和a這種情況也匹配了

            4要求多多的輸入輸出,不說了

代碼:

#include<iostream>#include<string>#include<cstring>#include<algorithm>using namespace std;int match(string a,string b)      // a is original  b contains  *{   if(a.length()<b.length()-1)  return 0;   int spark=0;   int sizeb=b.length();   for(int i=0;i<sizeb;i++)     if(b[i]=='*')     {          spark=i;          break;     }   string b1=b.substr(0,spark);   string a1=a.substr(0,spark);   reverse(a.begin(),a.end());    // include的是algorithm   reverse(b.begin(),b.end());   string a2=a.substr(0,b.length()-1-spark);   string b2=b.substr(0,b.length()-1-spark);   if(a1==b1&&a2==b2)   return 1;   else return 0;}int main(){   int n;   bool isfirst=true;   while(cin>>n)   {      if(isfirst==false)  cout<<endl;      if(isfirst==true)  isfirst=false;      string * p=new string [n];       for(int i=0;i<n;i++)         cin>>p[i];       int m;       cin>>m;       string key;       for(int i=0;i<m;i++)       {           cin>>key;           int exist=0;           for(int j=0;j<n;j++)             if(match(p[j],key))              {               if(exist==1)cout<<", ";               cout<<p[j];               exist=1;              }          if(exist==0)   cout<<"FILE NOT FOUND";        cout<<endl;       }   }}

  

聯繫我們

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