Middle-Out【codeforces 1231E】(字符串匹配问题)
The problem was inspired by Pied Piper story. After a challenge from Hooli's compression competitor Nucleus, Richard pulled an all-nighter to invent a new approach to compression: middle-out.
You are given two strings ss and tt of the same length nn. Their characters are numbered from 11 to nn from left to right (i.e. from the beginning to the end).
In a single move you can do the following sequence of actions:
- choose any valid index ii (1≤i≤n1≤i≤n),
- move the ii-th character of ss from its position to the beginning of the string or move the ii-th character of ss from its position to the end of the string.
Note, that the moves don't change the length of the string ss. You can apply a move only to the string ss.
For example, if s=s="test" in one move you can obtain:
- if i=1i=1 and you move to the beginning, then the result is "test" (the string doesn't change),
- if i=2i=2 and you move to the beginning, then the result is "etst",
- if i=3i=3 and you move to the beginning, then the result is "stet",
- if i=4i=4 and you move to the beginning, then the result is "ttes",
- if i=1i=1 and you move to the end, then the result is "estt",
- if i=2i=2 and you move to the end, then the result is "tste",
- if i=3i=3 and you move to the end, then the result is "tets",
- if i=4i=4 and you move to the end, then the result is "test" (the string doesn't change).
You want to make the string ss equal to the string tt. What is the minimum number of moves you need? If it is impossible to transform ss to tt, print -1.
The first line contains integer qq (1≤q≤1001≤q≤100) — the number of independent test cases in the input.
Each test case is given in three lines. The first line of a test case contains nn (1≤n≤1001≤n≤100) — the length of the strings ss and tt. The second line contains ss, the third line contains tt. Both strings ss and tt have length nn and contain only lowercase Latin letters.
There are no constraints on the sum of nn in the test (i.e. the input with q=100q=100 and all n=100n=100 is allowed).
For every test print minimum possible number of moves, which are needed to transform ss into tt, or -1, if it is impossible to do.
3 9 iredppipe piedpiper 4 estt test 4 tste test
2 1 2
4 1 a z 5 adhas dasha 5 aashd dasha 5 aahsd dasha
-1 2 2 3
In the first example, the moves in one of the optimal answers are:
- for the first test case s=s="iredppipe", t=t="piedpiper": "iredppipe" →→ "iedppiper" →→ "piedpiper";
- for the second test case s=s="estt", t=t="test": "estt" →→ "test";
- for the third test case s=s="tste", t=t="test": "tste" →→ "etst" →→ "test".
解题报告:题意解释就是给定两个长度为n的字符串s和t,其中对于s中的任意一个字符他都可以将该字符放到字符串首部或者尾部,问最小的操作次数是多少,可以实现s==t
首先要明确一点就是我们可以通过调整单个字符到字符串首尾,那么我们就可以直接去寻找最小的s和t的不同字符就可以
ac代码:
#include<iostream> #include<cstring> #include<cstdio> #include<algorithm> using namespace std; typedef long long ll; int main() { int t; scanf("%d",&t); while(t--) { int n; scanf("%d",&n); string a,b; cin>>a>>b; int ans=1e9; for(int i=0;i<n;i++) { int k=i; for(int j=0;j<n;j++) { if(k<n&&a[j]==b[k]) k++; }//就是相当于我每次都以一个新的起点去枚举b,然后对于这个位置会有一个变成a的最小修改值 //只要是有一部分相同的字符了,我就可以通过将某一位置的字符进行前/后置转换成想要的a ans=min(ans,n-k+i); // cout<<ans<<endl; } // cout<<a<<" "<<b<<endl; sort(a.begin(),a.end()); sort(b.begin(),b.end()); // cout<<a<<" "<<b<<endl; if(a!=b) { printf("-1\n"); continue; } printf("%d\n",ans); } }