#166 (Div. 2)
题意:找到大于y的第一个每位都不相同的年。
View Code
#include <cstdio> #include <cstring> #include <algorithm> #include <cmath> using namespace std; #define clr(x) memset(x,0,sizeof(x)) int a[4]; int main() { int y; while (scanf("%d",&y)!=EOF) { y++; for (;;y++) { int x = y; a[0] = x/1000; x %= 1000; a[1] = x/100; x %= 100; a[2] = x/10; a[3] = x%10; sort(a,a+4); if (a[0]!=a[1] && a[1]!=a[2] && a[2]!=a[3]) { printf("%d\n",y); break; } } } return 0; }
题意:在一个n*m的格子中可以给任意的格子的数+1,问至少操作多少次可以使得其中某行或者某列全为质数。
分析:打个质数表,在判断就ok。
View Code
#include <cstdio> #include <cstring> #include <algorithm> #include <cmath> using namespace std; #define clr(x) memset(x,0,sizeof(x)) const int maxn = 101005; int p[maxn]; int g[505][505]; void init() { int i,j; clr(p); p[0]=p[1]=1; for (i=2; i<maxn; i++) { if (p[i]==0) { for (j=i*2; j<maxn; j+=i) p[j] = 1; } } } int c[maxn]; int r[maxn]; int mi[505][505]; int main() { int i, j, k, x; int n, m, tot; init(); clr(mi); clr(r),clr(c); while (scanf("%d %d",&n,&m)!=EOF) { for (i=0; i<n; i++) for (j=0; j<m; j++) { scanf("%d",&g[i][j]); x = g[i][j]; while (p[x]) x++; mi[i][j] = x-g[i][j]; } int res = 99999999; for (i=0; i<n; i++) { tot = 0; for (j=0; j<m; j++) tot += mi[i][j]; r[i] = tot; if (r[i]<res) res = r[i]; } for (i=0; i<m; i++) { tot = 0; for (j=0; j<n; j++) tot += mi[j][i]; c[i] = tot; if (c[i]<res) res = c[i]; } printf("%d\n",res); } return 0; }
C.Secret
题意:将一个集合分成k个集合,要求每个集合的数至少有一个大于3且不成等差数列。
View Code
#include <cstdio> #include <cstring> #include <algorithm> #include <cmath> using namespace std; #define clr(x) memset(x,0,sizeof(x)) int m[1000005]; int main() { int n,i, j, k; while (scanf("%d %d",&n,&k)!=EOF) { if (k*3>n) { printf("-1\n"); continue; } int x = n-n%k; int y = x-k; for (j=0; j<k; j++) { for (i=j+1; i<=y; i+=k) m[i] = j+1; } for (i=y+1; i<x; i++) m[i] = i-y+1; m[x] = 1; for (i=x+1; i<=n; i++) m[i] = i-x; for (i=1; i<=n; i++) printf("%d%c",m[i],i==n?'\n':' '); } return 0; }
题意:找到一个字符串中,包含的坏字符不超过k个的子串的个数。
分析:字典树。
字典树①
#include <cstdio> #include <cstring> #define clr(x) memset(x,0,sizeof(x)) struct node { int count; struct node*next[26]; }tt[2500000]; int tot; char s[1600]; char c[27]; int main() { int i, j, k, x; int n, m, res, sum; while (scanf("%s %s %d",s,c,&k)!=EOF) { res = 0; tot = 0; node *root = &tt[++tot]; clr(tt[tot].next); for (i=0; s[i]; i++) { node *p = root; sum = 0; for (j=i; s[j]; j++) { if (c[s[j]-'a']=='0') sum++; if (sum>k) break; x = s[j]-'a'; if (p->next[x]==NULL){ res++; p->next[x] = &tt[++tot]; clr(tt[tot].next); } p = p->next[x]; } } printf("%d\n",res); } return 0; }
字典树②
#include <cstdio> #include <cstring> int next[2500000][26]; char s[1600]; char c[27]; int main() { int i, j, k; int res, n, tot, sum, p, x; while (scanf("%s %s %d",s,c,&k)!=EOF) { res = 0; tot = 1; for (i=0; s[i]; i++) { sum = 0; p = 1; for (j=i; s[j]; j++) { if (c[s[j]-'a']=='0') sum++; if (sum>k) break; x = s[j]-'a'; if (next[p][x] == 0){ res++; next[p][x] = ++tot; } p = next[p][x]; } } printf("%d\n",res); } return 0; }
hash1
#include<iostream> #include<algorithm> #include <cstdlib> #include <cstdio> using namespace std; long long a[3000000],k,c; char s[1520],v[30]; int main() { //freopen("Data.in","r",stdin); while(cin>>s>>v>>k) { c = 0; for(int i=0;s[i];i++){ long long kk = k; long long h = 0; for(int j=i; s[j]&&(v[s[j]-'a']>'0'||kk--);j++) a[c++]=h=(h*131)^s[j]; } sort(a,a+c); cout<<unique(a,a+c)-a<<endl; } }