2021-09-13 异或前缀和判最大回文子串(字母顺序可自排)

D . Beer Mugs [ 问题 7823 ] [ 讨论 ]
Description
Damian is a beer mug collector. His collection fills most of the shelves in his vintage wooden cabinet where all mugs are proudly displayed. The mugs are of various brands. There might be, and often are, more mugs of the same brand in the collection.

Mugs on a shelf in Damian’s collection always form a single symmetric row. Specifically, the symmetry of the row means that the sequence of particular mug brands in the row is the same when the mugs are being admired one by one from left to right and when the mugs are being admired one by one from right to left. There is still one empty shelf in the cabinet and Damian looks for an opportunity to fill it with a new set of mugs.

The widely recognized Mastodon brewery (admired for its Woolly Mammoth beer) organizes annually the so-called beer season. Participants of the season are engaged in daily beer brewing activities and are rewarded each day by a special collector mug. Each day in the season is assigned a particular mug brand. The mug brands for all days in the season are known in advance, some brands may appear repeatedly in the season.

A participant may subscribe for the whole season or just for a part of the season. However, all days of his or her participation have to be in one uninterrupted sequence of days, a participant cannot leave the season and then come back again after some days of absence.

Damian is keen to take part in the beer season. He decided that the set of mugs he brings home should be suited for his display without adding or removing any mug, and that the set should be as big as possible.

Given the list of mug brands provided by the brewery for all days in the beer season, find the size of the biggest set of mugs suitable for Damian’s display which can be obtained by subscribing to some appropriately chosen part of the beer season.

Input
The first input line contains integer N (1<N≤300000) the number of days in the brewery beer season. The next line contains N characters representing the list of all beer mug brands offered in the season, day by day. The list is naturally ordered from the first day to the last day of the season. Each brand is coded by a single lower case letter, from ’a’ to ’t’. The list contains no blanks.

Output
Print a single integer representing the size of the biggest set of mugs which Damian can bring home from the brewery beer season and which is suitable, without changes, for his collection display.

Samples
Input 复制
6
abcabc
Output
6
Input 复制
20
ghjahjghsajdjhlfslja
Output
7
Input 复制
12
aabbccddabcd
Output
9
整体的代码是看了别人的题解;
https://blog.csdn.net/qq_44555205/article/details/104504248?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522163152791816780255220537%2522%252C%2522scm%2522%253A%252220140713.130102334…%2522%257D&request_id=163152791816780255220537&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2allsobaiduend~default-1-104504248.pc_search_all_es&utm_term=D+.+Beer+Mugs+&spm=1018.2226.3001.4187
大佬博客。
首先map.count()是查找一个书是否在map里出现过。故map[0]=0;并不是只是赋初值之意,是先吧‘0’放入map方便之后查询。其次一个数出现偶数次会使异或前缀和为零其实就是一个数异或两次抵消掉了(我是这么理解的)

#include<bits/stdc++.h>
using namespace std;
char s[300050];
map<int,int> mp;
int ans;
int main(){
	int n;
	scanf("%d %s",&n,&s);
	int t=0;
	mp[0]=0;
	for(int i=0;i<n;i++){
		  t^=1<<(s[i]-'a');
		  if(!mp.count(t)) mp[t]=i+1;
		  else  ans=max(ans,i+1-mp[t]); 
		  for(int j=0;j<20;j++){
		  	  if(mp.count(t^(1<<j))){
		  	  	   ans=max(ans,i+1-mp[t^(1<<j)]);
				}
		  }
	}
      printf("%d\n",ans);
	 return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值