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

贴我longlong长的暴搜,0ms

Posted by javvy at 2010-08-26 20:09:08 on Problem 1270
#include "iostream"
#include "string"
using namespace std;
char s[21];
char rs[300][21];
int used[20];
int start;
int len;

class  stack
{
       public:
       char ss[21];
       int top;
       stack()
       {
              top=0;
       }
       void push(char c)
       {
            ss[top++]=c;
       }
       void pop()
       {
            top--;
       }
       void print()
       {
            for(int i=0;i<top;i++)
                    cout<<ss[i];
            cout<<endl;     
       }
       int find(char c)
       {
            int num=0;
            for(int i=0;i<top;i++)
                    if(ss[i]==c)
                                num++;
            return num;                            
       }
};

class Node
{
      public:
      char a;
      char b;
      Node* next;
      Node(char c,char d)
      {
           a=c;
           b=d;
           next=NULL;    
      }
      Node()
      {
            next=NULL;
      }
};

class hash
{
      public:
      Node table[50];
      hash()
      {
            for(int i=0;i<50;i++)
                    table[i]=Node();
      }
      int addr(Node m)
      {
             return m.b-'a';       
      }
      int addr2(char c)
      {
           return c-'a';
      }
      void add(Node m)
      {
             int ar=addr(m);
             Node* p=table[ar].next;
             if(p==NULL)
                     table[ar].next=new Node(m.a,m.b);   
            else
            {
                 while(p->next!=NULL)
                 {
                       p=p->next;   
                 }
                 p->next=new Node(m.a,m.b);
            }      
      }
      bool find2(char c)
      {
            int ar=addr2(c);
            Node* p=table[ar].next;
            while(p!=NULL)
            {
                  if(p->b==c)
                          return 1;
                  p=p->next;                   
            }
            return 0;
      }
        bool find(Node m)
      {
            int ar=addr(m);
            Node* p=table[ar].next;
            while(p!=NULL)
            {
                  if(p->b==m.b&&p->a==m.a)
                          return 1;
                  p=p->next;                  
            }
            return 0;
      }
      
      
      
};

void dfs(int now,stack st,hash h)
{
     if(now==strlen(s))
     {
            for(int i=0;i<strlen(s);i++)
                    rs[start][i]=st.ss[i];
            start++;       
            return;           
     } 
     for(int i=0;i<strlen(s);i++)
     {
             if(used[i])continue;
             int flag=0;
             for(int j=0;j<st.top;j++)
             {
                    Node m(s[i],st.ss[j]);
                    if(h.find(m))
                    {
                            flag=1;
                            break;     
                    }                
             }
             if(flag)continue;
             used[i]=1;
             st.push(s[i]);
             dfs(now+1,st,h);
             st.pop();
             used[i]=0;
     }
}

int cmp(char s1[][21],int i,int j)
{
     for(int k=0;k<strlen(s);k++)
     {
             if(s1[i][k]==s1[j][k])continue;
             else
                 return s1[i][k]-s1[j][k];
     }
     return 0;
}
int main()
{
    string t;
    int i,j;
    while(getline(cin,t))
    {
          for(i=j=0;i<t.length();i++)
          {
                 if(t[i]>='a'&&t[i]<='z')
                       s[j++]=t[i];              
          }
          s[j]='\0';
          getline(cin,t);
          int index=1;
          Node m;
          hash h;
          for(i=0;i<t.length();i++)
          {
                  if(t[i]>='a'&&t[i]<='z')
                  {
                        if(index%2==1)
                                m.a=t[i];
                        else
                        {
                                m.b=t[i];
                                h.add(m);
                        }
                        index++;                        
                  }                 
          }
          
          start=0;            
          for(int i=0;i<strlen(s);i++)
          {
                  memset(used,0,sizeof(used));
                  stack st;
                  if(h.find2(s[i]))
                        continue;
                  used[i]=1;
                  st.push(s[i]);            
                  dfs(1,st,h);     
                  
          }
          int visit[300];
          memset(visit,0,sizeof(visit));
          while(1)
          {
               int flag=0,i,temp;   
              for(i=0;i<start;i++)
              {                                  
                      if(!visit[i])
                      {
                               temp=i;
                               flag=1;
                               break;    
                      }
              }
              if(flag==0)
                         break;    
              for(int i=1;i<start;i++)
              {      
                     if(cmp(rs,temp,i)==0)
                            visit[i]=1;                    
                     if(!visit[i]&&cmp(rs,temp,i)>0)
                     {
                            temp=i;             
                     }
              }
              for(int i=0;i<strlen(s);i++)
                      cout<<rs[temp][i];
              cout<<endl;
              visit[temp]=1; 
          }
          cout<<endl;           
    } 
    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