symons

___________每一天都是幸福的!!

  博客园  ::  :: 新随笔  ::  :: 订阅 订阅  :: 管理

这题是最长子序列,然后再求出路径就可以了。开始写的比较乱,用数组什么的,后来用了指针就好办了。现在把代码贴出来。指针真的挺给力的。

--------------------------------------------------------------------------------------------------------------------

 1 #include <iostream>
 2 #include <stack>
 3 #include <cmath>
 4 using namespace std;
 5 struct people
 6 {
 7     int num,h;
 8     people* last;
 9 }girl[100+5];
10 int main()
11 {
12     int n;
13     int i,j;
14     int dp[100+10];
15     stack<int>haha;
16     while(cin>>n&&n)
17     {
18      for(i=1;i<=n;++i)
19       dp[i]=1;
20      for(i=1;i<=n;++i)
21      {
22       cin>>girl[i].h;
23       girl[i].num=i;
24       girl[i].last=NULL;
25      }
26      for(i=1;i<=n;++i)
27      {
28          for(j=1;j<i;++j)
29          {
30              if(girl[i].h>girl[j].h) 
31              {
32                  if(dp[j]+1>dp[i]) 
33                  {
34                      dp[i]=dp[j]+1;
35                      girl[i].last=&girl[j];
36 
37                  }
38              }
39          }
40      }
41      int max=0,maxsign=0;
42      for(i=1;i<=n;++i)
43      {
44          if(dp[i]>max){ max=dp[i];maxsign=i;}
45      }
46      cout<<"The number is ";
47      cout<<max;
48      cout<<":";
49      haha.push(girl[maxsign].num);
50      people* temple=girl[maxsign].last;
51      while(temple)
52      {
53             haha.push(temple->num); 
54             temple=temple->last;
55      }
56      while(haha.size()>0)
57      {     
58          cout<<" "<<haha.top();haha.pop();}
59          cout<<endl;
60     }
61     return 0;
62 }
63 
64 
65 
66 
67          

 

posted on 2012-11-18 17:30  symons  阅读(203)  评论(0编辑  收藏  举报