Page Contents
Team Name Solution February Challenge 2021
Сhef has assembled a football team! Now he needs to choose a name for his team. There are NN words in English that Chef considers funny. These words are s1,s2,…,sNs1,s2,…,sN.
Chef thinks that a good team name should consist of two words such that they are not funny, but if we swap the first letters in them, the resulting words are funny. Note that under the given constraints, this means that a word is a non-empty string containing only lowercase English letters.
Also See: February Long Challenge 2021 Solutions
Can you tell Chef how many good team names there are?
Input
- The first line of the input contains a single integer TT denoting the number of test cases. The description of TT test cases follows.
- The first line of each test case contains a single integer NN.
- The second line contains NN space-separated strings s1,s2,…,sNs1,s2,…,sN.
Output
For each test case, print a single line containing one integer — the number of ways to choose a good team name.
Constraints
- 1≤T≤1001≤T≤100
- 2≤N≤2⋅1042≤N≤2⋅104
- 2≤|si|≤202≤|si|≤20 for each valid ii
- s1,s2,…,sNs1,s2,…,sN contain only lowercase English letters
- s1,s2,…,sNs1,s2,…,sN are pairwise distinct
- the sum of NN over all test cases does not exceed 2⋅1042⋅104
Subtasks
Subtask #1 (20 points): s1,s2,…,sNs1,s2,…,sN contain only letters ‘a’ and ‘b’
Subtask #2 (80 points): original constraints
Example Input
3 2 suf mas 3 good game guys 4 hell bell best test
Example Output
2 0 2
Explanation
Example case 1: The good team names are (“muf”, “sas”) and (“sas”, “muf”).
Example case 2: There are no good team names because all funny words start with ‘g’.
Example case 3: The good team names are (“hest”, “tell”) and (“tell”, “hest”).
Follow us on telegram for quick update an abundance of free knowledge: Click Here
Solution
Program C:
#include <stdio.h> #include <string.h> int main() { int t; scanf("%d", &t); getchar(); while(t-- > 0) { int N; scanf("%d", &N); char exts[N][20]; char frst[N][26]; int n_unique = 0; int n_chars[N]; memset(n_chars, 0, N); for(int i = 0; i < N; i++) { char s[20]; scanf("%s", s); int ext_found = 0; for(int j = 0; j < n_unique; j++) { if(strcmp(&s[1], exts[j]) == 0) { // extension exists, add first character frst[j][n_chars[j]] = s[0]; n_chars[j]++; ext_found = 1; break; } } if(ext_found == 0) { // need to add new extension strcpy(exts[n_unique], &s[1]); frst[n_unique][0] = s[0]; n_chars[n_unique] = 1; n_unique++; } } int count = 0; for(int i = 0; i < n_unique; i++) { for(int j = (i + 1); j < n_unique; j++) { int n_unique_chars_1 = n_chars[j]; int n_unique_chars_2 = n_chars[i]; for(int k = 0; k < n_chars[i]; k++) { char ch = frst[i][k]; int exists = 0; for(int l = 0; l < n_chars[j]; l++) { if(frst[j][l] == ch) { exists = 1; break; } } if(exists == 1) { n_unique_chars_1--; n_unique_chars_2--; } } count += (2 * n_unique_chars_1 * n_unique_chars_2); } } printf("%d\n", count); } }
Program C++:
#include<bits/stdc++.h> #pragma GCC target ("avx2") #pragma GCC optimization ("O3") #pragma GCC optimization ("unroll-loops") #define int long long #define endl "\n" using namespace std; int input(string s,map<string,vector<char>> &m , int n) { for (int i=0;i<n;i++) { cin>>s; m[s.substr(1)].push_back(s[0]); } return 0; } int32_t main(){ int t; cin>>t; while(t--){ int n; cin>>n; set<string> s1,s2; map<string,vector<char>>m; string s; input(s,m,n); int sol=0; for(auto i:m) { for(auto j:m) { if(i.first!=j.first) { int count=0; vector<char> u,v; u=i.second; v=j.second; set<char> s(u.begin(),u.end()); for(auto x:v) { if(s.find(x)!=s.end()) { count++; } } sol+=(i.second.size()-count)*(j.second.size()-count); } } } cout<<sol<<endl; } }
Program Java:
import java.util.*; import java.lang.*; import java.io.*; class Codechef { public static void main (String[] args) throws java.lang.Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(br.readLine()); while(t-- > 0) { int n = Integer.parseInt(br.readLine()); String words[] = br.readLine().split(" "); Set<String> set = new HashSet<>(); HashMap<Character, Integer> map = new HashMap<>(); for(String s: words) { set.add(s); map.put(s.charAt(0), map.getOrDefault(s.charAt(0),0)+1); } int res = 0; Arrays.sort(words, (a,b) -> (int)a.charAt(0)-(int)b.charAt(0)); int i=0; StringBuilder one = new StringBuilder(); StringBuilder two = new StringBuilder(); while(i < n) { int j = i+map.get(words[i].charAt(0)); map.put(words[i].charAt(0), map.get(words[i].charAt(0))-1); while(j < n) { one.append(words[j].charAt(0)).append(words[i].substring(1)); two.append(words[i].charAt(0)).append(words[j].substring(1)); if(set.contains(one.toString())) { j += map.get(words[j].charAt(0)); } else{ if(!set.contains(two.toString())) res += 2; j++; } one.setLength(0); two.setLength(0); } i++; } System.out.println(res); } } }
Program Python:
def dis(x, y): s = len(set(x + y)) return s for _ in range(int(input())): n = int(input()) listBase = input().split() dict = {} for i in listBase: k = i[1:] if k in dict: dict[k].append(i[0]) else: dict[k] = [i[0]] list4 = list (dict.keys()) s = 0 for i in range(len(dict)): for l in range(i+1, len(dict)): temp = dis(dict[list4[i]], dict[list4[l]]) s+=(temp - len(dict[list4[i]])) * (temp - len(dict[list4[l]])) print(2*s)
Codechef Long Challenges
September Long Challenge 2021 Solution
- Airline Restrictions
- Travel Pass
- Shuffling Parities
- XOR Equal
- 2-D Point Meeting
- Minimize Digit Sum
- Minimum Digit Sum 2
- Treasure Hunt
- Covaxin vs Covishield
February Long Challenge 2021
- Frog Sort Solution Codechef
- Chef and Meetings Solution Codechef
- Maximise Function Solution Codechef
- Highest Divisor Solution Codechef
- Cut the Cake Challenge Solution Codechef
- Dream and the Multiverse Solution Codechef
- Cell Shell Solution Codechef
- Multiple Games Solution Codechef
- Another Tree with Number Theory Solution Codechef
- XOR Sums Solution Codechef
- Prime Game Solution CodeChef
- Team Name Solution Codechef
- Bash Matrix Solution CodeChef