“美登杯”上海市高校大学生程序设计赛B. 小花梨的三角形(模拟,实现)
題目鏈接:https://acm.ecnu.edu.cn/contest/173/problem/B/#report9
Problem B B 、 小 花梨 的 三角形
時(shí)間限制:1000ms 空間限制:512MB
Description
小花梨現(xiàn)在有一個(gè)?層三角形圖(參考下圖),第?層有2? ? 1個(gè)邊長(zhǎng)為1的等邊三角形。
每個(gè)交點(diǎn)處存在一個(gè)字符,總共有? + 1層字符,第?層有?個(gè)字符。
小花梨用等邊三角形三個(gè)頂點(diǎn)上的字符來(lái)表示這個(gè)三角形,兩個(gè)等邊三角形如果它們的三個(gè)
頂點(diǎn)字符相同(不區(qū)分順序)則視為同一類等邊三角形。小花梨想知道總共存在多少種不同類
別的等邊三角形。
Input
第一行為正整數(shù)?,表示三角形層數(shù)(1 ≤ ? ≤ 100)。
接下來(lái)? + 1行,第?行輸入?個(gè)字符,表示第?層的字符。(字符只包含小寫(xiě)字母"? ? ?")
Output
輸出一個(gè)整數(shù)表示存在多少種不同類別的三角形
Example
Sample Input Sample Output
1
a
bc
1
2
a
bb
cac
3
3
a
aa
aaa
aaaa
1
Note
一:只存在頂點(diǎn)為(?,?,?)的三角形
二:存在頂點(diǎn)為(?,?,?) 、 (?,?,?) 、 (?,?,?)的3類不同的三角形
樣例三:只存在頂點(diǎn)為(?,?,?)的三角形
思路:
枚舉行,枚舉列,枚舉邊長(zhǎng),統(tǒng)計(jì)三個(gè)節(jié)點(diǎn)的字母,
排序后插入set中即可。
存在正的三角形,還存在倒立的三角形
細(xì)節(jié)見(jiàn)代碼:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <queue> #include <stack> #include <map> #include <set> #include <vector> #include <iomanip> #define ALL(x) (x).begin(), (x).end() #define sz(a) int(a.size()) #define all(a) a.begin(), a.end() #define rep(i,x,n) for(int i=x;i<n;i++) #define repd(i,x,n) for(int i=x;i<=n;i++) #define pii pair<int,int> #define pll pair<long long ,long long> #define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0) #define MS0(X) memset((X), 0, sizeof((X))) #define MSC0(X) memset((X), '\0', sizeof((X))) #define pb push_back #define mp make_pair #define fi first #define se second #define eps 1e-6 #define gg(x) getInt(&x) #define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl using namespace std; typedef long long ll; ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;} ll lcm(ll a, ll b) {return a / gcd(a, b) * b;} ll powmod(ll a, ll b, ll MOD) {ll ans = 1; while (b) {if (b % 2) { ans = ans * a % MOD; } a = a * a % MOD; b /= 2;} return ans;} inline void getInt(int *p); const int maxn = 1000010; const int inf = 0x3f3f3f3f; /*** TEMPLATE CODE * * STARTS HERE ***/ string temp; set<string> st; int n; string a[maxn]; int main() {//freopen("D:\\code\\text\\input.txt","r",stdin);//freopen("D:\\code\\text\\output.txt","w",stdout);gbtb;cin >> n;n++;repd(i, 1, n) {cin >> a[i];}repd(i, 1, n) {repd(j, 0, i - 1) {int x = i;int y = j;for (int len = 1;; len++) {temp = "";if (x + len > n) {break;}temp.pb(a[x][y]);temp.pb(a[x + len][y]);temp.pb(a[x + len][y + len]);sort(ALL(temp));st.insert(temp);}}}for (int i = n; i >= 1; --i) {repd(j, 0, i - 1) {int x = i;int y = j;for (int len = 1;; len++) {temp = "";if (x - len < 0 || y - len < 0 || y >= x - len ) {break;}temp.pb(a[x][y]);temp.pb(a[x - len][y]);temp.pb(a[x - len][y - len]);sort(ALL(temp));st.insert(temp);}}}cout << sz(st) << endl;return 0; }inline void getInt(int *p) {char ch;do {ch = getchar();} while (ch == ' ' || ch == '\n');if (ch == '-') {*p = -(getchar() - '0');while ((ch = getchar()) >= '0' && ch <= '9') {*p = *p * 10 - ch + '0';}} else {*p = ch - '0';while ((ch = getchar()) >= '0' && ch <= '9') {*p = *p * 10 + ch - '0';}} }轉(zhuǎn)載于:https://www.cnblogs.com/qieqiemin/p/11455504.html
總結(jié)
以上是生活随笔為你收集整理的“美登杯”上海市高校大学生程序设计赛B. 小花梨的三角形(模拟,实现)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 原博客文章(Apache初配2008/4
- 下一篇: 心情-天气