问题描述
我正在编写一个代码,它使用我制作的一个函数来计算应该代表扑克牌的数字和字母字符串之间的相似度分数。例如,字符串“H8C6D6”代表 8 个红心、6 个俱乐部和 6 个钻石。无论如何,我正在编写的新函数采用一个长度大于或等于黄金序列的字符串(也就是为了计算相似度分数而与之进行比较的字符串),并计算所有子串的相似度分数与黄金序列等长。我遇到的问题是我只应该输出最大的相似度分数,而是输出所有相似度分数。我不知道如何解决这个问题。我尝试过 fmax() 等函数,但它们对我不起作用。任何帮助将不胜感激。
#include <iostream>
#include <cstring>
#include <cmath>
using namespace std;
double calcLikenessscore(string seq1,string seq2)
{
int n=seq1.length(); //length of seq1
int m=seq2.length(); //length of seq2
if(n != m) //if lengths of both the strings are not equal then we return -1
return -1;
int match_cards = 0; //variable to store the matching cards
int bonus = 0; //variable to store the bonus
for(int i = 0; i < n; i += 2)
{
if(seq1[i] == seq2[i]) //if the suit of both the sequences is equal
{
match_cards++; //matching cards is incremented by 1
if(seq1[i + 1] == seq2[i + 1]) //if the rank of both sequences is also equal
{
bonus++; //bonus is incremented by 1.
}
}
}
int total_cards = n / 2; //total cards are length/2.
double likeness_score = double(match_cards) / double(total_cards) + bonus; //calculating
likeness_score
return likeness_score;
}
double bestLikenessscore(string seq1,string gold_seq)
{
int n = seq1.length(); //Length of sequence 1
int m = gold_seq.length(); //Length of golden sequence
if (n < m) //If the length of sequence 1 is not greater than or equal to the golden
sequence,then it will return -1
return -1;
int i;
for (int i; i < n; i += 2)
{
string str1 = seq1.substr(i,m); //String that holds substring values of seq1
double d = calcLikenessscore(str1,gold_seq); //Function to calculate Likeness
scores of substrings
if (str1.length() == m)
{
cout << d << endl; //Outputs likeness scores,but how can I make it so it will
only output the largest value
}
}
return 0;
}
int main()
{
string s1; //First string to test; should be of greater or equal length to s2(gold_seq)
string s2; //Golden Sequence
cout << "enter first string: " << endl;
cin >> s1;
cout << "enter second string: " << endl;
cin >> s2;
double c = bestLikenessscore(s1,s2); //Runs all substrings through calcLikenessscore()
function,and outputs the lkeness scores of substrings
return 0;
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)