Cod sursa(job #1338041)

Utilizator dan.ghitaDan Ghita dan.ghita Data 9 februarie 2015 19:07:12
Problema Convertor Scor 100
Compilator cpp Status done
Runda rosedu_cdl_2015 Marime 1.68 kb
#include <bits/stdc++.h>

using namespace std;

fstream f, g;

vector<string> keys;
unordered_map< string, vector<string> > values;
unordered_set<string> usedKeys;

bool parsingKey;
string lastKey;

inline bool isDigit(char c)
{
    return ('0' <= c && c <= '9');
}

void parseLine(string line)
{

    string newItem = "";

    for(int i = 0; i < line.size(); ++i)
    {
        newItem = "";

        if(line[i] == '\"')  /// if it's a string it starts with "
        {

            ++i;

            while(line[i] != '\"')
                newItem += line[i], ++i;

        }
        else if(isDigit(line[i])) /// if it's a number it starts with a digit
            while(isDigit(line[i]))
                newItem += line[i], ++i;

        if(newItem != "")  /// if I have a new item I check to see if it is a key or a value
        {

            if(parsingKey)  /// it's a key!
            {
                lastKey = newItem;

                if(usedKeys.find(newItem) == usedKeys.end()) /// if it's a new key i add it to the vector of keys
                    usedKeys.insert(newItem),
                    keys.push_back(newItem);

            }
            else /// it's a value!
                values[lastKey].push_back(newItem);

            parsingKey = !parsingKey;
        }

    }


}


int main()
{
    f.open("convertor.in", fstream::in);
    g.open("convertor.out", fstream::out);

    string line;
    parsingKey = true;

    while( getline(f, line) )
        parseLine(line);


    for(auto it : keys)
        g<<it<<',';
    g<<"\n";

    for(int i = 0; i < values[keys[0]].size(); ++i)
    {
        for(auto key : keys)
            g<<values[key][i]<<",";
        g<<"\n";
    }

    return 0;
}