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

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 }

猜你喜欢

转载自www.cnblogs.com/Surprisezang/p/8999457.html