#include <iostream> #include <string> #include <cmath> using namespace std; int dlugoscLancucha = 0; int iloscZmian = 0; string ciag; string ciagOrg; string ciagOrg1; int n[200000]; int sum = 0; int binaryCounter[200001]; bool exitt; int sumOf; int indexy[200001]; char typy[200001]; void binaryAddOne() { int carry = 1; for (int i = 0; i < 200000; i++) { if (carry + binaryCounter[i] == 0) { binaryCounter[i] = 0; carry = 0; } if (carry + binaryCounter[i] == 1) { binaryCounter[i] = 1; carry = 0; } if (carry + binaryCounter[i] == 2) { binaryCounter[i] = 0; carry = 1; } } } int main() { cin >> dlugoscLancucha >> iloscZmian; cin >> ciag; ciagOrg = ciag; for (int o = 0; o < iloscZmian; o++) { cin >> indexy[o + 1] >> typy[o + 1]; } for (int zmiana = 0; zmiana < iloscZmian + 1; zmiana++) { sum = 0; sumOf = 0; if (zmiana != 0) ciag = ciagOrg1; if(zmiana != 0) ciag[indexy[zmiana]-1] = typy[zmiana]; ciagOrg1 = ciag; for (int i = 0; i < ciag.length() + 1; i++) { if (ciag[i] == 'N') { n[i] = 1; sum++; } else { n[i] = 0; } } for (int g = 0; g < 10000; g++) { binaryCounter[g] = 0; } int k = 0; int rots = pow(2, sum); while (k < rots*2) { ciag = ciagOrg1; int indp = 0; for (int j = 0; j < ciag.length() + 1; j++) { if (ciagOrg1[j] == 'N') { if (binaryCounter[j + 1 - indp] == 0) ciag[j] = 'C'; else ciag[j] = 'Z'; } else indp++; } binaryAddOne(); while (!exitt) { exitt = true; string tempArray; for (int i = 0; i < ciag.length(); i++) { if (ciag[i] == 'Z' && ciag[i + 1] == 'Z') { ciag[i] = 'C'; ciag.erase(i + 1, 1); exitt = false; i = 0; } else if (ciag[i] == 'C' && ciag[i + 1] == 'C') { ciag[i] = 'Z'; ciag.erase(i + 1, 1); exitt = false; i = 0; } } } if (ciag.length() == 1) sumOf++; exitt = false; k++; } cout << sumOf/2 << endl; } return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 | #include <iostream> #include <string> #include <cmath> using namespace std; int dlugoscLancucha = 0; int iloscZmian = 0; string ciag; string ciagOrg; string ciagOrg1; int n[200000]; int sum = 0; int binaryCounter[200001]; bool exitt; int sumOf; int indexy[200001]; char typy[200001]; void binaryAddOne() { int carry = 1; for (int i = 0; i < 200000; i++) { if (carry + binaryCounter[i] == 0) { binaryCounter[i] = 0; carry = 0; } if (carry + binaryCounter[i] == 1) { binaryCounter[i] = 1; carry = 0; } if (carry + binaryCounter[i] == 2) { binaryCounter[i] = 0; carry = 1; } } } int main() { cin >> dlugoscLancucha >> iloscZmian; cin >> ciag; ciagOrg = ciag; for (int o = 0; o < iloscZmian; o++) { cin >> indexy[o + 1] >> typy[o + 1]; } for (int zmiana = 0; zmiana < iloscZmian + 1; zmiana++) { sum = 0; sumOf = 0; if (zmiana != 0) ciag = ciagOrg1; if(zmiana != 0) ciag[indexy[zmiana]-1] = typy[zmiana]; ciagOrg1 = ciag; for (int i = 0; i < ciag.length() + 1; i++) { if (ciag[i] == 'N') { n[i] = 1; sum++; } else { n[i] = 0; } } for (int g = 0; g < 10000; g++) { binaryCounter[g] = 0; } int k = 0; int rots = pow(2, sum); while (k < rots*2) { ciag = ciagOrg1; int indp = 0; for (int j = 0; j < ciag.length() + 1; j++) { if (ciagOrg1[j] == 'N') { if (binaryCounter[j + 1 - indp] == 0) ciag[j] = 'C'; else ciag[j] = 'Z'; } else indp++; } binaryAddOne(); while (!exitt) { exitt = true; string tempArray; for (int i = 0; i < ciag.length(); i++) { if (ciag[i] == 'Z' && ciag[i + 1] == 'Z') { ciag[i] = 'C'; ciag.erase(i + 1, 1); exitt = false; i = 0; } else if (ciag[i] == 'C' && ciag[i + 1] == 'C') { ciag[i] = 'Z'; ciag.erase(i + 1, 1); exitt = false; i = 0; } } } if (ciag.length() == 1) sumOf++; exitt = false; k++; } cout << sumOf/2 << endl; } return 0; } |