poj 3007 Organize Your Train part II(二叉排序树)

题目:http://poj.org/problem?id=3007

题意:按照图示的改变字符串,问有多少种。。字符串。。

思路:分几种排序的方法,,刚开始用map 超时(map效率不高啊。。),后来搜了一下题解,用二叉排序树。。。

先贴AC代码:

  1 #include <iostream>
  2 #include <cstring>
  3 #include <algorithm>
  4 using namespace std;
  5 
  6 typedef struct tree
  7 {
  8     char str[100];
  9     struct tree *l,*r;
 10 }tr;
 11 tr head;
 12 int check(char s[])
 13 {
 14     tr *q;
 15     tr *p=&head;
 16     while(p)
 17     {
 18         if(strcmp(p->str,s)>0)
 19         {
 20             if(p->l)
 21             p=p->l;
 22             else
 23             {
 24                 q=new tr;
 25                 strcpy(q->str,s);
 26                 q->l=NULL;
 27                 q->r=NULL;
 28                 p->l=q;
 29                 return 1;
 30             }
 31         }
 32         else if(strcmp(p->str,s)<0)
 33         {
 34             if(p->r)
 35             p=p->r;
 36             else
 37             {
 38                 q=new tr;
 39                 strcpy(q->str,s);
 40                 q->l=NULL;
 41                 q->r=NULL;
 42                 p->r=q;
 43                 return 1;
 44             }
 45         }
 46         else
 47         return 0;
 48     }
 49     return 0;
 50 }
 51 /*void pre(tr *qqq)
 52 {
 53     if(qqq)
 54     {
 55         cout<<qqq->str<<endl;
 56         pre(qqq->l);
 57         pre(qqq->r);
 58     }
 59 }*/
 60 int main()
 61 {
 62     int t,i,j,len,cou,sum;
 63     char s[100],s1[100];
 64     cin>>t;
 65     while(t--)
 66     {
 67         cin>>s;
 68         strcpy(head.str,s);
 69         head.l=NULL; head.r=NULL;
 70         sum=1;
 71         len=strlen(s);
 72         for(i=1; i<len; i++)
 73         {
 74             cou=0;
 75             for(j=0; j<i; j++)
 76             s1[cou++]=s[j];
 77             for(j=len-1; j>=i; j--)
 78             s1[cou++]=s[j];
 79             s1[cou]='\0';
 80             if(check(s1))
 81             sum++;
 82 
 83             cou=0;
 84             for(j=len-1; j>=i; j--)
 85             s1[cou++]=s[j];
 86             for(j=0; j<i; j++)
 87             s1[cou++]=s[j];
 88             s1[cou]='\0';
 89             if(check(s))
 90             sum++;
 91 
 92             cou=0;
 93             for(j=i-1; j>=0; j--)
 94             s1[cou++]=s[j];
 95             for(j=len-1; j>=i; j--)
 96             s1[cou++]=s[j];
 97             s1[cou]='\0';
 98             if(check(s1))
 99             sum++;
100 
101             cou=0;
102             for(j=len-1; j>=i; j--)
103             s1[cou++]=s[j];
104             for(j=i-1; j>=0; j--)
105             s1[cou++]=s[j];
106             s1[cou]='\0';
107             if(check(s1))
108             sum++;
109 
110             cou=0;
111             for(j=0; j<i; j++)
112             s1[cou++]=s[j];
113             for(j=i; j<=len-1; j++)
114             s1[cou++]=s[j];
115             s1[cou]='\0';
116             if(check(s1))
117             sum++;
118 
119             cou=0;
120             for(j=i; j<=len-1; j++)
121             s1[cou++]=s[j];
122             for(j=0; j<i; j++)
123             s1[cou++]=s[j];
124             s1[cou]='\0';
125             if(check(s1))
126             sum++;
127 
128             cou=0;
129             for(j=i-1; j>=0; j--)
130             s1[cou++]=s[j];
131             for(j=i; j<=len-1; j++)
132             s1[cou++]=s[j];
133             s1[cou]='\0';
134             if(check(s1))
135             sum++;
136 
137             cou=0;
138             for(j=i; j<=len-1; j++)
139             s1[cou++]=s[j];
140             for(j=i-1; j>=0; j--)
141             s1[cou++]=s[j];
142             s1[cou]='\0';
143             if(check(s1))
144             sum++;
145 
146             cou=0;
147             for(j=len-1; j>=i; j--)
148             s1[cou++]=s[j];
149             for(j=0; j<=i-1; j++)
150             s1[cou++]=s[j];
151             s1[cou]='\0';
152             if(check(s1))
153             sum++;
154         }
155         cout<<sum<<endl;
156         //tr *qqq;
157         //qqq=&head;
158         //pre(qqq);
159     }
160     return 0;
161 }

 

