srm 541
資瓷點這里閱讀該文章O_o
250
Solution
水題,最暴力的方法枚舉就可以
Code
#include <bits/stdc++.h> using namespace std; #define pb push_back #define mp make_pair #define F first #define S second typedef long long LL; typedef pair<int, int> pii; map<char, int> s; int dx[] = {0, 1, -1, 0}; int dy[] = {1, 0, 0, -1}; const int N = 55; int f[N]; bool vis[N]; struct AntsMeet {int countAnts(vector <int> x, vector <int> y, string direction) {int n = x.size();s['N'] = 0, s['E'] = 1, s['W'] = 2, s['S'] = 3;for (int i = 0; i < n; ++i) x[i] <<= 1, y[i] <<= 1, f[i] = s[direction[i]], vis[i] = 1;for (int i = 1; i <= 4001; ++i) {for (int j = 0; j < n; ++j)if (vis[j]) {for (int k = j + 1; k < n; ++k)if (vis[k]) {if (x[j] == x[k] && y[j] == y[k]) vis[j] = vis[k] = 0;}}for (int j = 0; j < n; ++j)if (vis[j]) {x[j] += dx[f[j]];y[j] += dy[f[j]];}}int ans = 0;for (int i = 0; i < n; ++i) if (vis[i]) ++ans;return ans;} };550
Description
給出串A,B,C,S,F和整數k。
以及函數f(x)=A+x+B+x+C。
求fk(x)中以F為子串,出現了多少次。答案mod 109+7。
串的長度≤50, k≤107
Solution
注意到串長度≤50,以及k≤107,并且出現F的情況分為在A。B。C三個串中分別出現,以及在交界處出現。因為串的長度比較小,所以我們暴力50次以后,交界處包括F的次數就不再變化了(想一想,為什么)。于是后面的情況我們每次ans=ans×2+t就可以。。t是交界處的答案,ans是A,B,C中的答案。
Code
#include <bits/stdc++.h> using namespace std; #define pb push_back #define mp make_pair #define F first #define S second typedef long long LL; typedef pair<int, int> pii; const int M = 1e9 + 7; struct AkariDaisukiDiv1 {int gao(const string &s, const string &t, int l = 0, int r = 100000000) {int tmp = 0;for (int i = l; i < s.size() - t.size() + 1 && i < r; ++i)if (s.substr(i, t.size()) == t) ++tmp;return tmp;}int countF(string A, string B, string C, string S, string F, int k) {int cnt = 0;for (; cnt < k && S.size() < F.size(); ++cnt) S = A + S + B + S + C;if (S.size() < F.size()) return 0;int ans = gao(S, F), t = 0; string p = S.substr(0, F.size()), q = S.substr(S.size() - F.size(), F.size());for (int i = 0; cnt < k && i < 50; ++cnt, ++i) {t = gao(A + p, F, 0, A.size()) + gao(q + B + p, F, 1, F.size() + B.size()) + gao(q + C, F, 1);ans = (ans + ans + t) % M;p = (A + p).substr(0, F.size()), q = (q + C).substr((q + C).size() - F.size(), F.size());}for (; cnt < k; ++cnt) ans = (ans + ans + t) % M;return ans;} };轉載于:https://www.cnblogs.com/gcczhongduan/p/5286874.html
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
- 上一篇: python 函数性能分析
- 下一篇: Android中广播接收者Broadca