2018-01-08

// 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;
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容