入门经典——基础数据结构专题(List)

UVA127

链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=63

一道非常好的栈的模拟题,训练计划又往后推了一周,因为各种事情,不过自己已经做了修改,另外说一下,今晚寝室那两尊神不在,难得的心情好,

这题其实还是看了别人的代码的,大牛博客:http://blog.csdn.net/hyczms/article/details/38009937

我的代码:

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<string>
 5 #include<cmath>
 6 #include<vector>
 7 #include<stack>
 8 #include<algorithm>
 9 using namespace std;
10 const int maxn=54;
11 struct Card
12 {
13     char value,suit;
14 };
15 int judge(Card x,Card y)
16 {
17     return (x.value==y.value||x.suit==y.suit);
18 }
19 int main()
20 {
21     stack<Card> s[maxn];
22     Card card;
23     int n=0,i;
24     while(scanf("%c%c", &card.value, &card.suit) && card.value != '#')
25     {
26         getchar();
27         s[n++].push(card);
28         if(n==52)
29         {
30             int m=1,flag;
31             while(true)
32             {
33                 for(i=m;i<n;i++)
34                 {
35                     if(i>=3&&judge(s[i].top(),s[i-3].top()))
36                     {
37                         flag=1;
38                         break;
39                     }
40                     else if(i>=1&&judge(s[i].top(),s[i-1].top()))
41                     {
42                         flag=2;
43                         break;
44                     }
45                 }
46                 if(i==n)
47                     break;
48                 if(flag==1)
49                 {
50                     s[i-3].push(s[i].top());
51                     m=i-3;
52                 }
53                 else
54                 {
55                     s[i-1].push(s[i].top());
56                     m=i-1;
57                 }
58                 s[i].pop();
59                 if(s[i].empty())
60                 {
61                     for(int j=i;j<n-1;j++)
62                         s[j]=s[j+1];
63                     while(!s[n-1].empty())
64                         s[n-1].pop();
65                     n--;
66                 }
67             }
68             if(n>1)
69                 printf("%d piles remaining:",n);
70             else
71                 printf("%d pile remaining:",n);
72             for(int i=0;i<n;i++)
73             {
74                 printf(" %d",s[i].size());
75                 while(!s[i].empty())
76                     s[i].pop();
77             }
78             printf("\n");
79             n=0;
80         }
81     }
82     return 0;
83 }
View Code

开始刷题的第一天,也只是利用晚上复习完考研回寝室的时间做做自己喜欢的事情,希望可以坚持下去

 

UVA673

链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=103&page=show_problem&problem=614

一道栈的经典题目

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<string>
 5 #include<cmath>
 6 #include<vector>
 7 #include<stack>
 8 #include<algorithm>
 9 #include<cctype>
10 #include<cstdlib>
11 using namespace std;
12 const int maxn=128+10;
13 //char s[maxn];
14 int main()
15 {
16     int T;
17     cin>>T;
18     getc(stdin);
19     while(T--)
20     {
21         char s[maxn];
22         gets(s);
23         stack<char> a;
24         int str1=']'-'[';
25         int str2=')'-'(';
26         int n=strlen(s);
27         for(int i=0;i<n;i++)
28         {
29             if(a.empty())
30             {
31                 a.push(s[i]);
32                 continue;
33             }
34             char t=a.top();
35             if(s[i]-t==str1||s[i]-t==str2)
36                 a.pop();
37             else
38                 a.push(s[i]);
39         }
40         if(a.empty())
41             cout<<"Yes"<<endl;
42         else
43             cout<<"No"<<endl;
44     }
45     return 0;
46 }
View Code

 

UVA101

链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=103&page=show_problem&problem=37

这道题目在UVA上面AC率比较低,事实证明也不是一道非常容易的题目,卡了我好久,算是一道恶心的模拟,理解题意就用了好几天,最后也还是参照着题解才过的,不过这类似的题目我一定得多做,对提高代码能力是非常有帮助的。另外就是自己刷题速度实在是太慢了,必须督促自己坚持每天好好刷题,uva虽然略难,但真的是一个靠谱的OJ,不过这题,今晚回宿舍一定要在好好理解一下,总结一下

解题报告来自:http://m.blog.csdn.net/blog/yexiaohhjk/44969131

用了vector,确实大大减少了代码量,也是刘汝佳老师提供的一种写法,和神牛的差距永远那么大

贴一下题目意思:

1.move a onto b,把a和b上面的方块都放回原来位置,然后把a放到b上面;

2.move a over b,把a上面的放回原处,然后把a放在b所在的方块堆的上面;

3.pile a onto b,把b上面的放回原来位置,然后把a和a上面的方块整体放到b上面;

4.pile a over b,把a和a上面的方块整体放到b所在堆的上面。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<string>
 5 #include<cmath>
 6 #include<vector>
 7 #include<cctype>
 8 #include<algorithm>
 9 using namespace std;