再贴一下map超时的。。

  1 #include <iostream>
  2 #include <cstring>
  3 #include <map>
  4 #include <algorithm>
  5 using namespace std;
  6 
  7 int main()
  8 {
  9     int t,i,j,len,cou,sum;
 10     char s[100],s1[100];
 11     int f;
 12     cin>>t;
 13     while(t--)
 14     {
 15         map<string,int>mp;
 16         map<string,int>::iterator iter;
 17         cin>>s;
 18         sum=0;
 19         len=strlen(s);
 20         for(i=1; i<len; i++)
 21         {
 22             cou=0;
 23             for(j=0; j<i; j++)
 24             s1[cou++]=s[j];
 25             for(j=len-1; j>=i; j--)
 26             s1[cou++]=s[j];
 27             s1[cou]='\0';
 28             f=0;
 29             iter=mp.begin();
 30             while(iter!=mp.end())
 31             {
 32                 if(iter->first==s1)
 33                 {
 34                     f=1;
 35                     break;
 36                 }
 37                 iter++;
 38             }
 39             if(f==0)
 40             {
 41                 sum++;
 42                 mp[s1]=1;
 43             }
 44             cou=0;
 45             for(j=len-1; j>=i; j--)
 46             s1[cou++]=s[j];
 47             for(j=0; j<i; j++)
 48             s1[cou++]=s[j];
 49             s1[cou]='\0';
 50              f=0;
 51             iter=mp.begin();
 52             while(iter!=mp.end())
 53             {
 54                 if(iter->first==s1)
 55                 {
 56                     f=1;
 57                     break;
 58                 }
 59                 iter++;
 60             }
 61             if(f==0)
 62             {
 63                 sum++;
 64                 mp[s1]=1;
 65             }
 66 
 67             cou=0;
 68             for(j=i-1; j>=0; j--)
 69             s1[cou++]=s[j];
 70             for(j=len-1; j>=i; j--)
 71             s1[cou++]=s[j];
 72             s1[cou]='\0';
 73             f=0;
 74             iter=mp.begin();
 75             while(iter!=mp.end())
 76             {
 77                 if(iter->first==s1)
 78                 {
 79                     f=1;
 80                     break;
 81                 }
 82                 iter++;
 83             }
 84             if(f==0)
 85             {
 86                 sum++;
 87                 mp[s1]=1;
 88             }
 89 
 90             cou=0;
 91             for(j=len-1; j>=i; j--)
 92             s1[cou++]=s[j];
 93             for(j=i-1; j>=0; j--)
 94             s1[cou++]=s[j];
 95             s1[cou]='\0';
 96             f=0;
 97             iter=mp.begin();
 98             while(iter!=mp.end())
 99             {
100                 if(iter->first==s1)
101                 {
102                     f=1;
103                     break;
104                 }
105                 iter++;
106             }
107             if(f==0)
108             {
109                 sum++;
110                 mp[s1]=1;
111             }
112 
113             cou=0;
114             for(j=0; j<i; j++)
115             s1[cou++]=s[j];
116             for(j=i; j<=len-1; j++)
117             s1[cou++]=s[j];
118             s1[cou]='\0';
119              f=0;
120             iter=mp.begin();
121             while(iter!=mp.end())
122             {
123                 if(iter->first==s1)
124                 {
125                     f=1;
126                     break;
127                 }
128                 iter++;
129             }
130             if(f==0)
131             {
132                 sum++;
133                 mp[s1]=1;
134             }
135 
136             cou=0;
137             for(j=i; j<=len-1; j++)
138             s1[cou++]=s[j];
139             for(j=0; j<i; j++)
140             s1[cou++]=s[j];
141             s1[cou]='\0';
142             f=0;
143             iter=mp.begin();
144             while(iter!=mp.end())
145             {
146                 if(iter->first==s1)
147                 {
148                     f=1;
149                     break;
150                 }
151                 iter++;
152             }
153             if(f==0)
154             {
155                 sum++;
156                 mp[s1]=1;
157             }
158             cou=0;
159             for(j=i-1; j>=0; j--)
160             s1[cou++]=s[j];
161             for(j=i; j<=len-1; j++)
162             s1[cou++]=s[j];
163             s1[cou]='\0';
164              f=0;
165             iter=mp.begin();
166             while(iter!=mp.end())
167             {
168                 if(iter->first==s1)
169                 {
170                     f=1;
171                     break;
172                 }
173                 iter++;
174             }
175             if(f==0)
176             {
177                 sum++;
178                 mp[s1]=1;
179             }
180 
181             cou=0;
182             for(j=i; j<=len-1; j++)
183             s1[cou++]=s[j];
184             for(j=i-1; j>=0; j--)
185             s1[cou++]=s[j];
186             s1[cou]='\0';
187              f=0;
188             iter=mp.begin();
189             while(iter!=mp.end())
190             {
191                 if(iter->first==s1)
192                 {
193                     f=1;
194                     break;
195                 }
196                 iter++;
197             }
198             if(f==0)
199             {
200                 sum++;
201                 mp[s1]=1;
202             }
203         }
204         cout<<sum<<endl;
205     }
206     return 0;
207 }

 

posted @ 2013-12-19 13:50  水门  阅读(306)  评论(0编辑  收藏  举报