码迷,mamicode.com
首页 > 其他好文 > 详细

sort题目

时间:2016-04-06 00:15:03      阅读:236      评论:0      收藏:0      [点我收藏+]

标签:

uva,146

全排列问题:permutation 

具体详解:参考Devymex

UVa Problem 146 - ID Codes

 
Problem:

Please find the problem here.

Solution:

This is simply the next permutation problem. Thanks to the hint on Competitive Programming, there is a simple call in C++ library named next_permutation that can solve the problem, and therefore I used it.

Suppose we are not given the next_permutation code, how would I solve it? First, note that in order to find the least significant digit to increase, but you can‘t just increase a digit because this is a permutation, one also need to change another digit to change as well. Surely we don‘t want to alter the more significant digits, so we can only pick the less significant ones.

Formally, one scan from right to left, trying to find a digit such that there is a larger digit to its right. If such a digit does not exist, we can conclude there is no successor. Otherwise, change that digit to the least larger one found on the right, and sort the rest of the digits, that gives the answer.

For example, the successor of 1, 2, 3, 3 is 1, 3, 2, 3, this is done by noting 2 < 3, so we switch 2 to 3, and just sort [2, 3], which give [2, 3]

As a more exotic scenario, let‘s look at 1, 4, 3, 2, its successor is 2, 1, 3, 4, this is done by noting 1 < 2, so we switch 1 to 2, and just sort [1, 4, 3], which gives [1, 3, 4].
 
贴一下自己写的代码:
总是wrong answer不知道怎么的:到时候再来填坑。
技术分享
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <vector>
 5 
 6 using namespace std;
 7 
 8 int main()
 9 {
10     vector<char> s;
11     while(!cin.eof())
12     {
13         char c=cin.get();
14         if(c!=-1)
15         {
16             if(c!=\r && c!=\n)
17             {
18                 if(c==#)
19                     break;
20                 s.push_back(c);
21             }
22             else
23             {
24                 vector<char>::iterator it=s.end(),at;
25                 for(--it;it!=s.begin();--it)
26                 {
27                     if(*(it-1)<*it)
28                         break;
29                 }
30                 if(it==s.begin())
31                     printf("No Successor\n");
32                 else
33                 {
34                     it--;
35                     for(at=it+1;at!=s.end();at++)
36                         if(*at<*it)
37                             break;
38                     iter_swap(it,--at);
39                     reverse(it+1,s.end());
40                     for(vector<char>::iterator i=s.begin();i!=s.end();i++)
41                         cout<<*i;
42                     cout<<endl;
43                 }
44                 s.clear();
45                 
46             }
47         }
48     }
49     return 0;
50 }
View Code

accept代码:Andrew

技术分享
 1 #include <iostream>
 2 #include <vector>
 3 #include <algorithm>
 4 
 5 using namespace std;
 6 
 7 int main()
 8 {
 9     vector<char> line;
10     while (!cin.eof())
11     {   
12         char c = cin.get();
13         if (c != -1)
14         {
15             if (c != \r && c != \n)
16             {
17                 if (c == #)
18                 {
19                     break;
20                 }
21                 line.push_back(c);
22             }
23             else
24             {
25                 if (next_permutation(line.begin(), line.end()))
26                 {
27                     for (vector<char>::iterator i = line.begin(); i != line.end(); i++)
28                     {
29                         cout << *i;
30                     }
31                     cout << endl;
32                 }
33                 else
34                 {
35                     cout << "No Successor" << endl;
36                 }
37                 line.clear();
38             }
39         }
40     }
41 
42     return 0;
43 }
View Code

uva,299

技术分享
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 
 5 using namespace std;
 6 const int maxn=55;
 7 
 8 int main()
 9 {
10     int N;
11     int L;
12     int a[maxn];
13     scanf("%d",&N);
14     while(N--)
15     {
16         scanf("%d",&L);
17         for(int i=0;i<L;i++)
18             scanf("%d",&a[i]);
19         int count=0;
20         for(int i=0;i<L;i++)
21             for(int j=1;j<L-i;j++)
22             {
23                 if(a[j]<a[j-1])
24                 {
25                     swap(a[j],a[j-1]);
26                     count++;
27                 }
28             }
29         printf("Optimal train swapping takes %d swaps.\n",count);
30     }
31     return 0;
32 }
View Code

======

用结构体来模拟map很舒服。

uva,10008

技术分享
 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cstdio>
 4 #include <cstring>
 5 
 6 using namespace std;
 7 
 8 struct P
 9 {
10     int times;
11     char ch;
12 };
13 
14 int comp(const P x,const P y)
15 {
16     if(x.times==y.times)
17         return x.ch<y.ch;
18     return x.times>y.times;
19 }
20 int main()
21 {
22     int n;
23     char ch,s[10000];
24     P a[27];
25     for(int i=0;i<26;i++)
26     {
27         a[i].times=0;
28         a[i].ch=i+65;
29     }
30     scanf("%d",&n);
31     while(n--)
32     {
33         cin.getline(s,10000);
34         for(int i=0;i<strlen(s);i++)
35         {
36             if(isalpha(s[i]))
37             {
38                 if(islower(s[i])) s[i]-=32;
39                 a[(int)s[i]-65].times++;
40             }
41         }
42     }
43     sort(a,a+26,comp);
44     for(int i=0;i<26,a[i].times>0;i++)
45         cout<<a[i].ch<<" "<<a[i].times<<endl;
46     
47 }
View Code

 

sort题目

标签:

原文地址:http://www.cnblogs.com/do-it-best/p/5357507.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!