【codeforces 509E】Pretty Song

【题目链接】:http://codeforces.com/contest/509/problem/E

【题意】

让你计算一个字符串的所有子串里面元音字母出现的频率的和;

【题解】

先处理出前缀和->pre[i]->前i个字母里面元音字母的个数;
设ans[i]
表示长度为i的子串出现的元音字母的个数(重复的也要算进去);
注意到以下事实
长度为2的子串中第一个和最后一个字母出现的次数为1,其他为2;
长度为3的子串中第一个和最后一个字母出现的次数为1,第二个和最后第二个字母出现的次数为2,其他为3;
。。。
则能写出递推式
ans[i]=ans[i-1]+pre[n-i+1]-pre[i-1];
然后输出∑ans[i]/i就好

【Number Of WA

0

【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define ps push_back
#define fi first
#define se second
#define rei(x) cin >> x
#define pri(x) cout << x
#define ms(x,y) memset(x,y,sizeof x)

typedef pair<int,int> pii;
typedef pair<LL,LL> pll;

const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 5e5+100;

char s[N];
int pre[N],n;
map<char,int> dic;
double ans[N],temp=0;

int main()
{
    //freopen("F:\\rush.txt","r",stdin);
    ios::sync_with_stdio(false);
    dic['I']=dic['E']=dic['A']=dic['O']=dic['U']=dic['Y'] = 1;
    rei((s+1));
    n = strlen(s+1);
    rep1(i,1,n)
        if (dic[s[i]])
            pre[i] = pre[i-1]+1;
        else
            pre[i] = pre[i-1];
    ans[1] = pre[n];
    rep1(i,2,n)
        ans[i] = ans[i-1]+pre[n-i+1]-pre[i-1];
    rep1(i,1,n)
    {
        double j = i;
        temp+=ans[i]/j;
    }
    printf("%.10f\n",temp);
    //printf("\n%.2lf sec \n", (double)clock() / CLOCKS_PER_SEC);
    return 0;
}
posted @ 2017-10-04 18:44  AWCXV  阅读(96)  评论(0编辑  收藏  举报