fork(8) download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. string str[]={"abc", "def", "ghi", "jkl","mno", "pqrs", "tuv", "wxyz"};
  4. void solve(int *a, int n)
  5. {
  6. int i,size;
  7. queue<string >q;
  8. for(i=0;i<str[a[0]].size();i++)
  9. {
  10. string s="";
  11. s = s+ str[a[0]][i];
  12. q.push(s);
  13. }
  14. i=1;
  15. while(!q.empty())
  16. {
  17. if(i==n)
  18. {
  19. break;
  20. }
  21. int size = q.size();
  22. string s = str[a[i]];
  23.  
  24. while(size--)
  25. {
  26. string st = q.front();
  27. //cout<<st<<" ";
  28. q.pop();
  29. for(int j=0;j<s.size();j++)
  30. {
  31. //cout<<st+s[j]<<" ";
  32. q.push(st+s[j]);
  33. }
  34.  
  35. }
  36. i++;
  37. }
  38. while(!q.empty())
  39. {
  40. cout<<q.front()<<" ";
  41. q.pop();
  42. }
  43. cout<<"\n";
  44. }
  45. int main()
  46. {
  47. int t;
  48. cin>>t;
  49. while(t--)
  50. {
  51. int i,j ,n ;
  52. cin>>n;
  53. int a[n];
  54. for(i=0;i<n;i++)
  55. {
  56. cin>>a[i];
  57. a[i] = a[i]-2;
  58. }
  59. solve(a, n);
  60. }
  61. }
Success #stdin #stdout 0s 2884KB
stdin
1
3
2 3 4
stdout
adg adh adi aeg aeh aei afg afh afi bdg bdh bdi beg beh bei bfg bfh bfi cdg cdh cdi ceg ceh cei cfg cfh cfi