algorithm - matching a sequence of text files one by one upto a certain limit in c++ -
i have made program in c++ generates 20 text files 1.txt 100.txt. each text files contains random 5 numbers between 1 , 10. example:
1.txt 2.txt 3.txt 4.txt 5.txt 6.txt 7.txt 8.txt .... 1 4 3 3 1 7 1 8 4 7 4 4 4 1 3 9 6 6 8 5 3 4 4 6 7 3 1 1 7 3 7 1 8 9 7 2 9 2 10 3
i want check percentage of similarity previous files 1 one starting 2.txt. in above files example: numbers 1,3,4 , 7 arising till 7.txt in 8.txt it's different. can 8.txt dissimilar previous files. actual target stop checking files when dissimilarity appears need not check 20 text files. in word, i want find out file number until which, percentage of similarity remains same or increases. when starts decreasing, stop checking.
how in coding using c++? example: when check 3.txt check both 1.txt , 2.txt or 2.txt , on? logic behind this? , how calculate similarity?
std::intersection useful in case.
algorithm follows:
initialize varible called previoussimilarity zero.
x number ranging 1 100.
read numbers file x.txt vector v1
read numbers file (x+1).txt vector v2
use std::intersection algorithm find similarity , store in currentsimilarity.
if currentsimilarity < previoussimilarity, break out of loop (x last similar file number)
copy v2 v1.
increment x
go step 4.
sample code(skipping file reading part simplicity)
#include <algorithm> #include <iostream> #include <vector> using namespace std; int main() { std::vector<int> v1 = {111,3, 4, 88}; std::vector<int> v2 = {666, 4, 22, 3, 7777}; std::sort(v1.begin(), v1.end()); std::sort(v2.begin(), v2.end()); int minsize = std::min(v1.size(), v2.size()); std::vector<int> result(minsize); std::vector<int>::iterator = std::set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), result.begin()); result.resize(it - result.begin()); for(std::vector<int>::iterator = result.begin(); != result.end(); ++it) { cout<<*it<<endl; } }
output:
3 4
Comments
Post a Comment