思路:
一種節省空間的做法是map<string, int> + set<string> s[maxn]
篩選時,對每一個只需判斷是否爲後綴即可,判後綴可用string.substr()再進行比較.
#include <set>
#include <map>
#include <ctime>
#include <cmath>
#include <stack>
#include <queue>
#include <deque>
#include <cstdio>
#include <string>
#include <vector>
#include <cctype>
#include <sstream>
#include <utility>
#include <cstring>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <algorithm>
#define SF(a) scanf("%d", &a)
#define PF(a) printf("%d\n", a)
#define SFF(a, b) scanf("%d%d", &a, &b)
#define SFFF(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define SFFFF(a, b, c, d) scanf("%d%d%d%d", &a, &b, &c, &d)
#define CLEAR(a, b) memset(a, b, sizeof(a))
#define IN() freopen("in.txt", "r", stdin)
#define OUT() freopen("out.txt", "w", stdout)
#define FOR(i, a, b) for(int i = a; i < b; ++i)
#define ALL(a) (a).begin(), (a).end()
#define SZ(a) (int)(a).size()
#define PB push_back
#define LL long long
#define mod 10007
#define inf 100000007
#define eps 1e-12
using namespace std;
int buf[20];
int read() {
int x = 0; char ch = getchar(); bool f = 0;
while (ch < '0' || ch > '9') { if (ch == '-') f = 1; ch = getchar(); }
while (ch >= '0' && ch <= '9') x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
return f ? -x : x;
}
void write(int x) {
if (!x) { putchar(48); return; }
int l = 0; if (x < 0) putchar('-'), x = -x;
while (x) buf[++l] = x % 10, x = x / 10;
while (l) putchar(buf[l--] + 48);
}
//-------------------------chc------------------------------//
map<string, set<string>> M;
int main() {
int n;
while (cin >> n) {
while (n--) {
string name;
cin >> name;
int m;
cin >> m;
while (m--) {
string s;
cin >> s;
M[name].insert(s);
}
}
cout << M.size() << endl;
for (auto &it : M) {
vector<string> v;
for(string a : it.second) {
bool ok = true;
for(string b : it.second) {
if (a == b) continue;
if (SZ(b) > SZ(a) && b.substr(SZ(b) - SZ(a)) == a) {
ok = false;
break;
}
}
if (ok) v.PB(a);
}
cout << it.first << ' ';
cout << SZ(v);
FOR(i, 0, v.size())
cout << ' ' << v[i];
cout << endl;
}
}
return 0;
}
B. Proper Nutrition (擴展歐幾里得)歐幾里得算法:
設a = p * b + q;
gcd(b, q) | a, gcd(b, q) | b -> gcd(b, q) | gcd(a, b);
反之q = a - p * b;
gcd(a, b) | q, gcd(a, b) | b -> gcd(a, b) | gcd(b, q);
即gcd(a, b) = gcd(b, a%b);
擴展歐幾里得算法:
求解ax + by = gcd(a, b)
bx' + (a%b)y' = gcd(a, b)
ay' + b(x' - (a/b)*y') = gcd(a, b)
遞歸邊界: b = 0時, x = 1, y = 0
#include <set>
#include <map>
#include <ctime>
#include <cmath>
#include <stack>
#include <queue>
#include <deque>
#include <cstdio>
#include <string>
#include <vector>
#include <cctype>
#include <sstream>
#include <utility>
#include <cstring>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <algorithm>
#define SF(a) scanf("%d", &a)
#define PF(a) printf("%d\n", a)
#define SFF(a, b) scanf("%d%d", &a, &b)
#define SFFF(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define SFFFF(a, b, c, d) scanf("%d%d%d%d", &a, &b, &c, &d)
#define CLEAR(a, b) memset(a, b, sizeof(a))
#define IN() freopen("in.txt", "r", stdin)
#define OUT() freopen("out.txt", "w", stdout)
#define FOR(i, a, b) for(int i = a; i < b; ++i)
#define ALL(a) (a).begin(), (a).end()
#define SZ(a) (int)(a).size()
#define PB push_back
#define LL long long
#define mod 10007
#define inf 100000007
#define eps 1e-12
using namespace std;
int buf[20];
int read() {
int x = 0; char ch = getchar(); bool f = 0;
while (ch < '0' || ch > '9') { if (ch == '-') f = 1; ch = getchar(); }
while (ch >= '0' && ch <= '9') x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
return f ? -x : x;
}
void write(int x) {
if (!x) { putchar(48); return; }
int l = 0; if (x < 0) putchar('-'), x = -x;
while (x) buf[++l] = x % 10, x = x / 10;
while (l) putchar(buf[l--] + 48);
}
//-------------------------chc------------------------------//
void exgcd(LL a, LL b, LL &d, LL &x, LL &y) {
if (b == 0) d = a, x = 1, y = 0;
else {
exgcd(b, a % b, d, y, x);
y -= a / b*x;
}
}
int main() {
LL n, a, b, d, x, y;
while (cin >> n >> a >> b) {
exgcd(a, b, d, x, y);
if (n % d) {
puts("NO");
continue;
}
x *= n / d, y *= n / d;
if (x < 0) {
while (x < 0) x += b, y -= a;
}
else if (y < 0) {
while (y < 0) y += a, x -= b;
}
if (x >= 0 && y >= 0) {
puts("YES");
cout << x << ' ' << y << endl;
}
else puts("NO");
}
return 0;
}
其實很水。。。暴力就行。。
#include <set>
#include <map>
#include <ctime>
#include <cmath>
#include <stack>
#include <queue>
#include <deque>
#include <cstdio>
#include <string>
#include <vector>
#include <cctype>
#include <sstream>
#include <utility>
#include <cstring>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <algorithm>
#define SF(a) scanf("%d", &a)
#define PF(a) printf("%d\n", a)
#define SFF(a, b) scanf("%d%d", &a, &b)
#define SFFF(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define SFFFF(a, b, c, d) scanf("%d%d%d%d", &a, &b, &c, &d)
#define CLEAR(a, b) memset(a, b, sizeof(a))
#define IN() freopen("in.txt", "r", stdin)
#define OUT() freopen("out.txt", "w", stdout)
#define FOR(i, a, b) for(int i = a; i < b; ++i)
#define ALL(a) (a).begin(), (a).end()
#define SZ(a) (int)(a).size()
#define PB push_back
#define LL long long
#define mod 10007
#define inf 100000007
#define eps 1e-12
using namespace std;
int buf[20];
int read() {
int x = 0; char ch = getchar(); bool f = 0;
while (ch < '0' || ch > '9') { if (ch == '-') f = 1; ch = getchar(); }
while (ch >= '0' && ch <= '9') x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
return f ? -x : x;
}
void write(int x) {
if (!x) { putchar(48); return; }
int l = 0; if (x < 0) putchar('-'), x = -x;
while (x) buf[++l] = x % 10, x = x / 10;
while (l) putchar(buf[l--] + 48);
}
//-------------------------chc------------------------------//
void exgcd(LL a, LL b, LL &d, LL &x, LL &y) {
if (b == 0) d = a, x = 1, y = 0;
else {
exgcd(b, a % b, d, y, x);
y -= a / b*x;
}
}
int main() {
LL n, a, b;
cin >> n >> a >> b;
for (int i = 0; i * a <= n; ++i) {
if ((n - i*a) % b == 0) {
puts("YES");
cout << i << ' ' << (n - i*a) / b << endl;
return 0;
}
}
puts("NO");
return 0;
}