B. Equidistant String

小苏伊喜欢处理由零和一组成的字符串,并计算它们之间的汉明距离。她寻求找到一个长度相等的字符串,使得这个新字符串到两个已知字符串的距离相等。通过解析输入的字符串并输出符合条件的字符串或“不可能”的信息,读者可以跟随小苏伊的思维过程,体验解决这类问题的乐趣。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

B. Equidistant String
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Little Susie loves strings. Today she calculates distances between them. As Susie is a small girl after all, her strings contain only digits zero and one. She uses the definition of Hamming distance:

We will define the distance between two strings s and t of the same length consisting of digits zero and one as the number of positions i, such that si isn't equal to ti.

As besides everything else Susie loves symmetry, she wants to find for two strings s and t of length n such string p of length n, that the distance from p to s was equal to the distance from p to t.

It's time for Susie to go to bed, help her find such string p or state that it is impossible.

Input

The first line contains string s of length n.

The second line contains string t of length n.

The length of string n is within range from 1 to 105. It is guaranteed that both strings contain only digits zero and one.

Output

Print a string of length n, consisting of digits zero and one, that meets the problem statement. If no such string exist, print on a single line "impossible" (without the quotes).

If there are multiple possible answers, print any of them.

Sample test(s)
input
0001
1011
output
0011
input
000
111
output
impossible
Note

In the first sample different answers are possible, namely — 0010, 0011, 0110, 0111, 1000, 1001, 1100, 1101.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include<stdio.h>
#include<string.h>
#define N 100010

char s1[N], s2[N];

