CF 294 div2 D. A and B and Interesting Substrings (hash)

题目:http://codeforces.com/contest/519/problem/D

题意:在字符串里面找出有多少子串除首尾字符,其权值的和为0,且首尾字符相同。

分析:将字符和前缀和插入hash表,然后查询字符前缀和,注意去重。

代码:

#include <iostream>
#include <cstdio>

using namespace std;
typedef long long LL;

#define MOD 300007
#define MAXN 1000000

struct node
{
	LL per_sum;
	char ch;
	LL count;
	int next;
};

struct Hash
{
	int Table[MOD],cnt;
	node List[MAXN];
	
	void Clear()
	{
		cnt=0;
		for(int i=0;i<MOD;i++)
			Table[i]=-1;
	}
	void Insert(LL sum,char ch)
	{
		LL hash=sum+ch;
		if(hash<0)
			hash=-hash;
		hash=hash%MOD;
		LL temp=hash;
		temp=Table[hash];
		while(temp!=-1)
		{
			if(List[temp].per_sum==sum && List[temp].ch==ch)
			{
				List[temp].count++;
				return ;
			}
			temp=List[temp].next;
		}
		List[cnt].ch=ch;
		List[cnt].per_sum=sum;
		List[cnt].count=1;
		List[cnt].next=Table[hash];
		Table[hash]=cnt;
		cnt++;
	}
	LL Search(LL sum,char ch)
	{
		LL hash=sum+ch;
		if(hash<0)
			hash=-hash;
		hash=hash%MOD;
		hash=Table[hash];
		while(hash!=-1)
		{
			if(List[hash].ch==ch && List[hash].per_sum==sum)
				return List[hash].count;
			hash=List[hash].next;
		}
		return 0;
	}
}H;

char str[100010];
LL sum[1000010];

int main()
{
	H.Clear();
	int weight[30],i,j,k;
	LL ans=0;
	for(i=0;i<26;i++)
		scanf("%d",&weight[i]);
	scanf("%s",str);
	
	sum[0]=weight[str[0]-'a'];
	for(i=1;str[i]!='\0';i++)
		sum[i]=sum[i-1]+weight[str[i]-'a'];

	H.Insert(sum[0],str[0]);
	for(i=1;str[i]!='\0';i++)
	{
		ans+=H.Search(sum[i-1],str[i]);
		H.Insert(sum[i],str[i]);
	}
	cout<<ans<<'\n';
	return 0;
}

map版,更清爽,不过慢一点

#include <iostream>
#include <map>
#include <cstdio>

using namespace std;
typedef long long LL;

char str[100010];
int main()
{
	map <LL,int> mp[26];
	LL s[26],i,j,ans=0,sum=0;
	for(i=0;i<26;i++)
		cin>>s[i];
	scanf("%s",str);
	for(i=0;str[i];i++)
	{
		ans+=mp[str[i]-'a'][sum];
		sum+=s[str[i]-'a'];
		mp[str[i]-'a'][sum]++;
	}
	cout<<ans<<"\n";
	return 0;
}


The `implode()` function in PHP is used to join elements of an array into a string with a specified separator. It takes two parameters: the first parameter is the separator that will be used to join the array elements, and the second parameter is the array of elements to be joined. The function returns a string that is formed by joining the elements of the array with the specified separator. For example, if we have an array of strings like this: ``` $arr = array('apple', 'banana', 'pear'); ``` We can join the elements of the array into a string using the `implode()` function like this: ``` $str = implode(',', $arr); ``` This will create a string like this: ``` "apple,banana,pear" ``` The `explode()` function, on the other hand, is used to split a string into an array of substrings using a specified separator. It takes two parameters: the first parameter is the separator that will be used to split the string, and the second parameter is the string to be split. The function returns an array of substrings. For example, if we have a string like this: ``` $str = "apple,banana,pear"; ``` We can split the string into an array of substrings using the `explode()` function like this: ``` $arr = explode(',', $str); ``` This will create an array like this: ``` array('apple', 'banana', 'pear') ``` So, basically, the `implode()` function joins an array of elements into a string using a specified separator, while the `explode()` function splits a string into an array of substrings using a specified separator.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值