Helvetic Coding Contest 2018 D1(分数类的使用 && hash)

problem

The Rebel fleet is on the run. It consists of m ships currently gathered around a single planet. Just a few seconds ago, the vastly more powerful Empire fleet has appeared in the same solar system, and the Rebels will need to escape into hyperspace. In order to spread the fleet, the captain of each ship has independently come up with the coordinate to which that ship will jump. In the obsolete navigation system used by the Rebels, this coordinate is given as the value of an arithmetic expression of the form .

To plan the future of the resistance movement, Princess Heidi needs to know, for each ship, how many ships are going to end up at the same coordinate after the jump. You are her only hope!

Input

The first line of the input contains a single integer m (1 ≤ m ≤ 200 000) – the number of ships. The next m lines describe one jump coordinate each, given as an arithmetic expression. An expression has the form (a+b)/c. Namely, it consists of: an opening parenthesis (, a positive integer a of up to two decimal digits, a plus sign +, a positive integer b of up to two decimal digits, a closing parenthesis ), a slash /, and a positive integer c of up to two decimal digits.

Output

Print a single line consisting of m space-separated integers. The i-th integer should be equal to the number of ships whose coordinate is equal to that of the i-th ship (including the i-th ship itself).

Example

input

4
(99+98)/97
(26+4)/10
(12+33)/15
(5+1)/7

output

1 2 2 1

Note

In the sample testcase, the second and the third ship will both end up at the coordinate 3.

Note that this problem has only two versions – easy and hard.

solution

使用分数类+字符串hash判重

code example

#include
using namespace std;
typedef long long ll;
int debug_num=0;
#define debug cout<<"debug "<<++debug_num<<" :"

unordered_map<string,int> mp;

const int maxn=2e5+10;

ll gcd(ll a,ll b){
    if(b==0) return a;
    return gcd(b,a%b);
}

struct Fraction{
    ll num;
    ll den;
    Fraction (ll num=0,ll den=1){
        if(den<0){
            num=-num;
            den=-den;
        }
        assert(den != 0);
        ll g=gcd(abs(num),den);
        this->num=num/g;
        this->den=den/g;
    }
    Fraction operator + (const Fraction &o) const{
        return Fraction(num*o.den+den*o.num,den*o.den);
    }
    Fraction operator - (const Fraction &o) const{
        return Fraction(num*o.den-den*o.num,den*o.den);
    }
    Fraction operator * (const Fraction &o) const{
        return Fraction(num*o.num,den*o.den);
    }
    Fraction operator / (const Fraction &o) const{
        return Fraction(num*o.den,den*o.num);
    }
    bool operator < (const Fraction &o) const{
        return num*o.denbool operator == (const Fraction &o) const{
        return num*o.den==den*o.num;
    }
}no[maxn];



int main()
{
    //freopen("in.txt","r",stdin);
    //ios::sync_with_stdio(false);
    int m;
    cin>>m;
    //scanf("%d",&m);
    for(int i=0;i//scanf("(%d+%d)/%d",&a,&b,&c);
        //cout<
        string s;
        cin>>s;
        ll sum=0;
        int j;
        for(j=1;jif(s[j]=='+') break;
            sum=sum*10;
            sum+=s[j]-'0';
        }
        a=sum;
        sum=0;
        j++;
        for(;jif(s[j]==')') break;
            sum=sum*10;
            sum+=s[j]-'0';
        }
        b=sum;
        sum=0;
        j=j+2;
        for(;j10;
            sum+=s[j]-'0';
        }
        c=sum;
        no[i]=Fraction(a+b,c);
        s="";
        int tp=no[i].num;
        while(tp){
            s+=tp%10+'0';
            tp/=10;
        }
        s+='_';
        tp=no[i].den;
        while(tp){//逆序就逆序吧...
            s+=tp%10+'0';
            tp/=10;
        }
        mp[s]++;
        //cout<
    }
    for(int i=0;istring s="";
        int tp=no[i].num;
        while(tp){
            s+=tp%10+'0';
            tp/=10;
        }
        s+='_';
        tp=no[i].den;
        while(tp){//逆序就逆序吧...
            s+=tp%10+'0';
            tp/=10;
        }
        cout<" ";
    }
    return 0;
}

你可能感兴趣的:(Basic,Algorithm)