ACM-ICPC 2018 南京賽區現場賽 E. Eva and Euro coins (思維)


題目鏈接:https://codeforc.es/gym/101981/attachments

題意:給出兩個只包含01的字符串,每次可以選擇連續k個相同的數字進行翻轉,問能否通過若干次操作把兩個字符串變為相同。

題解:(qls:通過觀察可以發現,可以把每個 1 在不跨越其他 1 的情況下往左/右移 k 個位置,盡可能把 1 往左移,出現連續 k 個 1 就消掉,check一下兩個串操作完之后是否相等。)

 

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 #define ll long long
 4 #define ull unsigned long long
 5 #define mst(a,b) memset((a),(b),sizeof(a))
 6 #define mp(a,b) make_pair(a,b)
 7 #define pi acos(-1)
 8 #define pii pair<int,int>
 9 #define pb push_back
10 const int INF = 0x3f3f3f3f;
11 const double eps = 1e-6;
12 const int maxn = 1e6 + 10;
13 const int maxm = 1e5 + 10;
14 const ll mod =  998244353;
15 
16 int n,k;
17 string s1,s2;
18 int st[maxn],cnt[maxn];
19 
20 string F(string s) {
21     if(k == 1) return "";
22     int top = 0;
23     for(int i = 0; i < n; i++) {
24         if(top && st[top] == s[i] - '0') {
25             cnt[top]++;
26             if(cnt[top] == k) top--;
27         } else {
28             st[++top] = s[i] - '0';
29             cnt[top] = 1;
30         }
31     }
32     s = "";
33     for(int i = 1; i <= top; i++) {
34         while(cnt[i]) {
35             s += st[i];
36             cnt[i]--;
37         }
38     }
39     return s;
40 }
41 
42 int main() {
43 #ifdef local
44     freopen("data.txt", "r", stdin);
45 //    freopen("data.txt", "w", stdout);
46 #endif
47     ios_base::sync_with_stdio(0);
48     cin.tie(0);
49     cout.tie(0);
50     cin >> n >> k >> s1 >> s2;
51     if(F(s1) == F(s2)) cout << "Yes" << endl;
52     else cout << "No" << endl;
53     return 0;
54 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM