<p>A website domain <code>&quot;discuss.leetcode.com&quot;</code> consists of various subdomains. At the top level, we have <code>&quot;com&quot;</code>, at the next level, we have <code>&quot;leetcode.com&quot;</code>&nbsp;and at the lowest level, <code>&quot;discuss.leetcode.com&quot;</code>. When we visit a domain like <code>&quot;discuss.leetcode.com&quot;</code>, we will also visit the parent domains <code>&quot;leetcode.com&quot;</code> and <code>&quot;com&quot;</code> implicitly.</p>

<p>A <strong>count-paired domain</strong> is a domain that has one of the two formats <code>&quot;rep d1.d2.d3&quot;</code> or <code>&quot;rep d1.d2&quot;</code> where <code>rep</code> is the number of visits to the domain and <code>d1.d2.d3</code> is the domain itself.</p>

<ul>
	<li>For example, <code>&quot;9001 discuss.leetcode.com&quot;</code> is a <strong>count-paired domain</strong> that indicates that <code>discuss.leetcode.com</code> was visited <code>9001</code> times.</li>
</ul>

<p>Given an array of <strong>count-paired domains</strong> <code>cpdomains</code>, return <em>an array of the <strong>count-paired domains</strong> of each subdomain in the input</em>. You may return the answer in <strong>any order</strong>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> cpdomains = [&quot;9001 discuss.leetcode.com&quot;]
<strong>Output:</strong> [&quot;9001 leetcode.com&quot;,&quot;9001 discuss.leetcode.com&quot;,&quot;9001 com&quot;]
<strong>Explanation:</strong> We only have one website domain: &quot;discuss.leetcode.com&quot;.
As discussed above, the subdomain &quot;leetcode.com&quot; and &quot;com&quot; will also be visited. So they will all be visited 9001 times.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> cpdomains = [&quot;900 google.mail.com&quot;, &quot;50 yahoo.com&quot;, &quot;1 intel.mail.com&quot;, &quot;5 wiki.org&quot;]
<strong>Output:</strong> [&quot;901 mail.com&quot;,&quot;50 yahoo.com&quot;,&quot;900 google.mail.com&quot;,&quot;5 wiki.org&quot;,&quot;5 org&quot;,&quot;1 intel.mail.com&quot;,&quot;951 com&quot;]
<strong>Explanation:</strong> We will visit &quot;google.mail.com&quot; 900 times, &quot;yahoo.com&quot; 50 times, &quot;intel.mail.com&quot; once and &quot;wiki.org&quot; 5 times.
For the subdomains, we will visit &quot;mail.com&quot; 900 + 1 = 901 times, &quot;com&quot; 900 + 50 + 1 = 951 times, and &quot;org&quot; 5 times.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= cpdomain.length &lt;= 100</code></li>
	<li><code>1 &lt;= cpdomain[i].length &lt;= 100</code></li>
	<li><code>cpdomain[i]</code> follows either the <code>&quot;rep<sub>i</sub> d1<sub>i</sub>.d2<sub>i</sub>.d3<sub>i</sub>&quot;</code> format or the <code>&quot;rep<sub>i</sub> d1<sub>i</sub>.d2<sub>i</sub>&quot;</code> format.</li>
	<li><code>rep<sub>i</sub></code> is an integer in the range <code>[1, 10<sup>4</sup>]</code>.</li>
	<li><code>d1<sub>i</sub></code>, <code>d2<sub>i</sub></code>, and <code>d3<sub>i</sub></code> consist of lowercase English letters.</li>
</ul>

class Solution {
public:
    tuple<int,string,string,string> findValue(string s){
        int n = s.size();
        int a = 0;
        int dot=2;
        string d1;
        string d2;
        string d3;

        a = stoi(s.substr(0,s.find(' ')));

        // cout<<s.substr(s.find(' ')+1)<<endl;
        string x1 = s.substr(s.find(' ')+1);
        // cout<<x1<<endl;
        string x2 = x1.substr(x1.find('.')+1);
        // cout<<x2<<endl;
        // cout<<(x2.find('.')!=string::npos)<<endl;
        string x3 = x2.substr(x2.find('.')+1);
        // cout<<x2<<" "<<x3<<endl;

        // n--;
        // while(s[n]!=' '){
        //     if(s[n]=='.'){
        //         dot--;
        //     }
        //     if(dot>=2){
        //         d3 = s[n]+d3;
        //     }
        //     if(dot>=1){
        //         d2 = s[n]+d2;
        //     }
        //     d1 = s[n]+d1;
        //     n--;
        // }
        tuple<int,string,string,string> t1;
        get<0>(t1) = a;
        get<1>(t1) = x1;
        get<2>(t1) = x2;
        (x2!=x3)?(get<3>(t1) = x3):("");
        // (d1!=d2)?(get<1>(t1)=d1):(get<1>(t1)="");
        // (d2!=d3)?(get<2>(t1)=d2):(get<2>(t1)="");
        // get<3>(t1)=d3;

        return t1;
    }

    vector<string> subdomainVisits(vector<string>& cpdomains) {
        vector<string> ans;
        unordered_map<string,int> umap;

        for(auto s:cpdomains){
            tuple<int,string,string,string> t1 = findValue(s);
            int a; string b; string c; string d;
            tie(a,b,c,d) = t1;
            if(!b.empty()){
                umap[b] += a;
            }
            if(!c.empty()){
                umap[c] += a;
            }
            if(!d.empty()){
                umap[d] += a;
            }
        }

        for(auto&[key,value]:umap){
            ans.push_back(to_string(value)+" "+key);
        }

        return ans;
    }
};
17600