1 solutions

  • 0
    @ 2024-12-10 22:27:30

    C++ :

    
    #include<bits/stdc++.h>
    using namespace std;
    int n,s;
    char a[2005]; 
    int main()
    {        
        cin>>n;
        for(int i=1;i<=n;i++)
        	cin>>a[i];
        for(int i=1,j=n;i<=j;)
    	{
    		if(a[i]>a[j]) cout<<a[j--];//小的先输出
    		else if(a[i]<a[j]) cout<<a[i++];
    		else//相等的情况下找下一个小的数
    		{
    			int l=i+1,r=j-1;
    			while(a[l]==a[r]) l++,r--;//相等就找下一个小的数
    			if(a[l]>a[r]) cout<<a[j--];
    			else   cout<<a[i++];
    		}
            s++;
            if(s%80==0) cout<<endl;
    	} 
    	return 0;
    }
    

    Pascal :

    var l,r,i,j,k,n,m,p:Longint;
        s1,s2,s:array[0..2000] of  char ;
        oL:boolean;
        tl,tr:longint;
    begin
     readln(n);
     for i:=1 to n do
     begin
      read(s[i]);
      readln;
     end;
     p:=1;
     l:=1;
     r:=n;
     while p<=n do
      begin
        tl:=l;
        tr:=r;
        while s[tl]=s[tr] do
         begin
          inc(tl);
          dec(tr);
         end;
        if ord(s[tl])<ord(s[tr]) then
         begin
          s1[p]:=s[l];
          inc(l);
         end else
         begin
          s1[p]:=s[r];
          dec(r);
         end;
      inc(p);
      end;
    for i:=1 to n do
    begin
    
     write(s1[i]);
     if i mod 80=0 then writeln;
     end; 
    end.
    
    
    
    
    
    
    • 1

    Information

    ID
    544
    Time
    5000ms
    Memory
    256MiB
    Difficulty
    (None)
    Tags
    # Submissions
    0
    Accepted
    0
    Uploaded By