string操作

来源:互联网 发布:如何连接网络打印机 编辑:程序博客网 时间:2024/04/27 16:43

题目描述:把一个字符串只含有0和1,求把这个串排成非递减次序的串的最少的步数

输入:

4

01

10

110

11010

输出:

0

1

1

2

#include<iostream>#include<algorithm>#include<string>using namespace std;int main(){int n;cin >> n;while (n--){string tmp;cin >> tmp;int s = 0;int cnt = 0;int e = tmp.length() - 1;while (s < e){while (tmp[s] == '0'&&s<e)s++;while (tmp[e] == '1'&&s < e)e--;if (s < e){cnt++;char c = tmp[s];tmp[s] = tmp[e];tmp[e] = c;s++;e--;}}cout << cnt << endl;}return 0;}



原创粉丝点击