The 2018 ACM-ICPC Asia Beijing Regional Contest
http://hihocoder.com/problemset/problem/
#1870 : Jin Yong’s Wukong Ranking List
我是每加1个点就dfs判断1次。
正解是拓扑排序。。。
1 #include <cstdio> 2 #include <cstdlib> 3 #include <ctime> 4 #include <cstring> 5 #include <string> 6 #include <map> 7 #include <set> 8 #include <list> 9 #include <queue> 10 #include <vector> 11 #include <bitset> 12 #include <algorithm> 13 #include <iostream> 14 using namespace std; 15 const int maxn=50; 16 17 bool vis[maxn]; 18 char str[maxn][maxn],a[maxn],b[maxn],result1[maxn],result2[maxn]; 19 vector<int>e[maxn]; 20 int r; 21 22 void dfs(int d) 23 { 24 vector<int>::iterator j; 25 vis[d]=1; 26 for (j=e[d].begin();j!=e[d].end();j++) 27 if (!vis[*j]) 28 dfs(*j); 29 else 30 { 31 r=1; 32 return; 33 } 34 } 35 36 int main() 37 { 38 int n,i,j,k,g; 39 while (~scanf("%d",&n)) 40 { 41 for (i=1;i<=2*n;i++) 42 e[i].clear(); 43 g=0; 44 r=0; 45 for (i=1;i<=n;i++) 46 { 47 scanf("%s%s",a,b); 48 for (j=1;j<=g;j++) 49 if (strcmp(str[j],a)==0) 50 break; 51 if (j==g+1) 52 { 53 g++; 54 strcpy(str[g],a); 55 } 56 57 for (k=1;k<=g;k++) 58 if (strcmp(str[k],b)==0) 59 break; 60 if (k==g+1) 61 { 62 g++; 63 strcpy(str[g],b); 64 } 65 66 e[j].push_back(k); 67 68 memset(vis,0,sizeof(vis)); 69 if (r==0) 70 { 71 dfs(k); 72 if (r==1) 73 { 74 strcpy(result1,a); 75 strcpy(result2,b); 76 } 77 } 78 } 79 80 if (r==0) 81 printf("0\n"); 82 else 83 printf("%s %s\n",result1,result2); 84 } 85 return 0; 86 }
#1871 : Heshen's Account Book
把所有内容放入字符串里,再判断。
挺多细节要考虑的,代码下方有自己编的若干数据。
1 #include <cstdio> 2 #include <cstdlib> 3 #include <ctime> 4 #include <cstring> 5 #include <string> 6 #include <map> 7 #include <set> 8 #include <list> 9 #include <queue> 10 #include <vector> 11 #include <bitset> 12 #include <algorithm> 13 #include <iostream> 14 using namespace std; 15 const int maxn=2e5; 16 const int maxsinlen=20; 17 const int maxlen=1e3+10; 18 const int maxtotallen=3e5; 19 const int maxline=2e2+10; 20 21 char num[maxn][maxsinlen]; 22 char str[maxtotallen],s[maxlen],c; 23 int gx[maxline]; 24 25 int main() 26 { 27 int g=0,len,line=0,ind=0,pos=0,i,j,k; 28 bool vis=1; 29 strcpy(str,""); 30 while (gets(s)) 31 { 32 strcat(str,s); 33 strcat(str,"\n"); 34 } 35 len=strlen(str); 36 37 for (i=0;i<len;i++) 38 { 39 if (str[i]=='\n') 40 { 41 line++; 42 if (i==0 || str[i-1]<'0' || str[i-1]>'9' || str[i+1]<'0' || str[i+1]>'9') 43 { 44 if (vis && i>ind) 45 { 46 // strncpy(num[g++],str+ind,i-ind); 47 ///delete ' ','\n' 48 k=0; 49 for (j=ind;j<i;j++) 50 if (str[j]>='0' && str[j]<='9') 51 num[g][k++]=str[j]; 52 num[g][k]=0; 53 if (!(k>1 && num[g][0]=='0')) 54 { 55 g++; 56 gx[pos]++; 57 } 58 } 59 vis=1; 60 ind=i+1; 61 pos=line; 62 } 63 } 64 else if (str[i]==' ') 65 { 66 if (vis && i>ind) 67 { 68 // strncpy(num[g++],str+ind,i-ind); 69 ///delete ' ','\n' 70 k=0; 71 for (j=ind;j<i;j++) 72 if (str[j]>='0' && str[j]<='9') 73 num[g][k++]=str[j]; 74 num[g][k]=0; 75 if (!(k>1 && num[g][0]=='0')) 76 { 77 g++; 78 gx[pos]++; 79 } 80 } 81 vis=1; 82 ind=i+1; 83 pos=line; 84 } 85 else 86 { 87 if (str[i]<'0' || str[i]>'9') 88 vis=0; 89 } 90 } 91 92 if (g>=1) 93 printf("%s",num[0]); 94 for (i=1;i<g;i++) 95 printf(" %s",num[i]); 96 printf("\n"); 97 98 for (i=0;i<line;i++) 99 printf("%d\n",gx[i]); 100 return 0; 101 } 102 /** 103 12 104 34 105 56 106 78 107 900 108 109 --- 110 111 003 112 004 113 005 114 1 006 a 115 116 --- 117 ''' 118 119 23 123 120 123 123 121 122 123 124 adssa q3qe 125 qw 1 126 qw123 127 12 128 129 130 ''' 131 132 --- 133 134 1234 135 123 136 137 --- 138 139 1234 140 12 34 141 142 --- 143 144 145 1 146 147 2 148 149 3 150 151 as 152 153 4 154 155 --- 156 157 12 158 3 159 4 160 5a 161 162 163 164 --- 165 166 00 167 168 --- 169 170 0 171 172 --- 173 174 0 175 12 176 177 --- 178 179 0 180 12asdb 181 182 --- 183 184 ''' 185 0 186 a 187 0 0 0 0 188 189 12 0 01 100 10 01 0 190 0 191 192 ''' 193 194 **/
#1873 : Frog and Portal
重现赛时,看错题了,难受。
告诫自己:比赛时,一定要认真读题,认真分析数据!
1 #include <cstdio> 2 #include <cstdlib> 3 #include <ctime> 4 #include <cstring> 5 #include <string> 6 #include <map> 7 #include <set> 8 #include <list> 9 #include <queue> 10 #include <vector> 11 #include <bitset> 12 #include <algorithm> 13 #include <iostream> 14 using namespace std; 15 #define ll long long 16 const int maxn=1e3+10; 17 18 struct node 19 { 20 int x,y; 21 }f[300]; 22 23 int shu[300],w; 24 25 void cal(ll n) 26 { 27 if (n==0) 28 w=0; 29 else 30 { 31 cal(n>>1); 32 shu[++w]=n & 1; 33 } 34 } 35 36 int main() 37 { 38 ll n; 39 int g,i,j,k; 40 while (~scanf("%lld",&n)) 41 { 42 w=0; 43 cal(n); 44 45 g=0; 46 j=1; 47 k=70; 48 49 for (i=2;i<=w;i++) 50 { 51 ///(k+1)->(k+2) 52 f[++g]={k+1,k+2}; 53 if (shu[i]) 54 { 55 f[++g]={j,k+2}; 56 j+=2; 57 } 58 k+=2; 59 } 60 f[++g]={k,199}; 61 f[++g]={j,70}; 62 f[++g]={j+1,j+1}; 63 if (n==0) 64 f[++g]={199,199}; 65 66 printf("%d\n",g); 67 for (i=1;i<=g;i++) 68 printf("%d %d\n",f[i].x,f[i].y); 69 } 70 return 0; 71 } 72 /* 73 0 74 1 75 2 76 3 77 4 78 79 4294967296 80 4294967295 81 4294967294 82 83 */
#1878 : Palindromes
找规律。
Java TLE了,难受!
关于字符串的处理,以后别用Java写。。。
1 #include <cstdio> 2 #include <cstdlib> 3 #include <ctime> 4 #include <cstring> 5 #include <string> 6 #include <map> 7 #include <set> 8 #include <list> 9 #include <queue> 10 #include <vector> 11 #include <bitset> 12 #include <algorithm> 13 #include <iostream> 14 using namespace std; 15 const int maxn=1e6+10; 16 17 int shu[maxn],ori[maxn]; 18 char str[maxn]; 19 20 int main() 21 { 22 int t,len,i; 23 scanf("%d",&t); 24 while (t--) 25 { 26 scanf("%s",str); 27 len=strlen(str); 28 for (i=1;i<=len;i++) 29 shu[i]=str[len-i]-48; 30 if (str[0]>'1') 31 { 32 ///odd 33 for (i=1;i<=len-1;i++) 34 ori[i]=0; 35 ori[len]=2; 36 37 for (i=1;i<=len;i++) 38 { 39 shu[i]-=ori[i]; 40 if (shu[i]<0) 41 { 42 shu[i]+=10; 43 shu[i+1]--; 44 } 45 } 46 47 printf("%c",shu[len]+1+48); 48 for (i=len-1;i>=1;i--) 49 printf("%c",shu[i]+48); 50 if (len>1) 51 { 52 for (i=2;i<=len-1;i++) 53 printf("%c",shu[i]+48); 54 printf("%c",shu[len]+1+48); 55 } 56 printf("\n"); 57 } 58 else if (len==1) 59 printf("0\n"); 60 else if (str[1]=='0') 61 { 62 ///odd 63 for (i=1;i<=len-2;i++) 64 ori[i]=0; 65 ori[len-1]=2; 66 67 for (i=1;i<=len;i++) 68 { 69 shu[i]-=ori[i]; 70 if (shu[i]<0) 71 { 72 shu[i]+=10; 73 shu[i+1]--; 74 } 75 } 76 77 printf("%c",shu[len-1]+1+48); 78 for (i=len-2;i>=1;i--) 79 printf("%c",shu[i]+48); 80 if (len-1>1) 81 { 82 for (i=2;i<=len-2;i++) 83 printf("%c",shu[i]+48); 84 printf("%c",shu[len-1]+1+48); 85 } 86 printf("\n"); 87 } 88 else 89 { 90 ///even 91 for (i=1;i<=len-2;i++) 92 ori[i]=0; 93 ori[len-1]=1; 94 ori[len]=1; 95 96 for (i=1;i<=len;i++) 97 { 98 shu[i]-=ori[i]; 99 if (shu[i]<0) 100 { 101 shu[i]+=10; 102 shu[i+1]--; 103 } 104 } 105 106 printf("%c",shu[len-1]+1+48); 107 for (i=len-2;i>=1;i--) 108 printf("%c",shu[i]+48); 109 for (i=1;i<=len-2;i++) 110 printf("%c",shu[i]+48); 111 printf("%c",shu[len-1]+1+48); 112 printf("\n"); 113 } 114 } 115 return 0; 116 }
#1877 : Approximate Matching
得到所有与串S相似(相等)的串,组成一颗树,AC自动机,预处理每个节点遇到'0','1'到达的节点。
统计每个点在某一个位置出现的次数,每次每个点遇到'0','1',到达新的节点。
当到达叶子节点(某一个串的终止位置)时,总结果加上 该点的出现次数*2^k(k为还没确定的位置数目),同时,该节点的出现次数设置为0(以后再不出现)。
1 #include <bits/stdc++.h>
2 using namespace std;
3 #define ll long long
4 #define minv 1e-6
5 #define inf 1e9
6 #define pi 3.1415926536
7 #define nl 2.7182818284
8 const ll mod=1e9+7;//998244353
9 const int maxn=1e5+10;
10
11 ///at most 40 strings, each string contain 40 characters
12
13 struct node
14 {
15 int c,en;
16 ll v[2];
17 node *pre;
18 node *next[2];
19 node *fail;
20 node *num[2];
21 };
22
23 ll er[50];
24 char str[50];
25 queue<node*> q;
26
27 int main()
28 {
29 node *tr,*pos,*be,*p,*d;
30 int n,m,x,y,t,i,j,k,c;
31 ll result;
32 er[0]=1;
33 for (i=1;i<=40;i++)
34 er[i]=er[i-1]<<1;
35
36 scanf("%d",&t);
37 while (t--)
38 {
39 scanf("%d%d",&n,&m);
40 scanf("%s",str);
41 if (n>m)
42 {
43 printf("0\n");
44 continue;
45 }
46 tr=(node*) malloc (sizeof(node));
47 for (i=0;i<2;i++)
48 tr->next[i]=NULL;
49 tr->en=0,tr->v[0]=0,tr->v[1]=0;
50
51 for (i=0;i<=n;i++)
52 {
53 if (i!=n)
54 str[i]=(str[i]=='0')?'1':'0';
55
56 pos=tr;
57 for (j=0;j<n;j++)
58 {
59 c=str[j]-48;
60 if (pos->next[c])
61 pos=pos->next[c];
62 else
63 {
64 p=(node*) malloc (sizeof(node));
65 for (k=0;k<2;k++)
66 p->next[k]=NULL;
67 p->en=0,p->v[0]=0,p->v[1]=0;
68 p->c=c;
69
70 pos->next[c]=p;
71 p->pre=pos;
72 pos=p;
73 }
74 }
75 pos->en=1;
76
77 if (i!=n)
78 str[i]=(str[i]=='0')?'1':'0';
79 }
80
81 be=(node*) malloc (sizeof(node));
82 for (i=0;i<2;i++)
83 be->next[i]=tr;
84 tr->pre=be;
85 tr->num[0]=tr,tr->num[1]=tr;///???
86
87 ///长度相等,不用不停fail判断是否存在拥有结尾标志的点
88 q.push(tr);
89 while (!q.empty())
90 {
91 d=q.front();
92 q.pop();
93 if (d==tr)
94 d->fail=be;
95 else
96 {
97 pos=d->pre->fail;
98 c=d->c;
99 while (pos->next[c]==NULL)
100 pos=pos->fail;
101 d->fail=pos->next[c];
102
103 for (j=0;j<2;j++)
104 if (d->fail->next[j])
105 d->num[j]=d->fail->next[j];
106 else
107 d->num[j]=d->fail->num[j];
108 }
109 for (j=0;j<2;j++)
110 if (d->next[j])
111 q.push(d->next[j]);
112 }
113
114 result=0;
115 x=0,y=1;
116 tr->v[0]=1;
117 for (i=1;i<=m;i++)
118 {
119 q.push(tr);
120 while (!q.empty())
121 {
122 d=q.front();
123 for (j=0;j<2;j++)
124 if (d->next[j])
125 d->next[j]->v[y]+=d->v[x];
126 else
127 d->num[j]->v[y]+=d->v[x];
128 q.pop();
129 for (j=0;j<2;j++)
130 if (d->next[j])
131 q.push(d->next[j]);
132 }
133
134 q.push(tr);
135 while (!q.empty())
136 {
137 d=q.front();
138 // printf("%d ",d->v[y]);
139 d->v[x]=0;
140 if (d->en==1)
141 {
142 result+=d->v[y]*er[m-i];
143 d->v[y]=0;
144 }
145 q.pop();
146 for (j=0;j<2;j++)
147 if (d->next[j])
148 q.push(d->next[j]);
149 }
150 // printf("\n");
151
152 x=x^1,y=y^1;
153 }
154 printf("%lld\n",result);
155 }
156 return 0;
157 }