Advertisement
Saleh127

UVA 10336

Jun 20th, 2021
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.64 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4. #define test int t; cin>>t; for(int cs=1;cs<=t;cs++)
  5. ll n,m;
  6. char a[508][508];
  7. bool v[600][600];
  8. char ss;
  9. bool check(ll i,ll j)
  10. {
  11. if(i>=0 && i<n && j>=0 && j<m && a[i][j]==ss && v[i][j]==0) return true;
  12. return false;
  13. }
  14.  
  15. void dfs(ll i,ll j)
  16. {
  17. v[i][j]=1;
  18.  
  19. if(check(i+1,j)) dfs(i+1,j);
  20. if(check(i-1,j)) dfs(i-1,j);
  21. if(check(i,j+1)) dfs(i,j+1);
  22. if(check(i,j-1)) dfs(i,j-1);
  23.  
  24. }
  25.  
  26. bool cmp(pair<char,ll>a,pair<char,ll>b)
  27. {
  28. return (a.second>b.second || (a.second==b.second && a.first<b.first));
  29. }
  30.  
  31. int main()
  32. {
  33. ios_base::sync_with_stdio(0);
  34. cin.tie(0);cout.tie(0);
  35.  
  36. test
  37. {
  38. ll i,j,k,l;
  39. cin>>n>>m;
  40.  
  41. for(i=0;i<n;i++)
  42. {
  43. for(j=0;j<m;j++)
  44. {
  45. cin>>a[i][j];
  46. }
  47. }
  48.  
  49. map<char,ll>f;
  50.  
  51. for(i=0;i<n;i++)
  52. {
  53. for(j=0;j<m;j++)
  54. {
  55. if(v[i][j]==0)
  56. {
  57. ss=a[i][j];
  58. dfs(i,j);
  59. f[a[i][j]]++;
  60. }
  61. }
  62. }
  63.  
  64. vector<pair<char,ll>>x;
  65.  
  66. for(char w='a';w<='z';w++)
  67. {
  68. if(f[w]>0)
  69. {
  70. x.push_back({w,f[w]});
  71. }
  72. }
  73. sort(x.begin(),x.end(),cmp);
  74.  
  75. cout<<"World #"<<cs<<endl;
  76.  
  77. for(i=0;i<x.size();i++)
  78. {
  79. cout<<x[i].first<<": "<<x[i].second<<endl;
  80. }
  81.  
  82. memset(v,0,sizeof v);
  83. }
  84.  
  85. return 0;
  86. }
  87.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement