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 }
原文地址:https://www.cnblogs.com/scaulok/p/9984756.html