CF 593A 2Char

旧城等待, 2022-08-23 04:55 251阅读 0赞

题目网址:http://codeforces.com/problemset/problem/593/A

题目大意:给定n个由小写字母组成的串,从中取任意个字符串拼成一封信,这封信中最多有两种字符,输出信的最大长度。

首先想到了用STL中的各种容器,统计每个字符串含有哪两种字符(或者一种)作为map的first,对应的second是这个字符串,有相同first的字符串合在一起。然后遍历map,对first进行分解得到两种长度为1的字符串,以这两个字符串为first,找出他们的second加到遍历map的second里,最后找出second长度最大的,就是最终结果了。但是还有一些细节问题,比如当n个字符串都为单一一种字符的时候,我都是进行了特判,感觉A的很勉强。后来我在网上查这道题才发现这是一道A题。。。。。。又看了别人的代码,才感觉自己将题目写复杂了,用数组会更方便清晰。

My Code:

  1. #include<stdio.h>
  2. #include<iostream>
  3. #include<set>
  4. #include<map>
  5. #include<string>
  6. using namespace std;
  7. int main()
  8. {
  9. map<string,string> ma;
  10. set<string> se;
  11. string str,str_t,s[101];
  12. int n,i,j,k,maxx;
  13. while(cin>>n)
  14. {
  15. maxx=0;
  16. ma.clear();
  17. for(i=0;i<=100;i++)
  18. s[i].clear();
  19. for(i=0,k=0;i<n;i++)
  20. {
  21. cin>>str;
  22. se.clear();
  23. for(j=0;j<str.size();j++)
  24. {
  25. string ttt;
  26. ttt+=str[j];
  27. se.insert(ttt);
  28. }
  29. if(se.size()>2)
  30. continue;
  31. str_t.clear();
  32. for(set<string>::iterator it=se.begin();it!=se.end();it++)
  33. str_t+=(*it);
  34. if(ma[str_t].size()>0)
  35. ma[str_t]+=str;
  36. else
  37. {
  38. ma[str_t]+=str;
  39. s[k++]=str_t;
  40. }
  41. }
  42. // for(i=0;i<k;i++)
  43. // cout<<s[i]<<endl;
  44. int x=0,y=0,flag=0;
  45. for(i=0;i<k;i++)
  46. {
  47. if(s[i].size()==1)
  48. {
  49. if(ma[s[i]].size()>x)
  50. {
  51. x=ma[s[i]].size();
  52. flag=i;
  53. }
  54. }
  55. }
  56. for(i=0;i<k;i++)
  57. {
  58. if(s[i].size()==1)
  59. {
  60. if(ma[s[i]].size()>y&&i!=flag)
  61. {
  62. y=ma[s[i]].size();
  63. }
  64. int num1=ma[s[i]].size();
  65. if(num1>maxx)
  66. {maxx=ma[s[i]].size();}
  67. }
  68. else
  69. {
  70. string a,b;
  71. a+=s[i][0];
  72. b+=s[i][1];
  73. ma[s[i]]+=ma[a];
  74. ma[s[i]]+=ma[b];
  75. int num2=ma[s[i]].size();
  76. if(num2>maxx)
  77. {maxx=ma[s[i]].size();}
  78. }
  79. }
  80. if(maxx>x+y)
  81. {cout<<maxx<<endl;}
  82. else
  83. {cout<<x+y<<endl;}
  84. }
  85. return 0;
  86. }

Other Code:

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <algorithm>
  5. using namespace std;
  6. char s[1005];
  7. int a[30][30];
  8. int vis[30];
  9. bool cmp(int x,int y)
  10. {
  11. return x>y;
  12. }
  13. int main()
  14. {
  15. int n;
  16. scanf("%d",&n);
  17. while(n--)
  18. {
  19. scanf("%s",s);
  20. memset(vis,0,sizeof(vis));
  21. int len=strlen(s);
  22. int ans=0;
  23. int pos[3];
  24. memset(pos,-1,sizeof(pos));
  25. int i;
  26. for(i=0;i<len;i++)
  27. {
  28. if(!vis[s[i]-‘a‘])
  29. {
  30. vis[s[i]-‘a‘]=1;
  31. pos[ans++]=s[i]-‘a‘;
  32. }
  33. if(ans>2) break;
  34. }
  35. if(i==len) a[pos[0]+1][pos[1]+1]+=len;
  36. }
  37. int cnt=0;
  38. for(int i=1;i<27;i++)
  39. for(int j=i+1;j<27;j++)
  40. {
  41. int sum=0;
  42. sum+=a[i][j];
  43. sum+=a[j][i];
  44. sum+=a[i][0];
  45. sum+=a[j][0];
  46. cnt=max(sum,cnt);
  47. }
  48. cout<<cnt<<endl;
  49. return 0;
  50. }

发表评论

表情:
评论列表 (有 0 条评论,251人围观)

还没有评论,来说两句吧...

相关阅读

    相关 CF#581(Div.2)】A&B

    date:2019.8.20 我第一次打CF的比赛,感觉题目非常新颖并且非常棒,但是由于翻译软件不太给力所以我对题意的理解也不是很透彻,最后我做出了前两道题,感觉还可以吧

    相关 CF1081A

    CF1081A > 题意: > > > 从 ? 开始每次减去一个不是 ?的约数的数,问最小能得到多少? > > 做法: > > > 因为 $ n $ 一