Codeforces Round #479 (Div. 3) B. Two-gram
程序员文章站
2022-06-05 13:09:10
...
B. Two-gram
题目链接-B. Two-gram
题目大意
给你一个字符串,找出现次数最多的长度为2的子串
解题思路
- 开一个二维数组
t[][]
,用下标分别记录子串的两个字母,t[a][b]++
来计数 - 然后用两层for循环遍历该二维数组求最大值,并记录最大值对应的两个字符
- 具体操作见代码
附上代码
#include<bits/stdc++.h>
using namespace std;
int t[31][31];
int main(){
int n;
string s;
cin>>n>>s;
for(int i=0;i<n-1;i++){
int a=s[i]-'A';
int b=s[i+1]-'A';
t[a][b]++;
}
int max=0;
int x,y;
for(int j=0;j<26;j++){
for(int k=0;k<26;k++){
if(t[j][k]>max){
x=j;
y=k;
max=t[j][k];
}
}
}
cout<<char(x+'A')<<char(y+'A')<<endl;
return 0;
}
推荐阅读
-
Codeforces Round #595 (Div. 3)D1D2 贪心 STL
-
Codeforces Round #656 (Div. 3)D. a-Good String(递归+dfs)
-
CodeForces 1324 - Codeforces Round #627 (Div. 3)
-
Codeforces Round #649 (Div. 2)-B. Most socially-distanced subsequence(思维)
-
Codeforces Round #650 (Div. 3) B. Even Array
-
Codeforces Round #686 (Div. 3) A. Special Permutation
-
A. Add Odd or Subtract Even(思维题) Codeforces Round #624 (Div. 3)
-
Codeforces Round #461 (Div. 2) B. Magic Forest(异或的性质)
-
Codeforces Round #656 (Div. 3) (C、D题)
-
Codeforces Round #686 (Div. 3) F. Array Partition