A(Phone Number)
1.字典树
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <queue>
#include <vector>
#include <algorithm>
#define inf 0x3f3f3f3f
using namespace std;
int n;
bool F;
char tu[1010][20];
int cmp(const void *a,const void *b)
{
return strcmp((char *)b,(char *)a);
}
typedef struct Node
{
int f;
struct Node *next[15];
}Node,*Tree;
void Creat(Tree &T)
{
T=(Tree)malloc(sizeof(Node));
for(int i=0;i<15;i++)
{
T->next[i]=NULL;
}
T->f=0;
}
void in(Tree &T,char *s)
{
int k=strlen(s);
int t;
Tree p=T;
for(int i=0;i<k;i++)
{
t=s[i]-'0';
if(p->next[t]==NULL)
Creat(p->next[t]);
p=p->next[t];
p->f++;
}
if(p->f>1)
F=true;
}
int main()
{
Tree T;
while(scanf("%d",&n)!=EOF&&n!=0)
{
Creat(T);
F=false;
for(int i=0;i<n;i++)
{
scanf("%s",tu[i]);
}
qsort(tu,n,sizeof(tu[0]),cmp);
for(int i=0;i<n;i++)
{
//cout<<tu[i]<<endl;
in(T,tu[i]);
}
if(F)
{
printf("NO\n");
}
else printf("YES\n");
}
return 0;
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
2.暴力
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
using namespace std;
int cmp(const void *a,const void *b)
{
return strcmp((char *)a,(char *)b);
}
char a[1001][1001];
int main()
{
int N,len,tt;
while(scanf("%d",&N),N)
{
tt=0;
for(int i = 0;i<N;i++)
scanf("%s",a[i]);
int flag = 1;
qsort(a,N,sizeof(a[0]),cmp);
for(int i = 0;i<N&&flag;i++)
{
len = strlen(a[i]);
for(int j = 0;j<len&&flag;j++)
{
for(int k = i+1;k<N;k++)
{
if(a[i][j]==a[k][j])
{
tt++;
break;
}
else tt=0;
}
if(tt==len)
flag=0;
}
}
printf("%s",flag?"YES":"NO");
printf("\n");
}
return 0;
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
View Code
3.KMP
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
using namespace std;
char a[1001][12];
int next[100001];
int cmp(const void *a,const void *b)
{
return strcmp((char *)a,(char *)b);
}
void Getnext(char s[])
{
int l=strlen(s);
next[0]=-1;
int j=-1;
int i=0;
while(i<l)
{
if(j==-1||s[i]==s[j])
{
i++;
j++;
next[i]=j;
}
else j=next[j];
}
}
int main()
{
int n,flag=0;
while(scanf("%d",&n)!=EOF&&n!=0)
{
flag=0;
for(int i=0; i<n; i++)
scanf("%s%*c",a[i]);
qsort(a,n,sizeof(a[0]),cmp);
for(int i=1; i<n; i++)
{
Getnext(a[i-1]);
int k=0,k1=0;
while(k<strlen(a[i])&&k1<strlen(a[i-1]))
{
if(k1==-1||a[i][k]==a[i-1][k1])
{
k++;
k1++;
}
else k1=next[k1];
}
if(k1-strlen(a[i-1])==0)
{
printf("NO\n");
flag=1;
break;
}
}
if(flag==0) printf("YES\n");
}
return 0;
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
View Code
B( Balloons)
简单搜索
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <queue>
#include <vector>
#include <algorithm>
#include <queue>
#define inf 0x3f3f3f3f
typedef long long ll;
using namespace std;
char tu[110][110];
int v[110][110];
int n;
ll cn1,cn2;
struct node
{
int x,y,st;
};
void dfs(int r,int c)
{
if(c<n-1&&tu[r][c+1]=='1'&&v[r][c+1]==0)
{
v[r][c+1]=1;
dfs(r,c+1);
}
if(r<n-1&&tu[r+1][c]=='1'&&v[r+1][c]==0)
{
v[r+1][c]=1;
dfs(r+1,c);
}
if(c>0&&tu[r][c-1]=='1'&&v[r][c-1]==0)
{
v[r][c-1]=1;
dfs(r,c-1);
}
if(r>0&&tu[r-1][c]=='1'&&v[r-1][c]==0)
{
v[r-1][c]=1;
dfs(r-1,c);
}
}
void df(int r,int c)
{
if(c<n-1&&tu[r][c+1]=='1'&&v[r][c+1]==0)
{
v[r][c+1]=1;
df(r,c+1);
}
if(r<n-1&&tu[r+1][c]=='1'&&v[r+1][c]==0)
{
v[r+1][c]=1;
df(r+1,c);
}
if(c>0&&tu[r][c-1]=='1'&&v[r][c-1]==0)
{
v[r][c-1]=1;
df(r,c-1);
}
if(r>0&&tu[r-1][c]=='1'&&v[r-1][c]==0)
{
v[r-1][c]=1;
df(r-1,c);
}
if(r>0&&c>0&&tu[r-1][c-1]=='1'&&v[r-1][c-1]==0)
{
v[r-1][c-1]=1;
df(r-1,c-1);
}
if(r>0&&c<n-1&&tu[r-1][c+1]=='1'&&v[r-1][c+1]==0)
{
v[r-1][c+1]=1;
df(r-1,c+1);
}
if(r<n-1&&c>0&&tu[r+1][c-1]=='1'&&v[r+1][c-1]==0)
{
v[r+1][c-1]=1;
df(r+1,c-1);
}
if(r<n&&c<n-1&&tu[r+1][c+1]=='1'&&v[r+1][c+1]==0)
{
v[r+1][c+1]=1;
df(r+1,c+1);
}
}
int main()
{
int K=0;
while(scanf("%d",&n)!=EOF&&n!=0)
{
for(int i=0;i<n;i++)
{
scanf("%s",tu[i]);
}
cn1=0;
cn2=0;
memset(v,0,sizeof(v));
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(tu[i][j]=='1'&&v[i][j]==0)
{
v[i][j]=1;
dfs(i,j);
cn1++;
}
}
}
memset(v,0,sizeof(v));
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(tu[i][j]=='1'&&v[i][j]==0)
{
v[i][j]=1;
df(i,j);
cn2++;
}
}
}
printf("Case %d: %lld %lld\n",++K,cn1,cn2);
printf("\n");
}
return 0;
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.
- 89.
- 90.
- 91.
- 92.
- 93.
- 94.
- 95.
- 96.
- 97.
- 98.
- 99.
- 100.
- 101.
- 102.
- 103.
- 104.
- 105.
- 106.
- 107.
- 108.
- 109.
- 110.
- 111.
- 112.
- 113.
- 114.
- 115.
- 116.
- 117.
- 118.
- 119.
- 120.
- 121.
- 122.
- 123.
- 124.
- 125.
- 126.
- 127.
D(shopping)
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
using namespace std;
int cmp(const void *a,const void *b)
{
return *(int *)a-*(int *)b;
}
int a[100001];
int main()
{
int N;
while(scanf("%d",&N)!=EOF&&N!=0)
{
for(int i=0; i<N; i++)
scanf("%d",&a[i]);
qsort(a,N,sizeof(a[0]),cmp);
printf("%d\n",2*(a[N-1]-a[0]));
}
return 0;
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
View Code
E(Emergency)(好题,Floy)
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#define inf 0x3f3f3f3f
typedef long long ll;
using namespace std;
int n,m,q;
int tu[310][310];
bool h[310];
int main()
{
int xx,yy,zz,K=0;
while(scanf("%d%d%d",&n,&m,&q)!=EOF)
{
if(n==0&&m==0&&q==0) break;
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
tu[i][j]=inf;
tu[i][i]=0;
}
memset(h,0,sizeof(h));
for(int i=0; i<m; i++)
{
scanf("%d%d%d",&xx,&yy,&zz);
if(tu[xx][yy]>zz)
tu[xx][yy]=zz;
}
printf("Case %d:\n",++K);
while(q--)
{
scanf("%d",&zz);
if(zz==0)
{
scanf("%d",&xx);
if(h[xx])
{
printf("City %d is already recaptured.\n",xx);
continue;
}
h[xx]=1;
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
{
if(tu[i][xx]!=inf&&tu[xx][j]!=inf&&tu[i][j]>tu[i][xx]+tu[xx][j])
{
tu[i][j]=tu[i][xx]+tu[xx][j];
}
}
}
}
else
{
scanf("%d%d",&xx,&yy);
if(h[xx]==0||h[yy]==0)
{
printf("City %d or %d is not available.\n",xx,yy);
continue;
}
if(tu[xx][yy]==inf)
{
printf("No such path.\n");
}
else printf("%d\n",tu[xx][yy]);
}
}
printf("\n");
}
return 0;
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
H(Hello World!)
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <queue>
#include <vector>
#include <algorithm>
#define inf 0x3f3f3f3f
using namespace std;
int n;
struct node
{
int l,r;
}q[1000010];
int main()
{
int K=0;
bool F;
int l,r,d1,d2;
while(scanf("%d",&n)&&n!=0)
{
printf("Case %d:\n",++K);
for(int i=0;i<n;i++)
{
scanf("%d%d",&q[i].l,&q[i].r);
}
for(int i=0;i<n;i++)
{
l=-1;
r=-1;
d1=q[i].l;
d2=q[i].r;
F=true;
for(int i=0;i<n;i++)
{
if(q[i].l>d1&&q[i].r>d2)
{
if(F)
{
l=q[i].l;
r=q[i].r;
F=false;
}
else
{
if(l>q[i].l)
{
l=q[i].l;
r=q[i].r;
}
else if(l==q[i].l&&r>q[i].r)
{
r=q[i].r;
}
}
}
}
printf("%d %d\n",l,r);
}
printf("\n");
}
return 0;
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.