fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <unordered_set>
  4. #include <vector> // Cần thiết cho vector nếu dùng cấp phát động
  5.  
  6. using namespace std;
  7.  
  8. const int MAX_VIRUS_LENGTH = 10;
  9. const int STATIC_THRESHOLD = 50000; // Ví dụ: Ngưỡng cho mảng tĩnh
  10.  
  11. int main() {
  12. ios_base::sync_with_stdio(false);
  13. cin.tie(nullptr);
  14. cout.tie(nullptr);
  15.  
  16. int n, m, q;
  17. cin >> n >> m >> q;
  18. cin.ignore();
  19.  
  20. char** a = nullptr; // Khởi tạo con trỏ mảng động
  21. char static_a[n][m]; // Mảng tĩnh
  22. bool use_static = (n * m <= STATIC_THRESHOLD);
  23.  
  24. if (use_static) {
  25. for (int i = 0; i < n; ++i) {
  26. for (int j = 0; j < m; ++j) {
  27. cin >> static_a[i][j];
  28. }
  29. }
  30. } else {
  31. a = new char*[n]; // Cấp phát động
  32. for (int i = 0; i < n; ++i) {
  33. a[i] = new char[m];
  34. }
  35. for (int i = 0; i < n; ++i) {
  36. for (int j = 0; j < m; ++j) {
  37. cin >> a[i][j];
  38. }
  39. }
  40. }
  41.  
  42. unordered_set<string> virus;
  43. char temp[11];
  44.  
  45. for (int i = 0; i < n; ++i) {
  46. for (int j = 0; j < m; ++j) {
  47. for (int len = 2; len <= 10 && j + len <= m; ++len) {
  48. for (int k = 0; k < len; ++k) {
  49. temp[k] = use_static ? static_a[i][j + k] : a[i][j + k];
  50. }
  51. temp[len] = '\0';
  52. virus.insert(string(temp));
  53. }
  54. }
  55. }
  56.  
  57. for (int j = 0; j < m; ++j) {
  58. for (int i = 0; i < n; ++i) {
  59. for (int len = 2; len <= 10 && i + len <= n; ++len) {
  60. for (int k = 0; k < len; ++k) {
  61. temp[k] = use_static ? static_a[i + k][j] : a[i + k][j];
  62. }
  63. temp[len] = '\0';
  64. virus.insert(string(temp));
  65. }
  66. }
  67. }
  68.  
  69. while (q--) {
  70. string x;
  71. cin >> x;
  72. if (virus.find(x) == virus.end())
  73. cout << 0;
  74. else
  75. cout << 1;
  76. }
  77.  
  78. if (!use_static) { // Giải phóng bộ nhớ nếu dùng mảng động
  79. for (int i = 0; i < n; ++i) {
  80. delete[] a[i];
  81. }
  82. delete[] a;
  83. }
  84.  
  85. return 0;
  86. }
Success #stdin #stdout 0.01s 5276KB
stdin
4 5 4
attnt
oboot
campc
ontes
tt
attn
xyc
aoco
stdout
1101