sicily 1035. DNA matching

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<string>
 4 #include<algorithm>
 5 using namespace std;
 6
 7 bool cmp(string a, string b);
 8 bool match(string a, string b);
 9
10
11 int main(void) {
12     int testSize, couple;
13     string str[101];
14     int len[101];
15     bool used[101];
16
17     // for each test case: (t<=20)
18     cin >> testSize;
19     while(testSize--) {
20         // scan and store n strings(n <= 100)
21         cin >> couple;
22         for (int i = 0; i < couple; ++i) {
23             cin >> str[i];
24         }
25
26         // sort by length
27         sort(str, str + couple, cmp);
28         //sort(<#_RandomAccessIterator __first#>, <#_RandomAccessIterator __last#>, <#_Compare __comp#>)
29
30         for (int i = 0; i < couple; ++i) {
31             // store length
32             len[i] = str[i].size();
33             // init used
34             used[i] = false;
35         }
36
37         int count = 0;
38
39         for (int i = 0; i < couple; ++i) {
40             if (!used[i]) {
41                 for (int j = i + 1; j < couple && len[j] == len[i]; ++j) {
42                     if (match(str[i], str[j]) && !used[j]) {
43                         count++;
44                         used[i] = used[j] = true;
45                         break;
46                     }
47                 }
48             }
49         }
50
51         cout << count << ‘\n‘;
52     }
53
54     return 0;
55 }
56
57 bool cmp(string a,string b)
58 {
59     return a.size() > b.size();
60 }
61 bool match(string a,string b){
62     int len = a.size();
63     for (int i = 0; i<len; i++) {
64         if (a[i] == ‘A‘ && b[i] != ‘T‘)
65             return false;
66         if (a[i] == ‘T‘ && b[i] != ‘A‘)
67             return false;
68         if (a[i] == ‘C‘ && b[i] != ‘G‘)
69             return false;
70         if (a[i] == ‘G‘ && b[i] != ‘C‘)
71             return false;
72     }
73     return true;
74 }
时间: 2025-01-18 08:15:57

sicily 1035. DNA matching的相关文章

编程题目分类(剪辑)

1. 编程入门 2. 数据结构 3. 字符串 4. 排序 5. 图遍历 6. 图算法 7. 搜索:剪枝,启发式搜索 8. 动态规划/递推 9. 分治/递归 10. 贪心 11. 模拟 12. 算术与代数 13. 组合问题 14. 数论 15. 网格,几何,计算几何 [编程入门] PC 110101, uva 100, The 3n+1 problem, 难度 1 PC 110102, uva 10189, Minesweeper, 难度 1 PC 110103, uva 10137, The T

LeetCode 10. Regular Expression Matching

https://leetcode.com/problems/regular-expression-matching/description/ Implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. The matching should cover

UVA - 1368 DNA Consensus String

DNA Consensus String Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description  Figure 1. DNA (Deoxyribonucleic Acid) is the molecule which contains the genetic instructions. It consists of four different nuc

[LeetCode]Repeated DNA Sequences

题目:Repeated DNA Sequences 给定包含A.C.G.T四个字符的字符串找出其中十个字符的重复子串. 思路: 首先,string中只有ACGT四个字符,因此可以将string看成是1,3,7,20这三个数字的组合串: 并且可以发现{ACGT}%5={1,3,2,0};于是可以用两个位就能表示上面的四个字符: 同时,一个子序列有10个字符,一共需要20bit,即int型数据类型就能表示一个子序列: 这样可以使用计数排序的思想来统计重复子序列: 这个思路时间复杂度只有O(n),但是

POJ2778 DNA Sequence Trie+矩阵乘法

题意:给定N个有A C G T组成的字符串,求长度为L的仅由A C G T组成的字符串中有多少个是不含给定的N个字符串的题解: 首先我们把所有的模式串(给定的DNA序列)建Trie,假定我们有一个匹配串,并且在匹配过程到S[i]这个字符时匹配到了Trie上的某个节点t,那么有两种可能: 匹配失败:t->child[S[i]]为空,跳转到t->fail,因此t->fail一定不能是某个模式串的结尾: 匹配成功:跳转到t->child[S[i+1]],因此t->child[S[i

CodeForces 520C DNA Alignment

题意: 一段DNA序列(10^5长度)  定义h函数为两序列相同碱基个数  p函数为分别移动两个DNA序列后所有可能的h函数之和  问使p最大的序列有多少个 思路: 根据p函数的定义  我们发现p这个函数其实就是A序列每个碱基和B序列每个碱基比较再乘一个n 因此可以贪心构造B序列  即每次新加一个碱基必定是A序列中出现次数最多的碱基 那么最后的答案就是A序列中出现次数最多的碱基种类数的n次方 代码: #include<cstdio> #include<iostream> #incl

HDU - 1560 DNA sequence

给你最多8个长度不超过5的DNA系列,求一个包含所有系列的最短系列. 迭代加深的经典题.(虽然自己第一次写) 定一个长度搜下去,搜不出答案就加深大搜的限制,然后中间加一些玄学的减枝 //Twenty #include<cstdio> #include<cstdlib> #include<iostream> #include<algorithm> #include<cmath> #include<cstring> #include<

No matching bean of type dao found for depende

场景: Spring MVC + MyBatis 启动服务失败,控制台打印一系统异常. 异常大意如下: xxxService 加载 xxxInterface 或 xxxMapper 失败 最后一条异常 No matching bean of type [xxxService] found for depende 思考: 从异常本身看:找不到依赖或加载错误的接口很早以前就已经存在了,所以不会有问题 所以从最新修改或新增的接口上查找问题.定位到  xxxService 解决: 通常 No match

51Nod - 1035 最长的循环节

51Nod - 1035 最长的循环节 正整数k的倒数1/k,写为10进制的小数如果为无限循环小数,则存在一个循环节,求<=n的数中,倒数循环节长度最长的那个数,假如存在多个最优的答案,输出所有答案中最大的那个数. 1/6= 0.1(6) 循环节长度为1 1/7= 0.(142857) 循环节长度为6 1/9= 0.(1)  循环节长度为1 Input 输入n(10 <= n <= 1000) Output 输出<=n的数中倒数循环节长度最长的那个数 Input示例 10 Outp