# lintcode1006. 子域名访问计数

样例

["9001 discuss.lintcode.com"]

["9001 discuss.lintcode.com", "9001 lintcode.com", "9001 com"]

["900 google.mail.com", "50 yahoo.com", "1 intel.mail.com", "5 wiki.org"]

["901 mail.com","50 yahoo.com","900 google.mail.com","5 wiki.org","5 org","1 intel.mail.com","951 com"]

cpdomains的长度不超过100.


class Solution {
public:
/**
* @param cpdomains: a list cpdomains of count-paired domains
* @return: a list of count-paired domains
*/
vector<string> subdomainVisits(vector<string> &cpdomains) {
map<string,int> count;//统计子域名出现次数
vector<string>res;
for (int i = 0; i < cpdomains.size(); i++) {
/* code */
int sum=0;//在域名中访问次数
for (int j = 0; cpdomains[i][j]!=' '; j++) {//得到域名访问次数
/* code */
if(isdigit(cpdomains[i][j])) sum=sum*10+cpdomains[i][j]-'0';
}
vector<string> tmp=str(cpdomains[i]);//获得该域名的所有子域名
for (int k = 0; k < tmp.size(); k++) {//统计子域名出现次数
/* code */
count[tmp[k]]+=sum;
}
}
for (auto i : count) {//调整输出格式
/* code */
string tmp=to_string(i.second);
tmp=tmp+' '+i.first;
res.push_back(tmp);
}
return res;
}
vector<string> str(string tmp)//从后往前遍历得到所有子域名
{
vector<string>res;
for (int i = tmp.size()-1; i >= 0; i--) {
/* code */
else if(tmp[i]=='.'){