一些题解

题目来源: 鲸歌的博客

题解

这是一道简单的题目,但是朴素的算法是$\text{O}\left( n^2\right)$的,只能过$70\%$的点.如何优化呢?

考虑在什么情况下会使一段区间内出现过的A,B,C数量相等.显然的,设$totA\left[ i\right]$为前i个字符中A出现的次数(以此类推,$totB$和$totC$)

设一段相等的区间起始于$i$,终止于$j$,显然,当且仅当$totA[j]-totA[i]=totB[j]-totB[i]=totC[j]-totC[i]$时,i与j间A,B,C数相等

移项易得$totA[j]-totB[j]=totA[i]-totB[i],totB[j]-totC[j]=totB[i]-totC[i]$,易证该式成立为上式成立充分必要条件.
这两个值拥有相当有趣的推论,即记$ab[i]=totA[i]-totB[i],bc[i]=totB[i]-totC[i]$,
只要任何$i<j$且$ab[i]=ab[j], bc[i]=bc[j]$成立,i~j 间就有相同的A,B,C数目,反之,是不能有相同的A,B,C数目的.

这时算法仍然是$\text{O}\left( n^2\right)$,但是通过组合学的方法,我们可以将所有$ab[i],bc[i]$相同的点分为一类,每类中可行段的总和即是答案.而每类中只要是两两元素间的段
都是可行段,即段数为$\frac{n\left( n-1\right)}{2}$.n只需要扫描到此类节点时自增一即可.

最后问题,如何储存一类的和?使用hash表,平均复杂度接近常数,不过hash函数要写的好些.(有冲突怎么办?记得前向星么?就那么处理)

最后附上渣代码,不过效率比std高多了,std评测415ms,我的代码180ms.(有个神犇同学写的AC代码总共1500 ms...)

#include <cstdio>
#define nxp 1110007
#define par 111029
int tot[3],hash[nxp],t,t2,t3,t4,ln;
char c;
struct nnode{
	int ab,bc,cn,sz;
} nn[2000000];
inline int abs(int n){
	return (n<0?-n:n);
}
int hashF(int ab,int bc){
	return abs((ab*par+bc)%nxp);
}
int find(int ab,int bc){
	t4=hashF(ab,bc);
	t=hash[t4];
	while(nn[t].ab!=ab||nn[t].bc!=bc){
		if(t==0) return ~t4;//-t4-1
		t=nn[t].cn;
	}
	return t;
}
long long sum;
int main(){
	ln=1;
	nn[ln].ab=0;
	nn[ln].bc=0;
	t3=~t3;
	nn[ln].cn=hash[0];
	nn[ln].sz=1;
	hash[0]=ln;
	++ln;
	while(c=getchar(),c!=EOF&&c!=‘\n‘&&c!=‘\0‘){
		t2=c-‘A‘;
		if(t2<3){
			++tot[t2];
		}
		t3=find(tot[1]-tot[0],tot[2]-tot[1]);
		if(t3<0){//if has significate bit true
			nn[ln].ab=tot[1]-tot[0];
			nn[ln].bc=tot[2]-tot[1];
			t3=~t3;
			nn[ln].cn=hash[t3];
			nn[ln].sz=1;
			hash[t3]=ln;
			++ln;
		}else{
			sum+=nn[t3].sz;
			++nn[t3].sz;
		}
	}
	printf("%d\n",sum);
	return 0;
}

最后说一句,千万注意$ab[0]=bc[0]=0$,不然很可能错.(即是我代码main最前面如此伪和的来历).唉,解释了这么多不如代码实在啊...我这个代码用多次加法(每次sum加上此点前符合要求的起点总数)代替乘法,附上乘法版(不过这个优化根本不明显啊...)

#include <cstdio>
#define nxp 1110007
#define par 111029
int tot[3],hash[nxp],t,t2,t3,t4,ln,i;
char c;
struct nnode{
	int ab,bc,cn,sz;
} nn[2000000];
inline int abs(int n){
	return (n<0?-n:n);
}
int hashF(int ab,int bc){
	return abs((ab*par+bc)%nxp);
}
int find(int ab,int bc){
	t4=hashF(ab,bc);
	t=hash[t4];
	while(nn[t].ab!=ab||nn[t].bc!=bc){
		if(t==0) return ~t4;//-t4-1
		t=nn[t].cn;
	}
	return t;
}
long long sum;
int main(){
	ln=1;
	nn[ln].ab=0;
	nn[ln].bc=0;
	t3=~t3;
	nn[ln].cn=hash[0];
	nn[ln].sz=1;
	hash[0]=ln;
	++ln;
	while(c=getchar(),c!=EOF&&c!=‘\n‘&&c!=‘\0‘){
		t2=c-‘A‘;
		if(t2<3){
			++tot[t2];
		}
		t3=find(tot[1]-tot[0],tot[2]-tot[1]);
		if(t3<0){//if has significate bit true
			nn[ln].ab=tot[1]-tot[0];
			nn[ln].bc=tot[2]-tot[1];
			t3=~t3;
			nn[ln].cn=hash[t3];
			nn[ln].sz=1;
			hash[t3]=ln;
			++ln;
		}else{
			++nn[t3].sz;
		}
	}
	for(i=1;i<ln;++i) sum+=(nn[i].sz)*(nn[i].sz-1)>>1;
	printf("%lld\n",sum);
	return 0;
}

