首页 > 试题广场 >

字符串问题

[编程题]字符串问题
  • 热度指数:212 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
小摩手里有一个字符串A,小拜的手里有一个字符串B,B的长度大于等于A,所以小摩想把A串变得和B串一样长,这样小拜就愿意和小摩一起玩了。
而且A的长度增加到和B串一样长的时候,对应的每一位相等的越多,小拜就越喜欢。比如"abc"和"abd"对应相等的位数为2,为前两位。
小摩可以在A的开头或者结尾添加任意字符,使得长度和B一样。现在问小摩对A串添加完字符之后,不相等的位数最少有多少位?

输入描述:
第一行 为字符串A,第二行 为字符串B,
A的长度小于等于B的长度,B的长度小于等于100。
字符均为小写字母。


输出描述:
输出一行整数表示A串添加完字符之后,A B 不相等的位数最少有多少位?
示例1

输入

abe
cabc

输出

1
说白了就是在B串上移动A串找到最大公共子长度,但是比较时不能超出B串。

发表于 2018-10-07 14:02:25 回复(0)
#include<iostream>
#include<string>
using namespace std;
 
int main(){
    string s,s1;
    cin>>s>>s1;
     
    int sum1=0;
    //前面加
    int i,j;
    j = s1.size()-1;
    for(i=s.size()-1;i>=0;i--,j--){
        if(s[i]!=s1[j]){
            sum1++;
        }
    }
    //后面
    int sum2=0;
    for(i=0,j=0;i<s.size();i++,j++){
        if(s[i]!=s1[j]){
            sum2++;
        }
    }
    if(sum1>sum2){
         cout<<sum2;  
    }else{
        cout<<sum1;
    }
     
    return 0;
}

发表于 2020-07-30 11:48:48 回复(0)
A = input()
B = input()
na,nb = len(A),len(B)
res = []
for i in range(nb-na+1):
    count = 0
    for j in range(na):
        if B[i+j]!=A[j]:
            count += 1
    res.append(count)
print(min(res))

发表于 2020-05-03 18:03:36 回复(0)
importjava.util.Scanner;
 
publicclassMain {
    publicstaticvoidmain(String[] args) {
        Scanner scanner = newScanner(System.in);
        String mo =scanner.nextLine();
        String bai =scanner.nextLine();
        char[] cmo=mo.toCharArray();
        String maxStr="";
        String current="";
        String x="";
        String pre="";
        inttmp=0;
        for(inti=0;i<mo.length();i++) {
            current=x;
            tmp=i+x.length();
            while(tmp<mo.length()){
                 
                current+=cmo[tmp];
                if(!bai.substring(i,bai.length()).contains(current)) {
                    break;
                }else{
                    pre=current;
                }              
                tmp++;
            }
            if(maxStr.length()<pre.length())maxStr=pre;
            if(pre.length()>1) {
                x=pre.substring(1,pre.length());
            }else{
                x="";
            }
        }
        System.out.println(mo.length()-maxStr.length());
    }
}
求指教,就会写完这一个
发表于 2018-08-22 16:43:14 回复(0)