输入数据第一行是一个正整数T(T<=100),表示有T组测试数据;
接下来的T行,每行给出01串。
数据保证——
50%的字符串长度在[1,100 ]
95%的字符串长度在[1,10000]
100%的字符串长度在[1,1000000]
对于每组测试数据,请输出排成“非递减有序序列”的最小交换次数。
每组输出占一行。
3<br/>01<br/>10<br/>110
0<br/>1<br/>1
void timesofexchange(string &s)
{
int numberof0 = 0;
int count=0;
int cycle = 0;
for (int i = 0; i <s.size(); ++i)
{
if (s[i] == '0')
{
++numberof0;
}
}
while (cycle < s.size())
{
if (cycle <= numberof0-1)
{
if (s[cycle] != '0')
{
count++;
}
++cycle;
}
else
{
if (s[cycle] != '1')
{
count++;
}
++cycle;
}
}
count = count / 2;
cout << count << endl;
}
我并没有用什么排序,只是做了个求与预算。
思路很简单,举个例子大家就知道了
比如:1001001
求有多少个0,最后得到个字符串0000111
每一位与原字符串相与,共有四位不同,4/2=2
所以最少交换三次
#include "stdio.h" #include "stdlib.h" #include "string.h" int lingyipaixu( char * str ) { int i=0; int jiaohuanshu=0; int n=strlen(str); int j=n-1; while (j>i) { if(str[i]=='0') { i++; if(str[j] == '1') j--; } else { if(str[j]=='1') j--; else { str[j]='1'; str[i]='0'; jiaohuanshu++; j--; i++; } } } return jiaohuanshu; } int main() { char str[1000]; scanf("%s",str); printf("%d\n",lingyipaixu(str)); }
#include <iostream> #include <vector> #include <algorithm> #include <climits> #include <string> using namespace std; int swapMin(string str) { int res = 0; int length = str.size(); int index = length; //每次交换,将1和最后的0位置交换 for (int i = 0; i < length; i++) { if (str[i] == '1') { for (index = index; index > i; index--) { if (str[index] == '0') { res++; index = index - 1; break; } } } } return res; } int main() { int counts; string str; while (cin >> counts) { for (int i = 0; i < counts; i++) { cin >> str; cout << swapMin(str) << endl; } } return 0; }
#include <iostream>#include <vector>intmain(){using namespace std;intn = 0;while(cin >> n){vector<int> num;for(inti = 0; i < n; i ++){intcount = 0;string str;cin >> str;intret = 0;for(inti = 0; i < str.size(); i ++){if(str[i] == '0')ret ++;}intnum_zero = 0;for(inti = 0; i < ret; i ++){if(str[i] == '0')num_zero ++;}count = ret - num_zero;num.push_back(count);}for(inti = 0; i < num.size(); i ++)cout << num[i] << endl;}return0;}
import java.util.*; public class Main{ private static Scanner sc=new Scanner (System.in); public static void main(String[] args){ sc.nextLine(); while(sc.hasNext()){ StringBuilder sb=new StringBuilder (sc.nextLine()); int n=sb.length(); int j=0; int k=0; for(int i=0;i<n;i++){ if(sb.charAt(i)=='0')j++; } for(int i=0;i<j;i++){ if(sb.charAt(i)=='1')k++; } System.out.println(k); } } }
#include <iostream>#include <string>using namespace std;intmain(){intT;cin >> T;while(T--){string str;cin >> str;intstart = 0;intend = str.size() - 1;intcount = 0;while(start < end){while(str[start] == '0') start++;while(str[end] == '1') end--;//swapif(start < end){count++;start++;end--;}}cout << count << endl;}return0;}
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <algorithm>
#include <string.h>
using namespace std;
char a[1000000];
char b[1000000];
int main(){
int n;
scanf("%d",&n);
for(int time=0;time<n;time++)
{
int sum =0;
cin>>a;
memcpy(b, a, strlen(a));
int start=0;
long end = strlen(a);
sort(b, b+end);
while(start<end)
{
sum += a[start]^b[start];
start++;
}
printf("%d\n",sum/2);
}
return 0;
}
#include<iostream> #include<string.h> using namespace std; char data[1000005]; int main() { int T,len; cin >> T; int *changeNums = new int[T]; for (int i = 0; i < T; i++) changeNums[i] = 0; for (int i = 0; i < T; i++) { cin >> data; len = strlen(data); char *pFist = data; char *pLast = data + len - 1; while (pFist < pLast) { if (*pFist == '0') { pFist++; continue; } if (*pLast == '1') { pLast--; continue; } changeNums[i]++; pFist++; pLast--; } } for (int i = 0; i < T; i++) cout << changeNums[i] << endl; delete[]changeNums; return 0; }
import java.util.Arrays; import java.util.Scanner; public class Main{ public static void main(String[] args) { Scanner cin = new Scanner(System.in); int t = cin.nextInt();// t组测试数据 for (int i = 0; i < t; i++) { String s = cin.next(); int count = getBinaryStr(s); System.out.println(count); } } private static int getBinaryStr(String s) { char[] ch = s.toCharArray(); int low = 0; int high = ch.length - 1; int count = 0; while (low < high) { while (high > 0 && ch[high] == '1') { high--; } while (low < ch.length - 1 && ch[low] == '0') { low++; } if (low < high) { char temp = ch[low]; ch[low] = ch[high]; ch[high] = temp; count++; } } return count; } }
//solution : two pointer #include <iostream> #include <vector> #include <string> #include <algorithm> using namespace std; int getSwapNumber(string& s){ int lo = 0; int hi = s.size() - 1; int count = 0; while(lo < hi){ while(s[lo] == '0' && lo < hi)++lo; while(s[hi] == '1' && hi > lo)--hi; if(lo < hi){ swap(s[lo++],s[hi--]); ++count; } } return count; } int main(){ string s; int n; cin>>n; while(n--) { cin>>s; cout<<getSwapNumber(s)<<endl; cout<<s<<endl; } return 0; }
#include <iostream> #include <string> using namespace std; int main() { int T; cin >> T; while(T--) { int swapCount = 0; string lineStr; cin >> lineStr; int low = 0; int high = lineStr.length() - 1; while(low < high) { while(low < high && lineStr.at(low) == '0') { ++low; } while(low < high && lineStr.at(high) == '1') { --high; } if(low != high) { ++swapCount; ++low; --high; } } cout << swapCount << endl; } return 0; }
在VS里运行0011100得到的结果确实是2,但是提交程序的时候一直说该用例未通过,不懂哪里有问题: #include <iostream> #include <string> using namespace std; int count_change(string str) { int count=0; int i=0,j=str.size()-1; while(i<j) { while(str[i]=='0') i++; while (str[j]=='1') j--; if(i<j) { count++; i++; j--; } } return count; } int main( ) { string str; int T; cin>> T; while(T--) { getline(cin,str); cout<<count_change(str)<<endl;} return 0; }
}
#include<iostream> #include<string> using namespace std; int main(){ int T; cin >> T; while (T--){ string a; cin >> a; int len, i; len = a.length(); int count = 0; int number = 0; for (i = 0; i<len; i++) if (a[i] == '0') count++; char *arr = new char[len]; for (i = 0; i<len; i++) arr[i] = '1'; for (i = 0; i<count; i++) arr[i] = '0'; for (i = 0; i < len; i++) number += a[i] ^ arr[i]; cout << number / 2 << endl; } return 0; }