hdu1606&&zoj1315

题目意思:给出若干关键的单词,和若干的句子,从句子中找出包含关键词出现次数最多的输出,当次数相同时以任意顺序输出

解题思路:此题目主要是字符类的操作而且是关注字母的故可以用字典树也称Trie树的这个算法做,最后排下序,将最大的句子按格式要求输出即可

 

代码:

  

  1 #include<iostream>
  2 #include<cstdio>
  3 #include<cstring>
  4 #include<cmath>
  5 #include<algorithm>
  6 using namespace std;
  7 
  8 const int coutNum=26;
  9 
 10 struct Trie 
 11 {
 12     int v;
 13     struct Trie *next[coutNum];//各个分支 
 14 };
 15 
 16 //用来保存句子的顺序号和出现关键词的次数 
 17 struct node
 18 {
 19     int i;
 20     int c;
 21     bool operator <(node cc)const
 22     {
 23         return c>cc.c;
 24     }
 25 }AS[60];
 26 
 27 //字典树的建立  
 28 void insert(Trie *&root , char *s)
 29 {
 30     Trie *p=root;
 31     
 32     if(p==NULL)
 33     {
 34         Trie *temp=(Trie *)malloc(sizeof(Trie));
 35         temp->v=0;
 36         
 37         for(int i=0;i<coutNum;++i)
 38          temp->next[i]=NULL;
 39          
 40          p=temp;
 41          root=p;
 42     }
 43     
 44     while(*s)
 45     {
 46         if(p->next[*s-'a']==NULL)
 47         {
 48             Trie *temp=(Trie *)malloc(sizeof(Trie));
 49             temp->v=0;
 50             
 51             for(int i=0;i<coutNum;++i)
 52              temp->next[i]=NULL;
 53              
 54             p->next[*s-'a']=temp;
 55             p=p->next[*s-'a']; 
 56         }
 57         else
 58         p=p->next[*s-'a'];
 59      s++;
 60     }
 61     p->v++;    //关键词处标记 
 62 }
 63 
 64 //字典树的查找 
 65 int find(Trie *root,char *s)
 66 {
 67     if(root==NULL)
 68      return 0;
 69      
 70     Trie *p=root;
 71     while(*s)
 72     {
 73         if(p->next[*s-'a'])
 74          p=p->next[*s-'a'];
 75         else
 76          return 0;
 77          s++;
 78     }
 79     if(p->v)//存在则返回1 
 80     {
 81       return 1;    
 82      }
 83     return 0;
 84     
 85 }
 86 
 87 int n,m;
 88 
 89 char str1[26];
 90 char str2[26][80];
 91 int main()
 92 {
 93     int cnt=0;
 94     while(~scanf("%d%d",&n,&m))
 95     {
 96         Trie *head=NULL;
 97         getchar();
 98         
 99         //关键词的输入和字典树的建立 
100         memset(str1,'\0',sizeof str1);
101         for(int i=0;i<n;++i)
102         {
103             gets(str1);
104             insert(head,str1);
105         }
106         int k;
107         memset(AS,0,sizeof AS);
108         for(int i=0;i<m;++i)
109         {
110             gets(str2[i]);
111     
112             char ss;
113             char sk[80];
114             k=0;
115             int len=strlen(str2[i]);
116              AS[i].i=i;
117              //句子的输入 
118             for(int j=0;j<len;++j)
119             {
120                 ss=str2[i][j];
121                 if(isupper(ss))
122                  ss+=32;
123                 if(ss>='a'&&ss<='z')
124                 {
125                     sk[k++]=ss;
126                 }
127                 else
128                 {
129                     if(k>0)
130                      {
131                        AS[i].c+=find(head,sk);
132                         memset(sk,'\0',sizeof sk);
133                      }
134                      k=0;
135                 } 
136             }
137         }        
138         //排序 
139     sort(AS,AS+m);
140     int pos=0;
141     for(int i=0;i<m;i++)
142     {
143         if(AS[0].c==AS[i].c)
144         {
145             pos=i;
146         }
147     }
148     
149     printf("Excuse Set #%d\n",++cnt);
150     for(int i=0;i<=pos;++i)
151     {
152         printf("%s\n",str2[AS[i].i]);
153     }
154     
155     puts("");
156     }
157     return 0;
158 }
View Code

PS:此题目在排序是开始时从小到大排序 的  提交居然是错的 而后从大到小排序 提交通过 不是说输出没有顺序..

posted @ 2013-09-09 20:23  Vertex_ZL  阅读(170)  评论(0)    收藏  举报