10 const int maxn=26;
11 int n;
12 vector <int>pile[maxn];
13 void find_block(int a,int &p,int &h) //找到a在第p堆的第h个
14 {
15     for(p=0;p<n;p++)
16     {
17         for(h=0;h<pile[p].size();h++)
18             if(pile[p][h]==a)
19             return;
20     }
21 }
22 void clear_block(int p,int h) //将a上面的元素放回原处
23 {
24     for(int i=h+1;i<pile[p].size();i++)
25     {
26         int b;
27         b=pile[p][i];
28         pile[b].push_back(b);
29     }
30     pile[p].resize(h+1);
31 }
32 void onto_block(int p,int h,int p2) //将a及上面的元素放在b上
33 {
34     for(int i=h;i<pile[p].size();i++)
35         pile[p2].push_back(pile[p][i]);
36     pile[p].resize(h);
37 }
38 void print()  //打印输出
39 {
40     for(int i=0;i<n;i++)
41     {
42         printf("%d:",i);
43         for(int j=0;j<pile[i].size();j++)
44             printf(" %d",pile[i][j]);
45         printf("\n");
46     }
47 }
48 int main()
49 {
50     cin>>n;
51     for(int i=0;i<n;i++)
52         pile[i].push_back(i);
53     string s1,s2;
54     int a,b;
55     while(cin>>s1)
56     {
57         if(s1=="quit")
58             break;
59         cin>>a>>s2>>b;
60         int pa,pb,ha,hb;
61         find_block(a,pa,ha);
62         find_block(b,pb,hb);
63         if(pa==pb)  continue;
64         if(s2=="onto")
65             clear_block(pb,hb);
66         if(s1=="move")
67             clear_block(pa,ha);
68             onto_block(pa,ha,pb);
69     }
70     print();
71     return 0;
72 }
View Code

 

uva133

纯种的模拟题,约瑟夫环双向模拟,开始被vector坑了,后来发现这题真的不能用vector,同时也对vector有了更深的认识,还好有大牛提点

链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=69

别人的的博客:http://www.cnblogs.com/xiaobaibuhei/archive/2013/04/17/3026383.html

下面贴一下弱菜的代码:

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<string>
 5 #include<cmath>
 6 #include<vector>
 7 #include<algorithm>
 8 using namespace std;
 9 const int maxn=25;
10 int a[maxn];
11 int main()
12 {
13     int n,m,k;
14     while(cin>>n>>m>>k)
15     {
16         if(n==0&&m==0&&k==0)
17             break;
18         for(int i=0;i<n;i++)
19             a[i]=i+1;
20         int p=n-1,q=0;
21         int flag=1;
22         int length=n;
23         while(length)
24         {
25             for(int i=0;i<m;)
26             {
27                 p=(p+1)%n;
28                 if(a[p]) i++;
29             }
30             for(int i=0;i<k;)
31             {
32                 q=(q-1+n)%n;
33                 if(a[q]) i++;
34             }
35             if(flag) flag=0;
36             else printf(",");
37             if(p==q)
38             {
39                 printf("%3d",a[p]);
40                 a[p]=0;
41                 --length;
42             }
43             else
44             {
45                 printf("%3d%3d",a[p],a[q]);
46                 a[p]=a[q]=0;
47                 length-=2;
48             }
49         }
50         cout<<endl;
51     }
52     return 0;
53 }
View Code

 

uva442

这题是用新电脑在自习室做的,吐槽一下,新电脑打代码很难用,这题最值得学习就是类的继承的用法,很经典,这题也是一道很好的栈模拟题,看穿背景以后就是,只需要判断")".而不用管"("

题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=383

我的代码:

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<string>
 5 #include<vector>
 6 #include<stack>
 7 #include<algorithm>
 8 using namespace std;
 9 const int maxn=27;
10 typedef struct Matrix  //工程代码的写法还是值得学习
11 {
12     int a,b;
13     Matrix(int a=0,int b=0):a(a),b(b){};
14 };
15 Matrix p[maxn];
16 int main()
17 {
18     int n;
19     string c;
20     cin>>n;
21     for(int i=0;i<n;i++)
22     {
23         cin>>c;
24         cin>>p[c[0]-'A'].a>>p[c[0]-'A'].b;
25     }
26     string input;
27     stack <Matrix> s;
28     while(cin>>input)
29     {
30         int flag=1,sum=0;
31         for(int i=0;i<input.length();i++)
32         {
33             if(input[i]>='A'&&input[i]<='Z')
34                 s.push(p[input[i]-'A']);
35             else if(input[i]==')')
36             {
37                 Matrix m2=s.top(); s.pop();
38                 Matrix m1=s.top(); s.pop();
39                 if(m1.b!=m2.a)
40                 {
41                     flag=0;
42                     break;
43                 }
44                 sum+=m1.a*m2.a*m2.b;
45                 s.push(Matrix(m1.a,m2.b));
46             }
47         }
48         if(!flag)
49             cout<<"error"<<endl;
50         else
51             cout<<sum<<endl;
52     }
53     return 0;
54 }
View Code

 

