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

Trie+排序sort

Posted by 0801050332 at 2010-11-10 11:37:27 on Problem 1035
#include<iostream>
#include<fstream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<ctime>
#include<cstdlib>



using namespace std;

const int BRANCH_NUM=30;
const int MAX_LEN=20;

typedef struct node
{
      int idx;
      char str[MAX_LEN];
}node;
node record[10000];

typedef struct trie_node
{
      int idx;
      trie_node* branch[BRANCH_NUM];
      void init()
      {
            idx=0;
            memset(branch,NULL,sizeof(branch));
      }
}trie_node;

trie_node m_pool[100000];//这个地方开小了,WA
int nIndex;


class trie_tree
{
      protected:
      trie_node* root;
      public:
      trie_tree();
      void insert(char word[],int idx);
      int search(char word[]);
};

trie_tree::trie_tree()
{
      root=&m_pool[nIndex++];
      root->init();
}

void trie_tree::insert(char word[],int idx)
{
      int pos=0;
      trie_node* location=root;
      int id;
      //bool res=true;

      while('\0'!=word[pos])
      {
            id=word[pos]-'a';
            if(NULL==location->branch[id])
            {
                  location->branch[id]=&m_pool[nIndex++];
                  location->branch[id]->init();
            }
            location=location->branch[id];
            //if(location->isStr)
            //res=false;
            pos++;
      }
      location->idx=idx;
}

int trie_tree::search(char word[])
{
      int pos=0;
      trie_node* location=root;
      while('\0'!=word[pos] && NULL!=location)
      {
            location=location->branch[word[pos]-'a'];
            pos++;

      }
      if(NULL!=location && '\0'==word[pos])
      return location->idx;
      return 0;
}





void deleteLetter(char from[],char to[],int nIndex)
{
      int length=strlen(from);
      for(int i=0;i<nIndex;i++)
      to[i]=from[i];
      for(int i=nIndex;i<length-1;i++)
      to[i]=from[i+1];
      to[length-1]='\0';
}
void insert(char from[],char to[],int nIndex,char c)
{
      int length=strlen(from);
      for(int i=0;i<nIndex;i++)
      to[i]=from[i];
      to[nIndex]=c;
      for(int i=nIndex;i<length;i++)
      to[i+1]=from[i];
      to[length+1]='\0';
}
void modify(char from[],char to[],int nIndex,char c)
{
      strcpy(to,from);
      to[nIndex]=c;
}




inline bool cmp(const node& a,const node& b)
{
      return a.idx<b.idx;
}





int main()
{

      //freopen("input.txt","r",stdin);
      //freopen("output.txt","w",stdout);
      char str[MAX_LEN];

      char temp[MAX_LEN];

      trie_tree t;
      int idx=1;
      while(scanf("%s",str)!=EOF)
      {
            if('#'==str[0])
            break;
            t.insert(str,idx++);


      }
      int nCount=0;
      while(scanf("%s",str)!=EOF)
      {
            if('#'==str[0])
            break;
            if(t.search(str))
            {
                  printf("%s is correct\n",str);
            }
            else
            {
                  int x;
                  nCount=0;
                  int length=strlen(str);
                  for(int i=0;i<length;i++)
                  {
                        deleteLetter(str,temp,i);
                       // printf("%s\n",temp);
                        if(0!=(x=t.search(temp)))
                        {
                              strcpy(record[nCount++].str,temp);
                              record[nCount-1].idx=x;
                        }
                        for(int j=0;j<26;j++)
                        {
                              modify(str,temp,i,'a'+j);
                              //printf("%s\n",temp);
                              if(0!=(x=t.search(temp)))
                              {
                                    strcpy(record[nCount++].str,temp);
                                    record[nCount-1].idx=x;
                              }
                              insert(str,temp,i,'a'+j);
                             // printf("%s\n",temp);
                              if(0!=(x=t.search(temp)))
                              {
                                    strcpy(record[nCount++].str,temp);
                                    record[nCount-1].idx=x;
                              }

                        }



                  }
                  for(int i=0;i<26;i++)
                  {
                        insert(str,temp,length,'a'+i);
                        //printf("%s\n",temp);
                        if(0!=(x=t.search(temp)))
                        {
                              strcpy(record[nCount++].str,temp);
                              record[nCount-1].idx=x;
                        }
                  }
                  sort(record,record+nCount,cmp);
                  printf("%s:",str);
                  if(nCount)
                  printf(" %s",record[0].str);
                  for(int i=1;i<nCount;i++)
                  {
                        if(record[i].idx!=record[i-1].idx)//还有这里
                                             //注意判重,这是我出错的第二个地方
                        printf(" %s",record[i].str);
                  }
                  printf("\n");

            }
      }







      return 0;
}



/*



int main()
{
      freopen("input.txt","w",stdout);
      srand(time(NULL));
      for(int i=0;i<10000;i++)
      {
            int length=(rand()%15)+1;
            for(int j=0;j<length;j++)
            cout<<char('a'+(rand()%26));
            cout<<endl;
      }
      cout<<"#"<<endl;
      for(int i=0;i<50;i++)
      {
            int length=(rand()%15)+1;
            for(int j=0;j<length;j++)
            cout<<char('a'+(rand()%26));
            cout<<endl;
      }
      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