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 }