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

poj3617 Best Cow Line(贪心,字典序问题)

时间:2018-05-06 20:38:02      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:using   else   nbsp   ace   space   style   names   cin   int   

https://vjudge.net/problem/POJ-3617

这类字符串处理字典序问题经常用到贪心,

每决定输出一个字符之前,都要前后i++,j--逐个比大小,直至比出为止。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<queue>
 4 #include<cstring>
 5 #include<algorithm>
 6 #include<cmath>
 7 #include<set>
 8 #define INF 0x3f3f3f3f
 9 typedef long long ll;
10 using namespace std;
11 char a[2010];
12 int n;
13 int main()
14 {
15     cin >> n;
16     for(int i = 0; i < n; i++){
17         cin >> a[i];
18     }
19     int st=0, end=n-1;
20     for(int i = 0; i < n; i++){
21         int t=0;
22         while(st+t<n&&end-t>=0&&(a[st+t] == a[end-t])){//一直到比出大小为止
23             t++;
24         }
25         if(a[st+t] > a[end-t]){//取后 
26             cout << a[end]; 
27             end--;
28         }
29         else if(a[st+t] < a[end-t]){
30             cout << a[st];
31             st++;
32         }
33         if((i+1)%80==0) cout << endl;
34     }
35     return 0;
36 }

 

poj3617 Best Cow Line(贪心,字典序问题)

标签:using   else   nbsp   ace   space   style   names   cin   int   

原文地址:https://www.cnblogs.com/Surprisezang/p/8999457.html

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