第一题:ACGT
题目链接:https://ac.nowcoder.com/acm/contest/931/A
trie树、hash、map遍历
①、trie树上的节点多记一个rest值表示还有多少个串没被用。枚举所有串, 每次先在trie上跑匹配串,看一看那个点的rest。如果没法匹配的话就往trie里插入原串,把结束节点的rest+1
②、思路和trie类似。其实就是把trie换成hash。(把在树上跑换成去hash值)
③、将每个序列的个数存下,每次读入时判断对应序列的map的权值是否为0,若不为0,将输入序列和其对应序列的map权值-- ,ans++
下面是第三种解法 :
1 #include<bits/stdc++.h>
2 using namespace std;
3 #define ll long long
4 #define inf 0x3f3f3f
5 ll read()
6 {
7 ll res=0,flag=0;
8 char ch;
9 if((ch=getchar())=='-')flag=1;
10 else if(ch>='0'&&ch<='9')res=ch-'0';
11 while((ch=getchar())>='0'&&ch<='9')res=res*10+(ch-'0');
12 return flag?-res:res;
13 }
14 void out(int a)
15 {
16 if (a<0)
17 {
18 putchar('-');
19 a=-a;
20 }
21 if(a>=10)out(a/10);
22 putchar(a%10+'0');
23 }
24 map<string ,int >haha;
25 int n;
26 map<char,char> ab;
27 int main()
28 {
29 ios_base::sync_with_stdio(false);
30 ab['A'] = 'T';
31 ab['T'] = 'A';
32 ab['C'] = 'G';
33 ab['G'] = 'C';
34 while(cin>>n)
35 {
36 string a;
37 string b;
38 int cot = 0;
39 haha.clear();
40 for(int i=1; i<=n; i++)
41 {
42 cin>>a;
43 haha[a]++;
44 b="";
45 for(int i = 0; i < a.size(); i++)
46 {
47 b+=ab[a[i]];
48 }
49 // cout<<b<<" "<<haha[b]<<endl;
50 if(haha[b]!=0)
51 {
52 cot++;
53 haha[b]--;
54 haha[a]--;
55 }
56 }
57 cout<<cot<<endl;
58 }
59 return 0;
60 }
第二题:幸运数字考试
题目链接:https://ac.nowcoder.com/acm/contest/931/B
dfs打表+二分
因为n是<=1e19的,而long long是存不到大于777777777444444444的下一个幸运数,所以当n>777777777444444444,我们要进行特判。
剩下的情况的最大的幸运数则为777777777444444444。
因为幸运数肯定为偶数,且只包含4、7且4、7个数相同,所以打表完我们会发现 在long long可存的范围下幸运数并不多
所以我们将所有幸运数存入arr[ ]数组中,最后再对arr[ ]和n进行二分查找,返回大于等于n的第一个幸运数(打表时注意递归方式使数组arr[ ]默认从小到大排序)
下面贴代码:
1 #include<bits/stdc++.h>
2 using namespace std;
3 #define ll long long
4 #define inf 0x3f3f3f
5 ll read()
6 {
7 ll res=0,flag=0;
8 char ch;
9 if((ch=getchar())=='-')flag=1;
10 else if(ch>='0'&&ch<='9')res=ch-'0';
11 while((ch=getchar())>='0'&&ch<='9')res=res*10+(ch-'0');
12 return flag?-res:res;
13 }
14 void out(ll a)
15 {
16 if (a<0)
17 {
18 putchar('-');
19 a=-a;
20 }
21 if(a>=10)out(a/10);
22 putchar(a%10+'0');
23 }
24 const int N = 1e6+10;
25 int k = 1,t;
26 ll n;
27 ll arr[N];
28 void dfs(int num4,int num7,ll ans)
29 {
30 if(num4 == 0 && num7 == 0)
31 {
32 arr[k++] = ans;
33 return ;
34 }
35 else if(num4 == 0)
36 {
37 dfs(num4,num7-1,ans*10+7);
38 }
39 else if(num7 == 0)
40 {
41 dfs(num4-1,num7,ans*10+4);
42 }
43 else
44 {
45 dfs(num4-1,num7,ans*10+4);
46 dfs(num4,num7-1,ans*10+7);
47 }
48 }
49 int main()
50 {
51 ios::sync_with_stdio(false);
52 for(int i=2; i<=18; i+=2)
53 dfs(i/2,i/2,0);
54 k--;
55 cin>>t;
56 while(t--)
57 {
58 cin>>n;
59 if(n > 777777777444444444)
60 {
61 cout<<"44444444447777777777"<<endl;
62 continue;
63 }
64 int r = k;
65 int l = 1;
66 while(r - l > 0)
67 {
68 int mid = (r+l)>>1;
69
70 if(arr[mid]<n)
71 l = mid + 1;
72 else
73 r = mid;
74 }
75 cout<<arr[r]<<endl;
76 }
77 return 0;
78 }