cf777D(贪心&&c_str()函数)

题目链接:http://codeforces.com/contest/777/problem/D

 

题意:给出n行以#开头的字符串,从原字符串尾部删除尽量少的字符串,使其为非降序排列。

 

思路:我们可以从最后一个字符串着手,对其前面的字符串进行删除操作,使其不大于前者。遍历到第一个字符串时就得到了我们所需要的字符串啦。

 

代码:

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 const int MAXN=5e5+10;
 5 string gg[MAXN];
 6 char cc[MAXN];
 7 
 8 int main(void){
 9     // ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
10     int n;
11     cin >> n;
12     for(int i=0; i<n; i++){
13         scanf("%s", cc);
14         gg[i]=cc;
15     }
16     for(int i=n-2; i>=0; i--){
17         for(int j=0; gg[i][j]; j++){
18             if(gg[i][j]>gg[i+1][j]){
19                 gg[i][j]='\0';
20             }else if(gg[i][j]<gg[i+1][j]){
21                 break;
22             }
23         }
24     }
25     for(int i=0; i<n; i++){
26         // cout << gg[i].c_str() << endl;
27         printf("%s\n", gg[i].c_str()); //c_str():生成一个const char*指针,指向以空字符终止的数组。
28     }
29     return 0;
30 }

 

咋一看好像会超时,不过本题中并没有给出那种很极端的数据

posted @ 2017-02-24 21:41  geloutingyu  阅读(297)  评论(0编辑  收藏  举报