Codeforces Round #621 (Div. 1 + Div. 2)
[codeforces 1304C] Cow and Friend 26个字母中的某个字母前缀和+(26个字母中的某个字母)之前的(26个字母中的某个字母)的数量+int溢出
总目录详见https://blog.csdn.net/mrcrack/article/details/103564004
在线测评地址https://codeforces.ml/contest/1307/problem/C
Problem | Lang | Verdict | Time | Memory |
---|---|---|---|---|
C - Cow and Message | GNU C++11 | Accepted | 46 ms | 10500 KB |
26个字母中的某个字母前缀和+(26个字母中的某个字母)之前的((26个字母中的某个字母)的数量+int溢出
提一句,题中的思路是给手工算算的,要给计算机 算,还得读者自己再归纳。
因要满足选出的序列,字母位置序列构成等差数列。
对多出现的字串,要么是单个字符,要么是2个字符。因为可以随心所欲的取。
int溢出,情况如下
a(5*10^4个)b(5*10^4个) ab选取数量(5*10^4)*(5*10^4)=2.5*10^9 int溢出
int sum[27][maxn];//26个字母中的某个字母前缀和
LL ans[27][27];//ans[i][j] i字母之前 j字母的个数//(26个字母中的某个字母)之前的((26个字母中的某个字母)的数量
#include <stdio.h>
#include <string.h>
#define maxn 100010
#define LL long long
int sum[27][maxn];
char s[maxn];
LL ans[27][27];//ans[i][j] i字母之前 j字母的个数
LL max(LL a,LL b){
return a>b?a:b;
}
int main(){
int i,len,j,k;
LL mx=-1;
scanf("%s",s+1);
len=strlen(s+1);
for(i=1;i<=len;i++)
for(j=0;j<26;j++)
if(j==s[i]-'a')sum[j][i]=sum[j][i-1]+1;
else sum[j][i]=sum[j][i-1];
for(i=1;i<=len;i++)
for(j=0;j<26;j++)
ans[s[i]-'a'][j]+=sum[j][i-1];
for(i=0;i<26;i++)
for(j=0;j<26;j++)
mx=max(mx,ans[i][j]);
for(i=0;i<26;i++)mx=max(mx,sum[i][len]);
printf("%lld\n",mx);
return 0;
}