hdu5583

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <string.h>
 4 #include <math.h>
 5 
 6 char str[100001];
 7 ///注意在ans+=f[g]*f[g];中,ans是long long,所以运算的两个数至少有一个数是long long,所以f数组选择的类型是长整形
 8 long long f[100001];
 9 
10 int main()
11 {
12     long n,g,pre,len,i,j;
13     long long ans,t,s;
14     scanf("%ld",&n);
15     for (j=1;j<=n;j++)
16     {
17         g=0;
18         pre=-1;
19         ans=0;
20         scanf("%s",str);
21         len=strlen(str);
22         for (i=0;i<len;i++)
23             if (str[i]!=str[i+1])
24             {
25                 g++;
26                 f[g]=i-pre;
27                 ans+=f[g]*f[g];
28                 pre=i;
29             }
30         if (g==1)
31             printf("Case #%ld: %lld
",j,ans);
32         else
33         {
34             t=0;
35             for (i=1;i<g;i++)
36             {
37                 //(f[x]+1)*(f[x]+1)+(f[y]-1)*(f[y]-1)-f[x]*f[x]-f[y]*f[y]=2*f[x]-2*f[y]+2
38                 s=abs(f[i]-f[i+1]);
39                 if (s>t)
40                     t=s;
41             }
42             t=t*2+2;
43             for (i=2;i<g;i++)
44                 if (f[i]==1)
45                 {
46                     s=(f[i-1]+1+f[i+1])*(f[i-1]+1+f[i+1])-f[i-1]*f[i-1]-1-f[i+1]*f[i+1];
47                     if (s>t)
48                         t=s;
49                 }
50             printf("Case #%ld: %lld
",j,ans+t);
51         }
52     }
53     return 0;
54 }
原文地址:https://www.cnblogs.com/cmyg/p/7208556.html