当前位置:网站首页>Mark and lightbulbs (thinking)
Mark and lightbulbs (thinking)
2022-07-26 01:48:00 【to cling】
Codeforces Round #807 (Div. 2)
Problem
Give two lengths of n Of 01 character string s and t.s You can do the following :
- Select a subscript i i i, Satisfy : s [ i − 1 ] ≠ s [ i + 1 ] s[i - 1] \neq s[i + 1] s[i−1]=s[i+1]
- send s [ i ] = s [ i ] ⊕ 1 s[i] = s[i] \oplus 1 s[i]=s[i]⊕1
ask : How many times is the minimum required for this operation , To put s become t?
Solution
This question is quite observant in the postgraduate entrance examination .
- The length of the operation sequence in the title is 3, All in all 4 Kind of :001,011,100,110
001 → 011 , 011 → 001 , 100 → 110 , 110 → 100 001 \to 011,011 \to 001, 100 \to 110, 110 \to 100 001→011,011→001,100→110,110→100
This operation can be described as : Select three consecutive characters with different beginning and end : Reverse the middle position . - These consecutive characters , The middle position is either the same as the left , Or the same as the right .
therefore , The above four operations can be summed up into two :
s = s 1 s 2 s 3 s = s_1s_2s_3 s=s1s2s3
s 1 = s 2 , s 2 ≠ s 3 → s 1 ≠ s 2 , s 2 = s 3 s_1 = s_2, s_2 \neq s_3 \to s_1 \neq s_2, s_2 = s_3 s1=s2,s2=s3→s1=s2,s2=s3
s 1 ≠ s 2 , s 2 = s 3 → s 1 = s 2 , s 2 ≠ s 3 s_1 \neq s_2, s_2 = s_3 \to s_1 = s_2, s_2 \neq s_3 s1=s2,s2=s3→s1=s2,s2=s3
After the familiar XOR operation is :
01 → 10 01 \to 10 01→10
10 → 01 10 \to 01 10→01 - At this time, it suddenly became clear... DIU DIU
- Make a = ( s 1 ⊕ s 2 ) ( s 2 ⊕ s 3 ) . . . ( s n − 1 ⊕ s n ) Make a = (s_1 \oplus s_2)(s_2 \oplus s_3)...(s_{n-1} \oplus s_{n}) Make a=(s1⊕s2)(s2⊕s3)...(sn−1⊕sn), t Empathy , Assuming that b
for instance :
s = 000101 → a = 00111 s = 000101 \to a = 00111 s=000101→a=00111
t = 010011 → b = 11010 t =010011 \to b = 11010 t=010011→b=11010
Now the problem turns into : Use the above two operations ( 01 → 10 01 \to 10 01→10, 10 → 01 10 \to 01 10→01) take a Turn into b, The minimum number of operations required .
Then the result is obvious : The minimum operand is to a Corresponding 1 Move to b The sum of all operations at the corresponding position in .
Simply put : All the corresponding 1 The sum of the differences of subscripts of is the result .( If you don't understand, you can read the code ) - There is no solution :
- a and b in 1 The number of different
- s [ 0 ] ≠ t [ 0 ] s[0] \neq t[0] s[0]=t[0]
- s [ n − 1 ] ≠ t [ n − 1 ] s[n-1] \neq t[n-1] s[n−1]=t[n−1]
Code
const int N = 2e5 + 5, M = 1e6 + 7;
int a[N], b[N];
int main()
{
IOS;
int T; cin >> T;
while (T--)
{
int n; cin >> n;
string s, t; cin >> s >> t;
if (s[0] != t[0] || s[n - 1] != t[n - 1])
{
cout << "-1\n";
continue;
}
int cnt1 = 0, cnt2 = 0;
for (int i = 1; i <= n - 1; i++)
{
a[i] = s[i] != s[i - 1];
b[i] = t[i] != t[i - 1];
cnt1 += a[i];
cnt2 += b[i];
}
if (cnt1 != cnt2)
{
cout << "-1\n";
continue;
}
ll ans = 0;
int r = 1;
for (int i = 1; i <= n - 1; i++)
if (a[i])
{
while (r <= n - 2 && b[r] == 0) r++;
ans += abs(i - r);
r++;
}
cout << ans << endl;
}
return 0;
}
边栏推荐
- 01. MySQL transaction isolation level and concurrent database access
- Test questions and answers of the latest Beijing Construction eight (materialman) mock examination in 2022
- [Verilog digital system design (Xia Yuwen) 3 ----- basic concepts of Verilog syntax 1]
- How idea can quickly delete recently opened projects
- "Weilai Cup" 2022 Niuke summer multi school training camp 2 i.[let fat tension] matrix multiplication j.[link with arithmetic progression] linear regression
- flink sql 如何配置打印insert实参日志呢
- 4QAM, 16QAM modulation and demodulation simulation circuit, observe and analyze QAM constellation and bit error rate curve [matlab code]
- Easyrecovery15 data recovery software with high recovery rate and high download volume
- npm ERR! code ETIMEDOUTnpm ERR! syscall connectnpm ERR! errno ETIMEDOUTnpm ERR! network request t
- QTreeWidget虚线设置
猜你喜欢

C语言中的整型数据类型(你真的了解吗)

Prime Ring Problem

PtGui pro12 vertical line correction
![[Verilog digital system design (Xia Yuwen) 4 ----- basic concepts of Verilog syntax 2]](/img/fe/746ecaf4123072cca59d7510e9796c.png)
[Verilog digital system design (Xia Yuwen) 4 ----- basic concepts of Verilog syntax 2]

销量连连夺冠,五菱的成功秘诀只有低价吗?

U++ learning notes ustruct, uenum declaration and function library simple function implementation

zeromq浅析

The detailed knowledge summary of MySQL can be collected

Integer data type in C language (do you really understand it)
SQL injection tutorial: learn through examples
随机推荐
给RestTemplate添加拦截器记录请求响应,还需解决流只读一次的问题
There is no setter method in grpc list under flutter. How to use related attributes
保护系统日志服务器和设备
Shell exercises
npm ERR! code ETIMEDOUTnpm ERR! syscall connectnpm ERR! errno ETIMEDOUTnpm ERR! network request t
The e-commerce project is written in the resume. How to answer it during the interview
Is it safe to open an account for stock speculation through the online account manager?
大佬们, flinksql datahub源表,源表有字段 timestamp 16位, 写入Ora
"Weilai Cup" 2022 Niuke summer multi school training camp 2 k.[link with bracket sequence i] bracket sequence DP
Prime Ring Problem
Typora expiration solution, what if typora can't open
【独立站建设】shopify卖家:学会这几点,网上商店销量翻倍!
Understand Linglong platform unified access service from simple to deep Monet
Recommend a super good UI automation tool: uiautomator2!
达梦数据库表导入导出按钮灰色,导入不了dmp文件
How uxdb works on multiple processors
"Weilai Cup" 2022 Niuke summer multi school training camp 2 g.[link with monotonic subsequence] block structure
Summary after reading "poor dad and rich dad"
SVN version control branch and merge function use
【深入浅出玩转FPGA学习11----Testbench书写技巧2】