int main()
{
    int len;
    while(~scanf("%s%s", s1, s2))
    {
        int k=2, t=0;
        len=strlen(s1);
        for(int i=0;i<len;i++)
        {
            if(s1[i]!=s2[i])
            {
                t++;
            }
        }
        if(t%2!=0)
        {
            printf("impossible\n");
            continue;
        }
        for(int i=0;i<len;i++)
        {
            if(s1[i]==s2[i])
                printf("0");
            else
            {
                if(k%2)
                    printf("%c", s1[i]);
                else
                    printf("%c", s2[i]);
                k++;
            }
        }
        printf("\n");
    }
    return 0;
}
%% 1. 创建三维轨迹点 theta = linspace(0, 6*pi, 30); points = [cos(theta')*2, sin(theta')*2, theta'/5]; %% 2. 弦长参数化(归一化) diffs = diff(points); distances = sqrt(sum(diffs.^2, 2)); t_original = [0; cumsum(distances)]; t_original = t_original / t_original(end); % 归一化到[0,1] %% 3. 创建三次B样条 spline_x = csape(t_original, points(:,1)', 'second'); spline_y = csape(t_original, points(:,2)', 'second'); spline_z = csape(t_original, points(:,3)', 'second'); %% 4. 计算总弧长(关键修改) % 在密集点上采样计算总弧长 t_temp = linspace(0, 1, 1000); pts_temp = [fnval(spline_x, t_temp); fnval(spline_y, t_temp); fnval(spline_z, t_temp)]'; diffs_temp = diff(pts_temp); total_length = sum(sqrt(sum(diffs_temp.^2, 2))); %% 5. 生成等距插值点(核心算法) num_points = 200; % 目标点数 target_spacing = total_length / (num_points-1); % 目标间距 % 初始化 t_equal = zeros(1, num_points); t_equal(1) = 0; % 起点参数 current_length = 0; interpolated_points = zeros(num_points, 3); interpolated_points(1,:) = points(1,:); % 起点 % 迭代计算等距点参数 for i = 2:num_points % 牛顿迭代法求解参数 t_guess = t_equal(i-1) + 0.01; % 初始猜测 for iter = 1:5 % 5次迭代通常足够 % 计算当前位置 pt_current = [fnval(spline_x, t_guess); fnval(spline_y, t_guess); fnval(spline_z, t_guess)]'; % 计算与前一点的距离 segment_vec = pt_current - interpolated_points(i-1,:); current_dist = norm(segment_vec); % 计算导数(速度向量) deriv_x = fnval(fnder(spline_x,1), t_guess); deriv_y = fnval(fnder(spline_y,1), t_guess); deriv_z = fnval(fnder(spline_z,1), t_guess); speed = norm([deriv_x, deriv_y, deriv_z]); % 牛顿法更新参数 t_guess = t_guess - (current_dist - target_spacing)/speed; end t_equal(i) = t_guess; interpolated_points(i,:) = pt_current; end %% 6. 验证等距性(可选) diffs_final = diff(interpolated_points); distances_final = sqrt(sum(diffs_final.^2, 2)); fprintf('平均间距: %.4f\n标准差: %.4f\n最大偏差: %.4f\n', ... mean(distances_final), std(distances_final), max(abs(distances_final - target_spacing))); %% 7. 可视化 figure; plot3(points(:,1), points(:,2), points(:,3), 'ro', 'MarkerSize', 8, 'MarkerFaceColor', 'r'); hold on; plot3(interpolated_points(:,1), interpolated_points(:,2), interpolated_points(:,3), 'b-o', 'LineWidth', 1.5); title('等距B样条插值轨迹'); grid on; axis equal; legend('原始点', '等距插值点', 'Location', 'best'); %% 8. 输出插值点 csvwrite('equidistant_points.csv', interpolated_points); 转换成使用C++代码实现
06-16
%% 1. 创建三维轨迹点 theta = linspace(0, 6*pi, 30); points = [cos(theta')*2, sin(theta')*2, theta'/5]; %% 2. 弦长参数化(归一化) diffs = diff(points); distances = sqrt(sum(diffs.^2, 2)); t_original = [0; cumsum(distances)]; t_original = t_original / t_original(end); % 归一化到[0,1] %% 3. 创建三次B样条 spline_x = csape(t_original, points(:,1)', 'second'); spline_y = csape(t_original, points(:,2)', 'second'); spline_z = csape(t_original, points(:,3)', 'second'); %% 4. 计算总弧长(关键修改) % 在密集点上采样计算总弧长 t_temp = linspace(0, 1, 1000); pts_temp = [fnval(spline_x, t_temp); fnval(spline_y, t_temp); fnval(spline_z, t_temp)]'; diffs_temp = diff(pts_temp); total_length = sum(sqrt(sum(diffs_temp.^2, 2))); %% 5. 生成等距插值点(核心算法) num_points = 200; % 目标点数 target_spacing = total_length / (num_points-1); % 目标间距 % 初始化 t_equal = zeros(1, num_points); t_equal(1) = 0; % 起点参数 current_length = 0; interpolated_points = zeros(num_points, 3); interpolated_points(1,:) = points(1,:); % 起点 % 迭代计算等距点参数 for i = 2:num_points % 牛顿迭代法求解参数 t_guess = t_equal(i-1) + 0.01; % 初始猜测 for iter = 1:5 % 5次迭代通常足够 % 计算当前位置 pt_current = [fnval(spline_x, t_guess); fnval(spline_y, t_guess); fnval(spline_z, t_guess)]'; % 计算与前一点的距离 segment_vec = pt_current - interpolated_points(i-1,:); current_dist = norm(segment_vec); % 计算导数(速度向量) deriv_x = fnval(fnder(spline_x,1), t_guess); deriv_y = fnval(fnder(spline_y,1), t_guess); deriv_z = fnval(fnder(spline_z,1), t_guess); speed = norm([deriv_x, deriv_y, deriv_z]); % 牛顿法更新参数 t_guess = t_guess - (current_dist - target_spacing)/speed; end t_equal(i) = t_guess; interpolated_points(i,:) = pt_current; end %% 6. 验证等距性(可选) diffs_final = diff(interpolated_points); distances_final = sqrt(sum(diffs_final.^2, 2)); fprintf('平均间距: %.4f\n标准差: %.4f\n最大偏差: %.4f\n', ... mean(distances_final), std(distances_final), max(abs(distances_final - target_spacing))); %% 7. 可视化 figure; plot3(points(:,1), points(:,2), points(:,3), 'ro', 'MarkerSize', 8, 'MarkerFaceColor', 'r'); hold on; plot3(interpolated_points(:,1), interpolated_points(:,2), interpolated_points(:,3), 'b-o', 'LineWidth', 1.5); title('等距B样条插值轨迹'); grid on; axis equal; legend('原始点', '等距插值点', 'Location', 'best'); %% 8. 输出插值点 csvwrite('equidistant_points.csv', interpolated_points); 换成使用PCL实现
06-16
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值