[英]Simultaneous Union and Intersection between two maps in C++
在做一個大學項目時,我遇到了以下問題:我有兩個地圖(Kmer1 和 Kmer2),它們由一個字符串(鍵)和一個整數(值)組成。 我必須計算遵循這個公式的距離
[1-(I/U)]*100
Where...
...U = the sum of all int values inside Kmer1 U Kmer2
...I = the sum of all int values inside Kmer1 ∩ Kmer2
Consider that...
... The U and ∩ are made evaluating the keys (strings)
... When an element is in both maps:
- At the Union we add the one with higher int value
- At the Intersection we add the one with lower int value
例子:
Kmer1 = AAB¹ AAC¹ AAG³
Kmer2 = AAG¹ AAT² ABB¹
Union = AAB¹ AAC¹ AAG³ AAT² ABB¹ U= 8
Intersection = AAG¹ I= 1
Distance = 87.5
代碼時間。 我一直在嘗試解決它,但所有解決方案都像。,部分正確。 並非所有情況都被涵蓋,所以當我試圖涵蓋它們時,我以無限循環結束,異常上升。 無論如何,if-else 的長長的巢(這很糟糕。):這是最不糟糕且不起作用的嘗試:
設置:
Species::Kmer Kmer1, Kmer2; //The two following lines get the Kmer from another
Kmer1 = esp1->second.query_kmer(); //object.
Kmer2 = esp2->second.query_kmer();
Species::Kmer::const_iterator it1, it2, last1, last2;
it1 = Kmer1.cbegin(); //Both Kmer are maps, therefore they are ordered and
it2 = Kmer2.cbegin(); //whitout duplicates.
last1 = --Kmer1.cend();
last2 = --Kmer2.cend();
double U, I;
U = I = 0;
應用公式的循環:
while (it1 != Kmer1.cend() and it2 != Kmer2.cend()){
if (it1->first == it2->first) {
if (it1->second > it2->second) {
U += it1->second;
I += it2->second;
} else {
U += it2->second;
I += it1->second;
}
++it1;
++it2;
} else if (it1->first < it2->first) {
U += it1->second;
++it1;
} else {
U += it2->second;
++it2;
}
}
請注意,我不是先創建聯合和交集,然后再計算每個的總和,而是直接跳到值的總和。 我知道也許這並不難,但我一直在努力解決它,但我幾乎被困住了......
I've uploaded the whole code at Github: (Maybe it helps)
- There is a makefile to build the code
- There is a file called input.txt with a sample for this specific problem
- Also inside the input.txt, after line13 (fin) I've added the expected output
- Executing ./program.exe < input.txt should be enough to test it.
https://github.com/PauGalopa/Cpp-Micro-Projects/tree/master/Release
重要的是。 我知道幾乎所有的 STL 功能都可以在幾行中做到這一點但是......由於這是一個大學項目,我受到教學大綱的限制所以考慮我只允許使用“地圖“”“字符串”“向量”等等,不。我不能使用“算法”(我真的希望我可以)我會澄清關於我可以在評論中做或使用哪些事情的任何疑問。
在主 while 循環之后添加這兩個循環。
while (it1 != Kmer1.cend()){
U += it1->second;
it1++;
}
while (it2 != Kmer2.cend()){
U += it2->second;
it2++;
}
這是一個相當簡單的解決方案,僅使用std::map
的一些屬性,沒有迭代器。 我希望你被允許使用這種解決方案。
#include <iostream>
#include <map>
#include <string>
int main () {
std::map <std::string, int> A = {{"AAB", 1}, {"AAC", 1}, {"AAG", 3}};
std::map <std::string, int> B = {{"AAG", 1}, {"AAT", 2}, {"ABB", 1}};
std::map <std::string, int> Union;
int sum_A = 0, sum_B = 0, sum_Union = 0, sum_Inter = 0;;
for (auto &x: A) {
Union[x.first] = std::max (Union[x.first], x.second);
sum_A += x.second;
}
for (auto &x: B) {
Union[x.first] = std::max (Union[x.first], x.second);
sum_B += x.second;
}
for (auto &x: Union) {
sum_Union += x.second;
}
sum_Inter = sum_A + sum_B - sum_Union;
double distance = 100.0 * (1.0 - double(sum_Inter)/sum_Union);
std::cout << "sum_Union = " << sum_Union << " sum_Inter = " << sum_Inter << "\n";
std::cout << "Distance = " << distance << "\n";
}
unordered_mapping
的一種更簡潔的方法,但仍然適用於mapping
,是將Kmer1
的所有元素添加到U
,並將共享元素添加到I
。 然后將Kmer2
的所有未共享元素添加到U
:
for(it1 = Kmer1.cbegin(); it1 != Kmer1.cend(); it1++) {
auto other = Kmer2.find(it1->first);
if(other == Kmer2.cend()) {
U += it1->second;
} else {
U += max(it1->second, other->second);
I += min(it1->second, other->second);
}
}
for(it2 = Kmer2.cbegin(); it2 != Kmer2.cend(); it2++) {
if(Kmer1.count(it2->first) == 0) {
U += it2->second
}
}
對於正確實現的unordered_mapping
(哈希表), find
操作將是O(1)
,而不是O(log(n)
,使其更快一些。
這個循環應該工作:
while ( true ){
bool end1 = it1 == Kmer1.cend();
bool end2 = it2 == Kmer2.cend();
if( end1 and end2 )
break;
if( end2 or it1->first < it2->first ) {
U += (it1++)->second;
continue;
}
if( end1 or it2->first < it1->first ) {
U += (it2++)->second;
continue;
}
auto p = std::minmax( (it1++)->second, (it2++)->second );
I += p.first;
U += p.second;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.