代码中nxp和par即为hash函数常数了...这里选用了两个大小刚刚好的素数.~x是-x-1的一个很方便的代替方法(而且非常快).为什么-x-1主要是考虑到-0=0的情况不好分辨.(当然~x比-x快是显然的,无需取反后加一)

一些题解,布布扣,bubuko.com

时间: 2024-08-06 20:07:05

一些题解的相关文章

洛谷 P1079 Vigen&#232;re 密码 题解

此文为博主原创题解,转载时请通知博主,并把原文链接放在正文醒目位置. 题目链接:https://www.luogu.org/problem/show?pid=1079 题目描述 16 世纪法国外交家 Blaise de Vigenère 设计了一种多表密码加密算法――Vigenère 密 码.Vigenère 密码的加密解密算法简单易用,且破译难度比较高,曾在美国南北战争中为 南军所广泛使用. 在密码学中,我们称需要加密的信息为明文,用 M 表示:称加密后的信息为密文,用 C 表示:而密钥是一种

8.8联考题解

今天的T1让我怀疑我是不是在做奥赛题--这考的是什么知识点啊这个,会不会用绝对值函数? Evensgn 的债务 时间限制: 1 Sec  内存限制: 128 MB 题目描述 Evensgn 有一群好朋友,他们经常互相借钱.假如说有三个好朋友A,B,C.A 欠 B 20 元,B 欠 C 20 元,总债务规模为 20+20=40 元.Evensgn 是个追求简约的人,他觉得这样的债务太繁杂了.他认为,上面的债务可以完全等价为 A 欠C20 元,B 既不欠别人,别人也不欠他.这样总债务规模就压缩到了 

POJ 2533 - Longest Ordered Subsequence(最长上升子序列) 题解

此文为博主原创题解,转载时请通知博主,并把原文链接放在正文醒目位置. 题目链接:http://poj.org/problem?id=2533 Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given numeric sequence (a1, a2, ..., aN) be any sequence (ai1, ai2, ..., aiK)

(leetcode题解)Pascal&#39;s Triangle

Pascal's Triangle  Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 题意实现一个杨辉三角. 这道题只要注意了边界条件应该很好实现出来,C++实现如下 vector<vector<int>> generate(int

2017ZZUACM省赛选拔试题部分题解----谨以纪念我这卡线滚粗的美好经历

写在前面: 其实心里有些小小的不爽又有点小小的舒畅,为啥捏?不爽当然是因为没被选拔上啦,舒畅捏则是因为没被选拔上反而让自己警醒,学长也提点很多很多."沉下去,然后一战成名"学长如是对我说,我很开心.其实这完全算不算是题解,只是我个人的一些小想法而已.而且到现在还有一题不会...让自己长点记性吧. 题目 A :聪明的田鼠 Time Limit: 1 Sec Memory Limit: 128 MB Description 田鼠MIUMIU来到了一片农田,农田可以看成是一个M*N个方格的矩

LeetCode-001题解

此题目摘自LeetCode001 Given an array of integers, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2.

leetcode题解: Next Permutation

最近还一直在刷leetcode,当然,更多时候只是将题解写在自己的电脑上,没有分享出来.偶尔想起来的时候,就写出来. public class Solution { public void nextPermutation(int[] nums) { if(nums==null||nums.length<=1) return; nextPermutationHelp( nums,0,nums.length-1); } public void nextPermutationHelp(int []nu

HDU 5014 Number Sequence(2014 ACM/ICPC Asia Regional Xi&#39;an Online) 题解

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5014 Number Sequence Problem Description There is a special number sequence which has n+1 integers. For each number in sequence, we have two rules: ● ai ∈ [0,n] ● ai ≠ aj( i ≠ j ) For sequence a and sequ

HDU 1045 Fire Net 二分图Bipartite题解

本题可以使用DFS直接爆搜出答案,不过这样类型的题目其实是个二分图的题解. 这个二分图,难不在Hungary算法,而是难在于建图.需要挺高的抽象思维的. 建图: 1 把同一行不被X分开的格子标同一个号码,被X分开的标下一个号码,这样做是为了缩点,不需要把所有的格子都分开标号,而且可以更方便建个更加小的图. 2 同理把同一列的格子标号 3 然后判断相同一个格子的行标号和列标号是有路径的,其他不在同一个格子的都是没有路径的. 4 这样就等于以行标号和列标号作为左右顶点,构建成一个二分图了 然后使用H

POJ 2386 Lake Counting 搜索题解

简单的深度搜索就可以了,看见有人说什么使用并查集,那简直是大算法小用了. 因为可以深搜而不用回溯,故此效率就是O(N*M)了. 技巧就是增加一个标志P,每次搜索到池塘,即有W字母,那么就认为搜索到一个池塘了,P值为真. 搜索过的池塘不要重复搜索,故此,每次走过的池塘都改成其他字母,如'@',或者'#',随便一个都可以. 然后8个方向搜索. #include <stdio.h> #include <vector> #include <string.h> #include