早教吧作业答案频道 -->其他-->
DescriptionYouaregivenastringAandastringBofthesamelength,lessthan1000.InoneturnyoucanselectanycharacterofAandmoveittothebeginningofthestring(i.e.,beforethefirstcharacterofA).Outputtheminimalnumberofturnsne
题目详情
Description
You are given a string A and a string B of the same length,less than 1000.
In one turn you can select any character of A and move it to the beginning of the string (i.e.,
before the first character of A).
Output the minimal number of turns needed to transform A into B.If it's impossible,output -1
instead.
Input
The first line of input contains an integer T,indicates the cases.
Next T lines each contains 2 strings A and B that contain only lowercase letters
Output
Output T lines each contains the answer required.
Sample Input
3
abc cba
a b
aaabbb bbbaaa
Sample Output
2
-1
3
You are given a string A and a string B of the same length,less than 1000.
In one turn you can select any character of A and move it to the beginning of the string (i.e.,
before the first character of A).
Output the minimal number of turns needed to transform A into B.If it's impossible,output -1
instead.
Input
The first line of input contains an integer T,indicates the cases.
Next T lines each contains 2 strings A and B that contain only lowercase letters
Output
Output T lines each contains the answer required.
Sample Input
3
abc cba
a b
aaabbb bbbaaa
Sample Output
2
-1
3
▼优质解答
答案和解析
#include
#include
using namespace std;
int main(){
\x09int i, j, n, T;
\x09cin >> T;
\x09while(T--){
\x09\x09string A, B;
\x09\x09cin >> A >> B;
\x09\x09n = A.length();
\x09\x09vector vA(26), vB(26);
\x09\x09for(i=0; i\x09\x09\x09vA[A[i]-'a']++, vB[B[i]-'a']++;
\x09\x09for(i=n-1, j=n-1; i>=0; i--)
\x09\x09\x09j -= (A[i] == B[j]);
\x09\x09cout << ((vA==vB)? j+1: -1) << endl;
\x09}
}
题目等价于在A中找出一个最长的与B后缀相同的子序列.
#include#include
#include
using namespace std;
int main(){
\x09int i, j, n, T;
\x09cin >> T;
\x09while(T--){
\x09\x09string A, B;
\x09\x09cin >> A >> B;
\x09\x09n = A.length();
\x09\x09vector
\x09\x09for(i=0; i
\x09\x09for(i=n-1, j=n-1; i>=0; i--)
\x09\x09\x09j -= (A[i] == B[j]);
\x09\x09cout << ((vA==vB)? j+1: -1) << endl;
\x09}
}
看了DescriptionYoua...的网友还看了以下:
在公式E=I(R+r/n)中,已知E,I,R,r,求E≠IR,求n在公式E=I(R+r/n)中,已 2020-05-23 …
MATLAB求解符号线性方程组时,结果误差很大?symsF1F2F3F4F5C1C2C3C4EID 2020-06-04 …
e^(2πi*(1/6))=(e^2πi)^(1/6)=1^(1/6)=1e^(2πi*(1/6) 2020-06-12 …
求下面函数的解释,看不懂function A = fun(W)[m,n] = size(W); e 2020-06-27 …
英语单词填空1.时间状语:d-r-n-2.场所:b-s-s-o-f-r--e-a-t-e-t3.教 2020-07-14 …
英语翻译1、OldMacdonaldhadafarm.E-I-E-I-O.Andonthatfarm 2020-11-01 …
求证e^i(4π/n)+e^i(8π/n)+...+e^i4(n-1)π/n+e^i(4nπ/n)= 2020-11-01 …
DescriptionYouaregivenastringAandastringBofthesame 2020-11-10 …
2-1设文法G[E]的产生式集为:EàE+T|E-T|TTàT*F|T/F|FFà(E)|i(1)给 2020-11-26 …
英语向高人求教!写几句话.每句开头的第一个字母分别是“L,i,U,F,E,i,F,E,i,w,o,a 2020-12-15 …