Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

记住:大牛是不会理两种人的:1.读题不认真的人2.连KMP都没掌握好的人

Posted by fanhqme at 2009-02-22 12:14:48 on Problem 3510
In Reply To:如何能够在这种题上WA了N次后找到一个乐于助人的大牛? Posted by:AllwaysDead at 2009-02-21 16:45:57
......
class KMPComparer{
private:
        int id,pi[10];
        char chs[10];
public:
       KMPComparer(char *a){
                     chs[0]=a[0];
                     for (int i=1;a[i-1];i++)chs[i]=a[i];
                     id=-1;
                     pi[0]=-1;
                     for (int i=1;chs[i];i++){
                         pi[i]=pi[i-1];
                         while (pi[i]!=-1 && chs[pi[i]+1]!=chs[i])pi[i]=pi[pi[i]];
                         if (chs[pi[i]+1]==chs[i])pi[i]++;
                     }
       }
       void putch(char a){
            while (id!=-1 && chs[id+1]!=a)id=pi[id];//////////////////////////////////////////要想用O(1)的时间来解决这种事,KMP是不二的选择
            if (chs[id+1]==a)id++;
       }
       int compared(){return chs[id+1]==0;}
};
......
int main()
{
    char a;
    KMPComparer cDD("dd"),cEOF("EOF"),cPink("pink"),cEI("ei"),cCEI("cei");
    Outputpool outP;
    while ((a=getchar())!=EOF){
          cDD.putch(a);cEOF.putch(a);cPink.putch(a);cEI.putch(a);cCEI.putch(a);
          if (cEOF.compared())break;
          if (cDD.compared()){
             outP.rollback(1);
             outP.putch('p');
          }else if (cEI.compared() && cCEI.compared()==0){/////////////////////////////////////////////////////////////读题!
                outP.rollback(1);
                outP.putch("ie");
          }else if (cPink.compared()){
               outP.rollback(3);
               outP.putch("floyd");
          }else if (a>='a' && a<='z' || a==' ' || a=='\n' || a=='\t')outP.putch(a);
    }
    outP.empty();
    getchar();getchar();
    return 0;
}


Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator