[codeforces 1287B] Hyperset 字符串+排序+二分

[codeforces 1287B] Hyperset 字符串+排序+二分.net

总目录详见https://blog.csdn.net/mrcrack/article/details/103564004code

在线测评地址https://codeforces.com/contest/1287/problem/Bblog

 

Problem Lang Verdict Time Memory
B - Hyperset GNU C++11 Accepted 529 ms 0 KB

思考过程如图所示排序

基本思路:枚举2个字串,算出符合条件的第3个字串。3个字串上同一位置的规律应知足上图条件。算出的第3个字串,在原数据中进行二分查找,若找到,则组合书加1。字符串

注意,1,2,3串;运算过程为1,2,3;   1,3,2;   2,3,1;故最后结果别忘了除3.     2019-1-6 9:47get

#include <stdio.h>
#include <string.h>
int n,k,tot;
char s[1510][35],t[35];
int c2i(char c){
	if(c=='E')return 1;
	else if(c=='S')return 2;
	else if(c=='T')return 3;
}
int main(){
	int i,j,a,b,c,p;
	scanf("%d%d",&n,&k);
	for(i=1;i<=n;i++)scanf("%s",s[i]);
	for(i=1;i<=n;i++)
		for(j=i+1;j<=n;j++)
			if(strcmp(s[i],s[j])>0)
				strcpy(t,s[i]),strcpy(s[i],s[j]),strcpy(s[j],t);
	for(i=1;i<=n;i++)
		for(j=i+1;j<=n;j++){
			for(p=0;p<k;p++){
				a=c2i(s[i][p]),b=c2i(s[j][p]);
				c=a+b;
				if(c==2||c==5)t[p]='E';
				else if(c==4)t[p]='S';
				else if(c==3||c==6)t[p]='T';
			}
			t[k]='\0';
			int left=1,right=n+1,mid;
			while(left+1<right){
				mid=(left+right)/2;
				if(strcmp(s[mid],t)>0)right=mid;
				else left=mid;
			}
			if(strcmp(s[left],t)==0)tot++;
		}
	printf("%d\n",tot/3);
	return 0;
}