类哈希计数
1.Counting Roads - AtCoder abc061_b - Virtual Judge (vjudge.net)
利用数组的值去替换数组的下标来简化计数过程

1 #include <bits/stdc++.h> 2 using namespace std; 3 4 int n,m,a[51],b[51],c[51] = {0}; 5 6 int main(){ 7 cin >> n >> m; 8 for (int i = 1; i <= m; i ++ ) 9 { 10 cin >> a[i] >> b[i]; 11 c[a[i]] += 1; c[b[i]]+= 1; 12 } 13 14 for (int i = 1; i <= n; i ++ ) 15 cout << c[i] << endl; 16 return 0; 17 }
2.哈希计数Poll - AtCoder abc155_c - Virtual Judge (vjudge.net)

1 #include<bits/stdc++.h> 2 using namespace std; 3 int n; 4 5 int main() 6 { 7 ios::sync_with_stdio(false); 8 cin.tie(0); 9 cout.tie(0); 10 map<string , int>mp; 11 set<string>st; 12 cin >> n; 13 string s; 14 int maxn = 0; 15 for(int i = 1; i <= n ; i ++) 16 { 17 cin >> s; 18 mp[s] ++; 19 maxn = max(maxn , mp[s]); 20 } 21 for(auto t : mp) 22 { 23 if(t.second == maxn) 24 { 25 st.insert(t.first); 26 } 27 } 28 for(auto t : st) 29 { 30 cout << t << '\n'; 31 } 32 return 0; 33 }
3.Double Strings - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
枚举三个字符串,一看数据范围1 <= n <= 1e5, 时间复杂度为O(ne3),暴力直接TLE送走
采用优化做法 :字符串较短,我们可以依次枚举分割点,判断分割点左右的字符串是否存在,如果都存在,那么说明可以组合成当前字符串

1 #include<bits/stdc++.h> 2 using namespace std; 3 4 string a[100001]; 5 int t,n,ans; 6 map<string,bool>mp; 7 8 int main() 9 { 10 cin >> t; 11 12 while(t -- ) 13 { 14 cin >> n; 15 for (int i = 1; i <= n; i ++ ) cin >> a[i]; 16 for (int i = 1; i <= n; i ++ ) mp[a[i]] = true; //标记出现过的字符串数组 17 18 for (int i = 1; i <= n; i ++ ) //枚举每个字符串数组 19 { 20 ans = 0; //如果不能配对成功则输出 0 21 for (int j = 0; j < a[i].size(); j ++ ) //枚举每个字符串的断点 22 if(mp[a[i].substr(0,j)] && mp[a[i].substr(j,a[i].size() - j)]) //如果断点两侧都true,则可以配对成功 23 { 24 ans = 1; //标记成功则输出 1; 25 break; 26 } 27 cout << ans ; 28 } 29 cout << endl; 30 } 31 }
4.Problem - 1669E - Codeforces 用a数组来分别计算字符串0位和1位的字符,并转换为数字,
再用c数组来计算这个字符串出现的次数,然后分别枚举,第一位相同,第二位不同和第二位相同,第一位不同的情况

1 #include<bits/stdc++.h> 2 using namespace std; 3 #define endl '\n' 4 #define ll long long 5 #define cy cout << "YES" << endl 6 #define cn cout << "NO" << endl 7 int _,n,m; 8 const int N = 1e5 + 10,inf = 1e9; 9 const int mod = 1e9 + 7; 10 int a[N][2],c[12][12]; 11 ll ans; 12 13 int main() 14 { 15 cin >> _; 16 while(_ -- ) 17 { 18 cin >> n; 19 memset(c, 0, sizeof c); 20 ans = 0; 21 for (int i = 1; i <= n; i ++ ){ 22 char x,y; 23 cin >> x >> y; 24 a[i][0] = x - 'a'; 25 a[i][1] = y - 'a'; 26 c[a[i][0]][a[i][1]] ++; //累加出现次数 27 } 28 29 for (int i = 1; i <= n; i ++ ){ 30 for (int j = 0; j <= 10; j ++ ){ 31 if(j != a[i][0]){//第一位不同 32 ans += c[j][a[i][1]];//枚举第二位相同 33 } 34 if(j != a[i][1]){ 35 ans += c[a[i][0]][j]; //第一位相同,第二位不同的情况 36 } 37 } 38 } 39 cout << ans / 2 << endl; 40 } 41 return 0; 42 }