C - Help Shahhoud (18.9.30)(字符串交换的最小次数)

10 篇文章 0 订阅

Shahhoud is participating in the first Div.3 contest on Codeforces, the first problem was:

Given two strings A and B of equal length N (N is odd), find the minimum number of steps needed to change A into B, or print  - 1 if it's impossible.

In each step, you can choose an odd integer x such that 1 ≤ x ≤ N, and reverse the substring of length x that is centered in the middle of A. For example, performing a step with x = 3 on the string "abcde" results in "adcbe" and applying x = 5 on "abcde"results in "edcba".

Can you help Shahhoud solve the problem?

Input

The first line contains one integer T, the number of test cases.

Each test case consists of two lines, the first contains the string A, and the second contains the string B. (1 ≤ |A| = |B| ≤ 105) |A| = |B| is odd.

Both strings consist of lowercase English letters.

Output

For each test case, print one line containing one integer,  - 1 if A can't be changed into B, or the minimum number of steps to change A into B.

Example

Input

1
abcxdef
fecxdba

Output

2



#include <iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
using namespace std;
int main()
{char s1[100500],s2[100500];
int n,i,t,num,j;
scanf("%d",&t);
while(t--)
{
    cin>>s1>>s2;
    n=strlen(s1);
    for(i=0,j=n-1;i<n;i++,j--)
    {
        if(s1[i]!=s2[i]&&s1[i]!=s2[j])
            break;
    }
    if(i!=n)
        printf("-1\n");
    else
    {
        num=0;
        for(i=0,j=n-1;i<n/2;i++,j--)
        {
            if(num%2==0)
            {
                if(s1[i]!=s2[i])
                    num++;
            }
            else
            {
                if(s1[i]!=s2[j])
                {
                    num++;
                }
            }
        }
        printf("%d\n",num);
    }
}
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值