fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. int t;
  6. cin >> t;
  7. while(t--){
  8. int n;
  9. cin >> n;
  10. string s[n];
  11. int flag(0);
  12. for(int i=0;i<n;i++){
  13. cin >> s[i];
  14. }
  15. for(int i=0;i<n;i++){
  16. if(s[i].length() == 3 && s[i][0] == s[i][2]){
  17. flag = 1;
  18. break;
  19. }
  20. }
  21. for(int i=0;i<n;i++){
  22. if(s[i].length() == 1){
  23. flag =1;
  24. break;
  25. }
  26. int flag1(0);
  27. for(int j=0;j<s[i].length();j++){
  28. if(s[i][j] != s[i][0]){
  29. flag1 = 1;
  30. break;
  31. }
  32. }
  33. if(flag1 == 0){
  34. flag = 1;
  35. }
  36. }
  37. if(flag == 1){
  38. cout << "YES" << endl;
  39. }
  40. else{
  41. set<string> s2;
  42. set<string> s3;
  43. for(int i=0;i<n;i++){
  44. if(s[i].length() == 2){
  45. string x = s[i];
  46. reverse(x.begin(),x.end());
  47. if(s2.find(x) != s2.end()){
  48. flag = 1;
  49. break;
  50. }
  51. s2.insert(s[i]);
  52. }
  53. if(s[i].length() == 3){
  54. string x = s[i];
  55. reverse(x.begin(),x.end());
  56. if(s3.find(x) != s3.end()){
  57. flag = 1;
  58. break;
  59. }
  60. s3.insert(s[i]);
  61. string y;
  62. y += s[i][2];
  63. y += s[i][1];
  64. // cout << y << endl;
  65. if(s2.find(y) != s2.end()){
  66. flag = 1;
  67. break;
  68. }
  69. }
  70. }
  71. if(flag == 1){
  72. cout << "YES" << endl;
  73. }
  74. else{
  75. set<string> fin;
  76. for(int i=n-1;i>-1;i--){
  77. if(s[i].length() == 2){
  78. fin.insert(s[i]);
  79. }
  80. if(s[i].length() == 3){
  81. string zz;
  82. zz += s[i][1];
  83. zz += s[i][0];
  84. if(fin.find(zz) != fin.end()){
  85. flag = 1;
  86. break;
  87. }
  88. }
  89. }
  90. if(flag == 1){
  91. cout << "YES" << endl;
  92. }
  93. else{
  94. cout << "NO" << endl;
  95. }
  96.  
  97. }
  98. }
  99. }
  100. return 0;
  101. }
Success #stdin #stdout 0.01s 5516KB
stdin
1
3
ca
bcb
ca
stdout
YES