Codeforces 1382C2 前缀置换
xg了半天才发现规律
题意
给两个长度为n的字符串a,b。最多不操作2n次。使得a等于b。
操作为:选择任意一个下标pos,置换pos的前缀字符串,然后让1等于0,0等于1。
思路
比如10011001111。可以从左往右找到a【i】!=a【i+1】的位置,然后进行操作。使得字符串a变为全为1或者全为1或者全为0的字符串。
全为0,全为1的字符串同样也可以这样操作变成b,不过需要从右往左。
#include <iostream> #include <cmath> #include <cstdio> #include <cstring> #include <string> #include <map> #include <iomanip> #include <algorithm> #include <queue> #include <stack> #include <set> #include <vector> // #include <bits/stdc++.h> #define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); #define sp ' ' #define endl '\n' #define inf 0x3f3f3f3f; #define FOR(i,a,b) for( int i = a;i <= b;++i) #define bug cout<<"--------------"<<endl #define P pair<int, int> #define fi first #define se second #define pb(x) push_back(x) #define ppb() pop_back() #define mp(a,b) make_pair(a,b) #define ms(v,x) memset(v,x,sizeof(v)) #define rep(i,a,b) for(int i=a;i<=b;i++) #define repd(i,a,b) for(int i=a;i>=b;i--) #define sca3(a,b,c) scanf("%d %d %d",&(a),&(b),&(c)) #define sca2(a,b) scanf("%d %d",&(a),&(b)) #define sca(a) scanf("%d",&(a)); #define sca3ll(a,b,c) scanf("%lld %lld %lld",&(a),&(b),&(c)) #define sca2ll(a,b) scanf("%lld %lld",&(a),&(b)) #define scall(a) scanf("%lld",&(a)); using namespace std; typedef long long ll; ll gcd(ll a,ll b){return b?gcd(b,a%b):a;} ll lcm(ll a,ll b){return a/gcd(a,b)*b;} ll powmod(ll a, ll b, ll mod){ll sum = 1;while (b) {if (b & 1) {sum = (sum * a) % mod;b--;}b /= 2;a = a * a % mod;}return sum;} const double Pi = acos(-1.0); const double epsilon = Pi/180.0; const int maxn = 1e5+10; char a[maxn],b[maxn]; char c[maxn]; int n; int main() { //freopen("input.txt", "r", stdin); int _; scanf("%d",&_); while(_--) { cin>>n; rep(i,1,n) cin>>a[i]; rep(i,1,n){ cin>>b[i]; // c[i] = b[i]; } //reverse(b+1,b+1+n); queue<int>ans; rep(i,2,n){ if(a[i] != a[i-1]){ ans.push(i-1); } } if(a[n]!=b[n]){ ans.push(n); } repd(i,n-1,1){ if(b[i]!=b[i+1]){ ans.push(i); } } //ans.push(n); cout<<ans.size()<<sp; while(ans.size()){ int x; x = ans.front(); ans.pop(); cout<<x<<sp; } cout<<endl; } }