标签:int string line first class strong acm his 全局
题目描述:
Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6788 Accepted Submission(s): 3052
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int maxn = 100; const int inf = 0x3f3f3f3f; string c = "ACGT"; string a[maxn]; int pos[10],n; int getlast() {//所有字符串还没有放置长度的最大值,即剩余需要的step的最小可能值 int ans = 0; for (int i = 0; i < n; i++) { ans = max(ans, int(a[i].size()) - pos[i]); } return ans; } int depth; bool dfs(int step) { int last = getlast(); if (step + last>depth)return false;//剩余需要放的长度+已放置了的长度>预设的depth,A*剪枝 if (!last)return true;//如果没有剩余的需要放了,满足要求 int tmp[10];//别当成全局变量了... memcpy(tmp, pos, sizeof(pos)); for (int i = 0; i < 4; i++) {//该层放什么元素 bool f = 0; for (int j = 0; j < n; j++) { if (pos[j]<int(a[j].size())&&a[j][pos[j]] == c[i]) { //如果该字符串下一个放置的字符就是当前遍历的字符,就可以放置 f = 1; pos[j]++; } } if (f) {//如果该层可以放置 if (dfs(step + 1))return true; memcpy(pos, tmp, sizeof(tmp));//回溯 } } return false; } int main() { //freopen("test.txt", "r", stdin); int t; scanf("%d", &t); while (t--) { scanf("%d", &n); int maxlen = 0; for (int i = 0; i < n; i++) { cin >> a[i]; maxlen = max(maxlen, int(a[i].size())); pos[i] = 0; } depth = maxlen;//预设初始深度为最长的字符串 while (1) { if (dfs(0)) break; depth++; } printf("%d\n", depth); } return 0; } /* 1 4 ACGT ATGC CGTT CAGT */ /* 8 */
标签:int string line first class strong acm his 全局
原文地址:https://www.cnblogs.com/MYMYACMer/p/14594815.html