百度2015春季實習生招聘附加題_01排序,2015_01

來源:互聯網
上載者:User

百度2015春季實習生招聘附加題_01排序,2015_01
給定一個01串(僅由‘ 0’或‘1’組成的字串),現在想把這個數字串排序成“非遞減”有序序列,請問至少需要多少次交換(任意兩個位置交換)? 
輸入描述:


輸入資料第一行是一個正整數T(T<=100),表示有T組測試資料;
接下來的T行,每行給出01串。
資料保證——
50%的字串長度在[1,100 ]
95%的字串長度在[1,10000]
100%的字串長度在[1,1000000]




輸出描述:


對於每組測試資料,請輸出排成“非遞減有序序列”的最小交換次數。
每組輸出佔一行。


輸入例子:


3
01
10
110


輸出例子:


0
1

1


這道就比較水了。。。。

#include <iostream>#include <cstring>#include <string>using namespace std;void swap(char* lhs, char* rhs){char temp=*lhs;*lhs=*rhs;*rhs=temp;}int main(){int T,len,l,r,i,j;string s;cin>>T;while(T--){cin>>s;len=s.length();j=0;l=0;r=len-1;while(l<r){while('0'==s[l])l++;while('1'==s[r])r--;if(l<r){++j;swap(&s[l],&s[r]);}}cout<<j<<endl;}return 0;}




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.