zoukankan      html  css  js  c++  java
  • CodeForces

    Pushok the dog has been chasing Imp for a few hours already.

    Fortunately, Imp knows that Pushok is afraid of a robot vacuum cleaner.

    While moving, the robot generates a string t consisting of letters 's' and 'h', that produces a lot of noise. We define noise of string t as the number of occurrences of string "sh" as a subsequence in it, in other words, the number of such pairs (i, j), that i < j and  and .

    The robot is off at the moment. Imp knows that it has a sequence of strings ti in its memory, and he can arbitrary change their order. When the robot is started, it generates the string t as a concatenation of these strings in the given order. The noise of the resulting string equals the noise of this concatenation.

    Help Imp to find the maximum noise he can achieve by changing the order of the strings.

    Input

    The first line contains a single integer n (1 ≤ n ≤ 105) — the number of strings in robot's memory.

    Next n lines contain the strings t1, t2, ..., tn, one per line. It is guaranteed that the strings are non-empty, contain only English letters 's' and 'h' and their total length does not exceed 105.

    Output

    Print a single integer — the maxumum possible noise Imp can achieve by changing the order of the strings.

    Examples

    Input
    4
    ssh
    hs
    s
    hhhs
    Output
    18
    Input
    2
    h
    s
    Output
    1

    题意:
    给定n个字符串自由组合,求最多出现多少个sh这样的子序列。
    思路:
    对于s1,s2,产生字符串的个数就是S1.SH+s2*SH+max(s1.S*s2.H+s2.S*s1.H)
    所以按照s1.S*s2.H-s2.S*s1.H排序即可。
    #include<iostream>
    #include<algorithm>
    #include<vector>
    #include<stack>
    #include<queue>
    #include<map>
    #include<set>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<ctime>
    #define fuck(x) cout<<#x<<" = "<<x<<endl;
    #define debug(a,i) cout<<#a<<"["<<i<<"] = "<<a[i]<<endl;
    #define ls (t<<1)
    #define rs ((t<<1)+1)
    using namespace std;
    typedef long long ll;
    typedef unsigned long long ull;
    const int maxn = 100086;
    const int maxm = 100086;
    const int inf = 2.1e9;
    const ll Inf = 999999999999999999;
    const int mod = 1000000007;
    const double eps = 1e-6;
    const double pi = acos(-1);
    
    struct node{
        ll S,H;
    }a[maxn];
    
    char s[maxn];
    
    bool cmp(node a,node b){
        return a.S*b.H>a.H*b.S;
    }
    
    int main()
    {
    //    ios::sync_with_stdio(false);
    //    freopen("in.txt","r",stdin);
    
        int n;
        scanf("%d",&n);
        ll ans=0;
        for(int i=1;i<=n;i++){
            scanf("%s",s);
            for(int j=0;s[j];j++){
                if(s[j]=='s'){a[i].S++;}
                else{
                    ans+=a[i].S;
                    a[i].H++;
                }
            }
        }
    
        sort(a+1,a+1+n,cmp);
    
    
        ll tmp=0;
        for(int i=1;i<=n;i++){
    
            ans+=a[i].H*tmp;
            tmp+=a[i].S;
        }
        printf("%lld
    ",ans);
    
        return 0;
    }
    View Code
  • 相关阅读:
    黄聪:PHP获取MAC地址(转)
    黄聪:HTC同步出现太多联系人删除项解决方法
    黄聪:WordPress Cron 定时任务(转)
    黄聪:wordpress后台发布文章,自定义栏目和字段
    黄聪:如何在wordpress任何一个页面调用“插入图片”按钮,实现上传图片功能
    黄聪:获取搜索引擎来源访客的搜索关键词
    黄聪:用php判断当前用户访问网站是否为手机登录
    黄聪:php计算获取页面执行时间
    黄聪:抓取网页链接的php类:snoop
    黄聪:解决wordpress定时发布文章失败”丢失计划任务”的插件
  • 原文地址:https://www.cnblogs.com/ZGQblogs/p/10848108.html
Copyright © 2011-2022 走看看