POJ 3617 Best Cow Line

传送门:http://poj.org/problem?id=3617

主要考虑两个方面:

1.在给定的N个字符,如果字符的开头和末尾的字符相同怎么删除

2.在输出时每80个字符输出一个换行符。

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <iostream>
 4 using namespace std;
 5 
 6 const int MAXN=2000+5;
 7 char s[MAXN],t[MAXN];
 8 
 9 void solve(int N){
10     int left=0,right=N-1,j=0;
11     while(left<=right){
12         int flag=0;
13         for(int i=0;i+left<=right;i++){
14             if(s[left+i]<s[right-i]){
15                 flag=1;
16                 break;
17             }else if(s[left+i]>s[right-i]){
18                 flag=0;
19                 break;
20             }
21         }
22         if(flag)
23             t[j++]=s[left++];
24         else
25             t[j++]=s[right--];
26     }
27     for(int i=0;i<N;i++){
28         if(i!=0&&i%80==0)
29             cout<<endl;
30         cout<<t[i];
31     }
32     cout<<endl;
33 }
34 
35 
36 
37 
38 int main(){
39     int N;
40     while(cin>>N){
41         for(int i=0;i<N;i++)
42             scanf(" %c",&s[i]);
43         solve(N);
44     }
45 }

 

posted on 2017-01-25 16:24  mkfoy  阅读(209)  评论(0编辑  收藏  举报

导航