思维题----CodeForces - 1178B

题目:
Recall that string a is a subsequence of a string b if a can be obtained from b by deletion of several (possibly zero or all) characters. For example, for the string a="wowwo", the following strings are subsequences: "wowwo", "wowo", "oo", "wow", "", and others, but the following are not subsequences: "owoo", "owwwo", "ooo".

The wow factor of a string is the number of its subsequences equal to the word "wow". Bob wants to write a string that has a large wow factor. However, the "w" key on his keyboard is broken, so he types two "v"s instead.

Little did he realise that he may have introduced more "w"s than he thought. Consider for instance the string "ww". Bob would type it as "vvvv", but this string actually contains three occurrences of "w":

"vvvv"
"vvvv"
"vvvv"
For example, the wow factor of the word "vvvovvv" equals to four because there are four wows:

"vvvovvv"
"vvvovvv"
"vvvovvv"
"vvvovvv"
Note that the subsequence "vvvovvv" does not count towards the wow factor, as the "v"s have to be consecutive.

For a given string s, compute and output its wow factor. Note that it is not guaranteed that it is possible to get s from another string replacing "w" with "vv". For example, s can be equal to "vov".

Input
The input contains a single non-empty string s, consisting only of characters "v" and "o". The length of s is at most 106.

Output
Output a single integer, the wow factor of s.

Examples
Input
vvvovvv
Output
4
Input
vvovooovovvovoovoovvvvovovvvov
Output
100
Note
The first example is explained in the legend.
题目大意:
对于每一个o点,找出o点左面有几个w,(没两个v可以构成一个w);
再找出每个o点的右面有几个w,
然后对于每一个o,求出 左面w的个数*右面w的个数 ,最后加起来所有的o的乘积。
这是一个很简单的思维题,可一直wrong。
第一种思路:
对于每个点,从头跑一边,看它前面有几个符合要求的
然后从末尾再跑一边,看它后面有几个符合要求的,
最后算乘积再加和。
(可这种方案一直wrong)
第二种思路:**感谢px哥哥提供的思路。
对于每个点,从头跑一边,记录它前面有多少符合要求的,并且记录一共有多少符合要求的w。
那么对于每个点,它  后面符合的w的点的数目  ==  总的符合w的数目 -- 前面符合w的数目。

//线性问题,统计出每个点左面有多少点
//对于每个点,总的减去它左面的点就是要求的右面的点。 
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
char str[1000003];
int date[1000006];
int num[1000003];
int main()
{
	for(int i=2;i<=1000000;i++)
	{
		num[i]=i-1;
	}
	
	scanf("%s",str);
	int qq=strlen(str);
	int c1=0;
	int k=0;
	long long sum=0;
	for(int i=0;i<qq;i++)
	{
		c1=0;
		while(str[i]=='v'&&i<qq)
		{
			c1++;
			i++;
		}
		
		sum+=num[c1];
		
		if(str[i]=='o')
		{
			++k;
			date[k]=num[c1]+date[k-1];
		
		}
	}
	
	long long ans=0;
	for(int i=1;i<=k;i++)
	{
		ans+=(date[i]*(sum-date[i]));
	}

	printf("%lld\n",ans);
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值