• Codeforces Gym-101808C(字符串)


    C. Help Shahhoud

    time limit per test

    2.0 s

    memory limit per test

    256 MB

    input

    standard input

    output

    standard output

    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 Ainto B.

    Example

    input

    Copy

    1
    abcxdef
    fecxdba

    output

    Copy

    2
    此题也不难,但是要注意细节,下面第一个代码TLE了,原因是当输出-1的时候没用break及时退出,第二个代码AC。
    代码一(TLE):
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cstdlib>
    using namespace std;
    int main(){
        string str1,str2;
        int n;
        int j;
        int i;
        int len;
        int sum;
        cin>>n;
        while(n--){
            cin>>str1;
            cin>>str2;
            len=str1.length();
            sum=0;
            for( i=0,j=len-1;i<len;i++,j--){
                if((str1[i]!=str2[i])&&(str1[i]!=str2[j])) 
                printf("-1
    ");
            }
            for(int i=0,j=len-1;i<(len-1)/2;i++,j--){
                if(sum%2==0){
                    if(str1[i]!=str2[i])
                    sum++;
                }
                else{
                    if(str1[i]!=str2[j])
                    sum++;
                }
            }
            cout<<sum<<endl;
    }
        return 0;
    }
    代码二(AC):
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cstdlib>
    using namespace std;
    int main(){
        string str1,str2;
        int n;
        int j;
        int i;
        int len;
        int sum;
        cin>>n;
        while(n--){
            cin>>str1;
            cin>>str2;
            len=str1.length();
            sum=0;
            for( i=0,j=len-1;i<len;i++,j--){
                if((str1[i]!=str2[i])&&(str1[i]!=str2[j])) 
                break;
            }
            if(i!=len) printf("-1
    ");
            else{
            for(int i=0,j=len-1;i<(len-1)/2;i++,j--){
                if(sum%2==0){
                    if(str1[i]!=str2[i])
                    sum++;
                }
                else{
                    if(str1[i]!=str2[j])
                    sum++;
                }
            }
            cout<<sum<<endl;
        }
       }
        return 0;
    }
    天晴了,起飞吧
  • 相关阅读:
    BAPI / RFC with Delphi(系列之一)--安装部件
    如何动态改变Table Control的列抬头
    如何实现标准TCODE的屏幕增强(HOWTO:Implement a screen exit to a standard SAP transaction)
    JNDI 连接Windows Active Directory 教程
    BAPI / RFC with Delphi(系列之三)--TSAPLogonControl使用(无对话框的登录sap的delphi源代码)
    BAPI / RFC with Delphi(系列之二)--TSAPLogonControl使用(有对话框的登录sap的delphi源代码)
    asp.net
    关于ETL的经验总结[经典](转)
    保持Oracle的优良性能
    保持Oracle的优良性能
  • 原文地址:https://www.cnblogs.com/jianqiao123/p/11238230.html
Copyright © 2020-2023  润新知