// ConsoleApplication1.cpp: 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
#include <tuple>
using namespace std;
int ipow[6] = { -3, -2, -1, 3, 1, 2 };
vector< int > powVec(ipow, ipow + 6);
vector<vector<int>> hasKnownedPowValue(8, powVec); //已知的Pow对8个f_id的所有值
int iln[3] = { -2, -1, 1 };
vector< int > lnVec(iln, iln + 3);
vector<vector<int>> hasKnownedLnValue(8, lnVec); //已知的ln对8个f_id的所有值
int iexp[3] = { -3, -2, -1 };
vector< int > expVec(iexp, iexp + 3);
vector<vector<int>> hasKnownedExpValue(8, expVec); //已知的exp对8个f_id的所有值
bool isContain(vector<int> vec, int number);
int fidIndex(string f_id);
int funcIndex(string func_name);
vector<int> whichKnownValue(string f_id, string func_name);
//split 方法
std::vector<std::string> split(const std::string& s, const std::string& delim)
{
std::vector<std::string> elems;
size_t pos = 0;
size_t len = s.length();
size_t delim_len = delim.length();
if (delim_len == 0) return elems;
while (pos < len)
{
int find_pos = s.find(delim, pos);
if (find_pos < 0)
{
elems.push_back(s.substr(pos, len - pos));
break;
}
elems.push_back(s.substr(pos, find_pos - pos));
pos = find_pos + delim_len;
}
return elems;
}
//对一个Tuple进行处理
//返回这个Tuple是否被删除
bool solution(vector<string> dstTuple)
{
size_t tupleNumber = dstTuple.size();
vector<vector<int>> list(8, vector<int>(3,0));
for (size_t i = 0; i < tupleNumber; i++)
{
//取出tuple第i个元素(是一个String类型)
//例如 currentTupleElementStr = "f1:pow:1"
string currentTupleElementStr = dstTuple[i];
//以":"分割string
vector<string>splitStr = split(currentTupleElementStr, ":");
string current_f_id = splitStr[0];
string current_func_name = splitStr[1];
string curren_index_str = splitStr[2];
int current_index_value = atoi(curren_index_str.c_str());
string current_needToCompare = current_f_id + current_func_name; //"f1pow"
//判断当前元素是否被统计过,如果被统计过直接下一个
int current_func_index = funcIndex(current_func_name);
//如果被统计过, 当天tuple的这个方法的累加值一定不等于0
if (list[fidIndex(current_f_id)][current_func_index] != 0)
{
continue;
}
list[fidIndex(current_f_id)][current_func_index] = current_index_value;
for (size_t j = 1; j < tupleNumber - i; j++)
{
//取出tuple第i+j个元素(是一个String类型)
//例如 nextTupleElementStr = "f2:pow:2"
string nextTupleElementStr = dstTuple[i + j];
//以":"分割string
vector<string>splitStr = split(nextTupleElementStr, ":");
string next_f_id = splitStr[0];
string next_func_name = splitStr[1];
string next_index_str = splitStr[2];
int next_index_value = atoi(next_index_str.c_str());
string next_needToCompare = next_f_id + next_func_name; //"f2pow"
//如果函数名字相同包括f_id
if (current_needToCompare == next_needToCompare)
{
//属于哪个pow,ln,exp已知值里
vector<int> knowValueVec = whichKnownValue(current_f_id, current_func_name);
//当前累加值
int tmpSum = 0;
int func_index = funcIndex(next_func_name);
tmpSum = list[fidIndex(next_f_id)][func_index] + next_index_value;
// 是否包含在已知的值里面
if (isContain(knowValueVec, tmpSum))
{
//这个tuple重复了 添加到需要删除的数组里
return true;
}
//如果j为最后一个
if (j == tupleNumber - i)
{
//添加到已知值
knowValueVec.push_back(tmpSum);
//添加到当前tuple指数累积值
list[fidIndex(next_f_id)][func_index] = tmpSum;
}
}
}
}
return false;
}
bool isContain(vector<int> vec, int number)
{
vector<int>::iterator ret;
ret = std::find(vec.begin(), vec.end(), number);
if (ret == vec.end())
return false;
else
return true;
}
int fidIndex(string f_id)
{
string str1 = f_id.substr(1, f_id.length() - 1);
return atoi(str1.c_str()) - 1;
}
int funcIndex(string func_name)
{
if (func_name == "pow")
{
return 0;
}
else if (func_name == "ln")
{
return 1;
}
else
{
return 2;
}
}
//选择已知的指数数组,通过函数名字和f_id确定
vector<int> whichKnownValue(string f_id, string func_name)
{
int f_id_index = fidIndex(f_id);
vector<int> knowValue;
if (func_name == "pow")
{
knowValue = hasKnownedPowValue[f_id_index];
}
else if (func_name == "ln")
{
knowValue = hasKnownedLnValue[f_id_index];
}
else
{
knowValue = hasKnownedExpValue[f_id_index];
}
return knowValue;
}
int main()
{
//vector<vector<string>> tuple = { {"f1:pow:1"},{"f2:pow:2"},{ "f1:pow:2" },{ "f2:pow:2" } };
vector<string> tuple = { "f1:pow:1" ,"f2:pow:2" , "f1:pow:4" , "f2:pow:2" };
cout<<solution(tuple);
return 0;
}
2018-01-08
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 本周开心事儿: 1.转角遇见遛狗归来的老爷爷 早上上班路口转角处,看见了隔壁单元遛狗回来的老爷爷,好久没有遇见了,...
- 1.早睡早起 本周早起不用闹钟基本没问题,虽然起床时间不是很固定,这和就寝时间不固定有很大关系。睡眠周期也在努力...
- 芯芯第503~509天,16M+15D~21D “拜拜” : 这周女儿刚学会说的词语——“拜拜”,真让我喜出望外!...