乐逍遥xwl

导航

POJ 3617 字典序最小问题

 
 

Description

FJ is about to take his N (1 ≤ N ≤ 2,000) cows to the annual"Farmer of the Year" competition. In this contest every farmer arranges his cows in a line and herds them past the judges.

The contest organizers adopted a new registration scheme this year: simply register the initial letter of every cow in the order they will appear (i.e., If FJ takes Bessie, Sylvia, and Dora in that order he just registers BSD). After the registration phase ends, every group is judged in increasing lexicographic order according to the string of the initials of the cows' names.

FJ is very busy this year and has to hurry back to his farm, so he wants to be judged as early as possible. He decides to rearrange his cows, who have already lined up, before registering them.

FJ marks a location for a new line of the competing cows. He then proceeds to marshal the cows from the old line to the new one by repeatedly sending either the first or last cow in the (remainder of the) original line to the end of the new line. When he's finished, FJ takes his cows for registration in this new order.

Given the initial order of his cows, determine the least lexicographic string of initials he can make this way.

Input

* Line 1: A single integer: N
* Lines 2..N+1: Line i+1 contains a single initial ('A'..'Z') of the cow in the ith position in the original line

Output

The least lexicographic string he can make. Every line (except perhaps the last one) contains the initials of 80 cows ('A'..'Z') in the new line.

Sample Input

6
A
C
D
B
C
B

Sample Output

ABCBCD



题意:这题意思就是给你一个字符串,让你每次从首尾取出一个字符组成一个新的字符串,要使最后组成的字符串字典序最小。

思路:我的写法就是每次先比较字符串首尾字符,哪个小就加哪个,并且在原字符串中把这个字符删了,
但是如果首尾字符相同,显然要组成最小字典序的话还需要继续往里比较字符大小,这里只需要比较这个字符串与反转之后的字符串大小就行,
另外有个地方要注意,输出的时候每80个字符需要换行......

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cmath>
 5 #include<algorithm>
 6 #include<map>
 7 #include<set>
 8 #include<vector>
 9 using namespace std;
10 #define ll long long
11 const int inf=99999999;
12 const int mod=1e9+7;
13 int main()
14 {
15        ios::sync_with_stdio(false);
16        int n;
17        cin>>n;
18     string str="";
19     char ch;
20     for(int i=0;i<n;i++)
21     {
22         cin>>ch;
23         str+=ch;
24      }
25      //cout<<str<<endl;
26      string ans="";
27      string temp;
28      while(!str.empty())
29      {
30          if(str[0]>str[str.size()-1])//首和尾进行比较 
31          {
32              ans+=str[str.size()-1];// 尾字符小 
33              str.erase(str.size()-1,1);//删去 
34          }
35         else if(str[0]<str[str.size()-1])
36         {
37             ans+=str[0];//首字符小 
38             str.erase(0,1);//删去 
39         }
40         else if(str[0]==str[str.size()-1])//如果相等 
41         {
42             temp=str;//存str 
43             reverse(str.begin(),str.end());//逆置再比较 
44             if(str>temp)//str前面的部分大 
45             {
46                 ans+=str[str.size()-1];//加尾字符 
47                 str.erase(str.size()-1,1);//删去 
48             }
49             else//否则删首字符 
50             {
51                 ans+=str[0];
52                 str.erase(0,1);
53             }
54         }
55      }
56      for(int i=0;i<ans.size();i++)
57      {
58          cout<<ans[i];
59          if((i+1)%80==0)//格式输出 
60              cout<<endl;
61      }
62     return 0;
63 }


本来开始的写法是用的string的成员函数,写起来会简单一点,但是在POJ上编译过不去,可能是我太菜了......

 

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cmath>
 5 #include<algorithm>
 6 #include<map>
 7 #include<set>
 8 #include<vector>
 9 using namespace std;
10 #define ll long long
11 const int inf=99999999;
12 const int mod=1e9+7;
13 int main()
14 {
15        ios::sync_with_stdio(false);
16        int n;
17        cin>>n;
18     string str="";
19     char ch;
20     for(int i=0;i<n;i++)
21     {
22         cin>>ch;
23         str+=ch;
24      }
25      //cout<<str<<endl;
26      string ans="";
27      string temp;
28      while(!str.empty())
29      {
30          if(str.front()>str.back())
31          {
32              ans+=str.back(); 
33              str.pop_back(); 
34          }
35         else if(str.front()<str.back())
36         {
37             ans+=str.front(); 
38             str.erase(0,1);
39         }
40         else if(str.front()==str.back())
41         {
42             temp=str; 
43             reverse(str.begin(),str.end());
44             if(str>temp) 
45             {
46                 ans+=str.back(); 
47                 str.pop_back();
48             }
49             else
50             {
51                 ans+=str.front();
52                 str.erase(0,1);
53             }
54         }
55      }
56      for(int i=0;i<ans.size();i++)
57      {
58          cout<<ans[i];
59          if((i+1)%80==0)
60              cout<<endl;
61      }
62     return 0;
63 }


posted on 2019-05-11 12:00  乐逍遥xwl  阅读(212)  评论(0编辑  收藏  举报