zoukankan      html  css  js  c++  java
  • zoj 2840 File Searching

    题目地址: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;
           }
    
       }
    }
    


      

  • 相关阅读:
    睁大你的眼睛,警惕职业生涯中的“红灯”
    几则好玩的脑筋急转弯
    File.Delete()的解决
    Powershell之True或False
    发送邮件
    IE与CSS兼容性
    WSS
    File.Delete()
    添加field部署
    css加载
  • 原文地址:https://www.cnblogs.com/814jingqi/p/3247196.html
Copyright © 2011-2022 走看看