uva11111

这道题目跟上面的题目几乎类似,但还是搞了半天,就是普普通通的栈模拟,很经典的一道题,现在越来越喜欢uva上面刘汝佳的题目了,确实都很经典,但自己还是太弱了,智商又不够,只能慢慢积累了

链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2052

参看了一下别人的博客,吐槽一下,开始自己犯了低级错误,把int跟char混用

链接:http://m.blog.csdn.net/blog/cgl1079743846/7757457

我的代码:

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<string>
 5 #include<cmath>
 6 #include<stack>
 7 using namespace std;
 8 const int maxn=100010;
 9 int k[maxn];
10 int cnt,flag;
11 typedef struct toy
12 {
13     int val,res;
14 };
15 stack <toy> s;
16 void work()
17 {
18     while(!s.empty())
19         s.pop();
20     toy temp;
21     temp.val=k[0];
22     temp.res=abs(k[0]);
23     s.push(temp);
24     for(int i=1;i<cnt;i++)
25     {
26         if(s.empty())
27             return;
28         if(k[i]<0)
29         {
30             if(abs(k[i])<s.top().res)
31             {
32                 toy apple;
33                 apple.val=k[i];
34                 apple.res=abs(k[i]);
35                 s.push(apple);
36             }
37             else return;
38         }
39         if(k[i]>0)
40         {
41             if(s.top().val+k[i]==0)
42             {
43                 if(i!=cnt-1)
44                 {
45                     s.pop();
46                     s.top().res-=k[i];
47                 }
48                 else s.pop();
49             }
50             else return;
51         }
52     }
53     if(s.empty())
54         flag=1;
55 }
56 int main()
57 {
58     int x;
59     char c;
60     while(~scanf("%d%c",&x,&c))
61     {
62         k[0]=x;
63         cnt=1;
64         flag=0;
65         if(c!='\n')
66         {
67             while(scanf("%d%c",&k[cnt++],&c))
68             {
69                 if(c=='\n')
70                     break;
71             }
72         }
73         if(cnt%2==0)
74             work();
75             if(flag)
76                 cout<<":-) Matrioshka!"<<endl;
77             else
78                 cout<<":-( Try again."<<endl;
79     }
80     return 0;
81 }
View Code

 

uva540

list专题的最后一题,用了模拟+队列

参考了一下这位大神的解题报告:http://blog.csdn.net/mistkafka/article/details/9472643

我的代码:

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<string>
 5 #include<cmath>
 6 #include<vector>
 7 #include<queue>
 8 using namespace std;
 9 const int maxrank=1000000;
10 const int maxn=1000;
11 int team[maxrank];
12 queue <int> que[maxn];
13 queue <int> bigQue;
14 int main()
15 {
16     int T;
17     int cases=0;
18     while(scanf("%d",&T)!=EOF)
19     {
20         if(T==0)
21             break;
22         while(!bigQue.empty())
23             bigQue.pop();
24         for(int i=0;i<maxn;i++)
25             while(!que[i].empty())
26             que[i].pop();
27         int n;
28         memset(team,0,sizeof(team));
29         for(int cas=0;scanf("%d",&n)==1;cas++)
30         {
31             for(int i=0;i<n;i++)
32             {
33                 int num;
34                 scanf("%d%c",&num);
35                 team[num]=cas;
36             }
37         }
38         printf("Scenario #%d\n",++cases);
39         while(true)
40         {
41             string cmd;
42             cin>>cmd;
43             if(cmd=="ENQUEUE")
44             {
45                 int num;
46                 cin>>num;
47                 if(que[team[num]].empty())
48                     bigQue.push(team[num]);
49                 que[team[num]].push(num);
50             }
51             else if(cmd=="DEQUEUE")
52             {
53                 int app=bigQue.front();
54                 cout<<que[app].front()<<endl;
55                 que[app].pop();
56                 if(que[app].empty())
57                     bigQue.pop();
58             }
59             else if(cmd=="STOP")
60             {
61                 cout<<endl;
62                 break;
63             }
64         }
65     }
66     return 0;
67 }
View Code

接下来开始下一个专题Binary Trees,继续加油,总还是有很多不懂的东西,只有从头开始刷题,才能发现当年在刷一些简单数据结构时自己学得不扎实,以及遇到的各种问题

 

posted @ 2015-06-23 00:34  wolf940509  阅读(246)  评论(0编辑  收藏  举报