Links: https://ac.nowcoder.com/acm/problem/13222
Source: Cattle-off network
Title Description
Specifically, the first stage is to find a length and an equal and continuous audio sequence in the second segment of the audio so as to minimize their difference. Audio define two equal length difference is:
difference = the SUM (A [I] - B [I]) 2 (≤ I ≤ n. 1), where the SUM () indicates summation
where n represents the sequence length, a [i] , b [i] respectively represent two audio pitch. Mr. Kangaroo want to know now, the minimum difference is how much? Audio data to ensure that the first segment length is less than equal to the length of the second segment of audio.
Enter a description:
The first line of an integer n (1 ≤ n ≤ 1000) , indicates the length of the first segment of the audio. The second row of n represents an integer of the pitch of the first segment of an audio (0 ≤ pitch ≤ 1000). A third line integer m (1 ≤ n ≤ m ≤ 1000), indicates the length of the second segment of audio. M represents an integer in the fourth row of the second section of audio pitch (0 ≤ pitch ≤ 1000).
Output Description:
The minimum value of the output difference
Export
copy0
#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
int main(){
int n,m,a[1001],b[1001];
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
}
cin>>m;
for(int i=1;i<=m;i++){
cin>>b[i];
}
long long ans=10000000000;
for(int i=1;i<=m-n+1;i++){
long long sum=0;
for(int j=1;j<=n;j++){
sum=sum+pow((a[j]-b[i+j-1]),2);
}
ans=min(sum,ans);
}
cout<<ans<<endl;
return 0;
}
Encountered a problem: she did not understand the meaning of the questions, that is different from the number of different, so they can not AC, did not pay attention to the topic of hidden conditions are different and equal to the size of a tune strings.
Solution: different is (a [i] -b [i]) ^ 2 is calculated, and is looking for a string equal, then each of the inner loop only needs to loop size (a) times, the outer loop needs only to m-n + 1 of the line (the second string which is equal to a first set of strings is the last in the size is the size of the m-n + 1), so that the outer loop requires only m -n + 1 times.
Sentiment: enumeration done before, I felt very simple, and it is looked down upon this method, I understand that this is not written me a little difficulty of the subject, and now I began to brush enumeration topic, intends to put enumeration topic brush enough in other title to brush, personally I think that the topic is the fastest way to enumerate people really focus on the idea turned into practice, first enumeration, in the algorithm. Chong Chong Chong