**
A - Xiper的奇妙历险(1)
**
题意:给你一个数n(貌似只给了9。。。)让你输出n皇后的不冲突的方法总数,以及输出全部的解。
**
//#include <bits/stdc++.h>
//#define _ ios_base::sync_with_stdio(0);cin.tie(0);
#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <bitset>
#include <string>
#include <numeric>
#include <algorithm>
#include <functional>
#include <iterator>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <complex>
#include <ctime>
/*
int gcd(int a,int b)
{
return ! b ? a : gcd(b,a % b);
}
struct data
{
int val;
int pos;
int ranks;
}p[2005];
typedef struct
{
double x,y;
}Point;
bool cmp1(const data &a,const data &b)
{
if(a.val == b.val)
return a.pos < b.pos;
return a.val < b.val;
}
bool cmp2(const data &a,const data &b)
{
return a.val > b.val;
}
bool cmp3(const data &a,const data &b)
{
return a.pos < b.pos;
}
const double ERR = 0.0001;
bool feq (double a, double b)
{
return fabs(a-b) < ERR;
}
double fgcd(double a, double b)
{
if (feq(a, 0))
return b;
if (feq(b, 0))
return a;
return fgcd(b, fmod(a, b));
}
double dist(double x0, double x1, double y0, double y1)
{
return sqrt((x1-x0)*(x1-x0) + (y1-y0)*(y1-y0));
}
*/
#define INF 0x3f3f3f3f
#define eps 1e-6
typedef long long LL;
const double pi = acos(-1.0);
const long long mod = 1e9 + 7;
using namespace std;
int t = 0;
int x[1005];
int p[100005][10];
bool place(int k)
{
for(int i = 1;i < k;i++)
if(x[k] == x[i] || abs(k - i) == abs(x[k] - x[i]))
return false;
return true;
}
void Queue(int n)
{
int i,k;
memset(x,0,sizeof(x));
k = 1;
while(k > 0)
{
x[k] = x[k] + 1;
while(x[k] <= n && !place(k))
x[k] = x[k] + 1;
if(x[k] <= n && k == n)
{
for(i = 1;i <= n;i++)
p[t][i] = x[i];
t++;
}
else if(x[k] <= n && k < n)
k = k + 1;
else
{
x[k] = 0;
k = k - 1;
}
}
}
int main()
{
int n;
scanf("%d",&n);
Queue(n);
printf("%d\n",t);
for(int i = 0;i < t;i++)
{
for(int j = 1;j <= n;j++)
printf("%d ",p[i][j]);
puts("");
}
return 0;
}
B - Xiper的奇妙历险(2)
**
迷宫问题
**
//#include <bits/stdc++.h>
//#define _ ios_base::sync_with_stdio(0);cin.tie(0);
#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <bitset>
#include <string>
#include <numeric>
#include <algorithm>
#include <functional>
#include <iterator>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <complex>
#include <ctime>
/*
int gcd(int a,int b)
{
return ! b ? a : gcd(b,a % b);
}
struct data
{
int val;
int pos;
int ranks;
}p[2005];
typedef struct
{
double x,y;
}Point;
bool cmp1(const data &a,const data &b)
{
if(a.val == b.val)
return a.pos < b.pos;
return a.val < b.val;
}
bool cmp2(const data &a,const data &b)
{
return a.val > b.val;
}
bool cmp3(const data &a,const data &b)
{
return a.pos < b.pos;
}
const double ERR = 0.0001;
bool feq (double a, double b)
{
return fabs(a-b) < ERR;
}
double fgcd(double a, double b)
{
if (feq(a, 0))
return b;
if (feq(b, 0))
return a;
return fgcd(b, fmod(a, b));
}
double dist(double x0, double x1, double y0, double y1)
{
return sqrt((x1-x0)*(x1-x0) + (y1-y0)*(y1-y0));
}
*/
#define INF 0x3f3f3f3f
#define eps 1e-6
typedef long long LL;
const double pi = acos(-1.0);
const long long mod = 1e9 + 7;
using namespace std;
const int a = 100;
const int dl[] = {1,-1,0,0,0,0};
const int dr[] = {0,0,1,-1,0,0};
const int dc[] = {0,0,0,0,1,-1};
int L, R, C, d[a][a][a], v[a][a][a];
char m[a][a][a];
struct S
{
int l, r, c;
S(int l, int r, int c):l(l),r(r),c(c) {}
};
int bfs(int l1, int r1, int c1)
{
queue<S> Q;
d[l1][r1][c1] = 0;
v[l1][r1][c1] = 1;
Q.push(S(l1, r1, c1));
while(!Q.empty())
{
S s = Q.front();
Q.pop();
for(int i = 0; i < 6; i++)
{
int newl = s.l + dl[i];
int newr = s.r + dr[i];
int newc = s.c + dc[i];
if(newl >= 0 && newl < L && newr >= 0 && newr < R && newc >= 0 && newc < C && m[newl][newr][newc] != 'x' && !v[newl][newr][newc])
{
if(newl == s.l - 1)
{
if(m[newl][newr][newc] == 'U' && m[s.l][s.r][s.c] == 'D')
{
Q.push(S(newl, newr, newc));
v[newl][newr][newc] = 1;
d[newl][newr][newc] = d[s.l][s.r][s.c] + 1;
if(m[newl][newr][newc] == 'Y')
return d[newl][newr][newc];
}
}
else if(newl == s.l + 1)
{
if(m[newl][newr][newc] == 'D' && m[s.l][s.r][s.c] == 'U')
{
Q.push(S(newl, newr, newc));
v[newl][newr][newc] = 1;
d[newl][newr][newc] = d[s.l][s.r][s.c] + 1;
if(m[newl][newr][newc] == 'Y')
return d[newl][newr][newc];
}
}
else
{
Q.push(S(newl, newr, newc));
v[newl][newr][newc] = 1;
d[newl][newr][newc] = d[s.l][s.r][s.c] + 1;
if(m[newl][newr][newc] == 'Y')
return d[newl][newr][newc];
}
}
}
}
return -1;
}
int main()
{
scanf("%d %d %d", &L, &R, &C);
int l1, r1, c1;
for(int i = 0; i < L; i++)
for(int j = 0; j < R; j++)
{
scanf("%s", m[i][j]);
for(int k = 0; k < C; k++)
if(m[i][j][k] == 'X')
{
l1 = i;
r1 = j;
c1 = k;
}
}
memset(v, 0, sizeof(v));
int ans = bfs(l1, r1, c1);
if(ans >= 0)
printf("%d\n", ans);
else
printf("-1\n");
return 0;
}
C - Xiper的奇妙历险(3)
**
八数码问题,有多组数据
**
//#include <bits/stdc++.h>
//#define _ ios_base::sync_with_stdio(0);cin.tie(0);
#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <bitset>
#include <string>
#include <numeric>
#include <algorithm>
#include <functional>
#include <iterator>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <complex>
#include <ctime>
/*
int gcd(int a,int b)
{
return ! b ? a : gcd(b,a % b);
}
struct data
{
int val;
int pos;
int ranks;
}p[2005];
typedef struct
{
double x,y;
}Point;
bool cmp1(const data &a,const data &b)
{
if(a.val == b.val)
return a.pos < b.pos;
return a.val < b.val;
}
bool cmp2(const data &a,const data &b)
{
return a.val > b.val;
}
bool cmp3(const data &a,const data &b)
{
return a.pos < b.pos;
}
const double ERR = 0.0001;
bool feq (double a, double b)
{
return fabs(a-b) < ERR;
}
double fgcd(double a, double b)
{
if (feq(a, 0))
return b;
if (feq(b, 0))
return a;
return fgcd(b, fmod(a, b));
}
double dist(double x0, double x1, double y0, double y1)
{
return sqrt((x1-x0)*(x1-x0) + (y1-y0)*(y1-y0));
}
*/
#define INF 0x3f3f3f3f
#define eps 1e-6
typedef long long LL;
const double pi = acos(-1.0);
const long long mod = 1e9 + 7;
using namespace std;
struct data
{
int code;
int state[9];
};
char num[10];
int fact[] = {1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880};
int dx[] = { -1, 1, 0, 0 };
int dy[] = { 0, 0, -1, 1 };
int vis[5000000];
int dist[5000000];
data s,goal;
int encode(int *state)
{
int code = 0;
for(int i = 0;i < 9;i++)
{
int cnt = 0;
for(int j = 0;j < i;j++)
{
if(state[j] > state[i])
cnt++;
}
code += fact[i] * cnt;
}
return code;
}
void BFS()
{
queue <data> q;
q.push(goal);
vis[goal.code] = 1;
while(!q.empty())
{
data f = q.front();
q.pop();
int z = 0;
while(f.state[z] != 0)
z++;
int x = z / 3,y = z % 3;
for(int d = 0;d < 4;d++)
{
int newx = x + dx[d];
int newy = y + dy[d];
int newz = newx * 3 + newy;
if(newx >= 0 && newx < 3 && newy >= 0 && newy < 3)
{
data r = f;
r.state[z] = f.state[newz];
r.state[newz] = 0;
r.code = encode(r.state);
if(!vis[r.code])
{
vis[r.code] = 1;
dist[r.code] = dist[f.code] + 1;
q.push(r);
}
}
}
}
}
int main()
{
//freopen("int.txt","r",stdin);
//freopen("out.txt","w",stdout);
memset(vis,0,sizeof(vis));
memset(dist,0,sizeof(dist));
for(int i = 0; i < 9; i ++)
goal.state[i] = (i + 1) % 9;
goal.code = encode(goal.state);
BFS();
int T;
scanf("%d",&T);
while(T--)
{
for(int i = 0;i < 9;i++)
{
cin >> num[i];
if(num[i] == 'x')
s.state[i] = 0;
else
s.state[i] = num[i] - '0';
}
s.code = encode(s.state);
if(!vis[s.code])
printf("-1\n");
else
printf("%d\n",dist[s.code]);
}
return 0;
}
E - 吴队长征婚
**
**
//#include <bits/stdc++.h>
//#define _ ios_base::sync_with_stdio(0);cin.tie(0);
#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <bitset>
#include <string>
#include <numeric>
#include <algorithm>
#include <functional>
#include <iterator>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <complex>
#include <ctime>
/*
int gcd(int a,int b)
{
return ! b ? a : gcd(b,a % b);
}
struct data
{
int val;
int pos;
int ranks;
}p[2005];
typedef struct
{
double x,y;
}Point;
bool cmp1(const data &a,const data &b)
{
if(a.val == b.val)
return a.pos < b.pos;
return a.val < b.val;
}
bool cmp2(const data &a,const data &b)
{
return a.val > b.val;
}
bool cmp3(const data &a,const data &b)
{
return a.pos < b.pos;
}
const double ERR = 0.0001;
bool feq (double a, double b)
{
return fabs(a-b) < ERR;
}
double fgcd(double a, double b)
{
if (feq(a, 0))
return b;
if (feq(b, 0))
return a;
return fgcd(b, fmod(a, b));
}
double dist(double x0, double x1, double y0, double y1)
{
return sqrt((x1-x0)*(x1-x0) + (y1-y0)*(y1-y0));
}
*/
#define INF 0x3f3f3f3f
#define eps 1e-6
typedef long long LL;
const double pi = acos(-1.0);
const long long mod = 1e9 + 7;
using namespace std;
int t = 0;
int x[1005];
int p[100005][10];
#include<cstdio>
#include<algorithm>
using namespace std;
int N,s,n,l;
int S[64],L[64];
int dfs(int c,int r,int P)
{
if (c == n)
return 1;
for(int i = P;i >= 0;i--)
{
if(S[i])
continue;
if(L[i] == r)
{
S[i] = 1;
if(dfs(c + 1,l,N - 1))
return 1;
S[i] = 0;
return 0;
}
if(L[i] < r)
{
S[i] = 1;
if(dfs(c,r - L[i],i - 1))
return 1;
S[i] = 0;
if(r == l)
return 0;
while(L[i] == L[i - 1] && i > 0)
i--;
}
}
return 0;
}
int main()
{
while(scanf("%d", &N) && N)
{
s = 0;
for (int i = 0;i < N;i++)
{
scanf("%d",&L[i]);
s += L[i];
}
sort(L,L + N);
for (l = L[N - 1];l <= s;l++)
if (s % l == 0)
{
memset(S,0,sizeof(S));
n = s / l;
if(dfs(1,l,N - 1))
{
printf("%d\n",l);
break;
}
}
}
return 0;
}
I - 谭爷剪花布条
**
给你两个串P,T。让你找出P中含有不重叠的T的数量最大是多少,KMP改下就好
//#include <bits/stdc++.h>
//#define _ ios_base::sync_with_stdio(0);cin.tie(0);
#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <bitset>
#include <string>
#include <numeric>
#include <algorithm>
#include <functional>
#include <iterator>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <complex>
#include <ctime>
/*
int gcd(int a,int b)
{
return ! b ? a : gcd(b,a % b);
}
struct data
{
int val;
int pos;
int ranks;
}p[2005];
typedef struct
{
double x,y;
}Point;
bool cmp1(const data &a,const data &b)
{
if(a.val == b.val)
return a.pos < b.pos;
return a.val < b.val;
}
bool cmp2(const data &a,const data &b)
{
return a.val > b.val;
}
bool cmp3(const data &a,const data &b)
{
return a.pos < b.pos;
}
const double ERR = 0.0001;
bool feq (double a, double b)
{
return fabs(a-b) < ERR;
}
double fgcd(double a, double b)
{
if (feq(a, 0))
return b;
if (feq(b, 0))
return a;
return fgcd(b, fmod(a, b));
}
double dist(double x0, double x1, double y0, double y1)
{
return sqrt((x1-x0)*(x1-x0) + (y1-y0)*(y1-y0));
}
*/
#define INF 0x3f3f3f3f
#define eps 1e-6
typedef long long LL;
const double pi = acos(-1.0);
const long long mod = 1e9 + 7;
using namespace std;
char p[100005],t[100005];
vector <int> KMP(char *p,char *t)
{
int n = strlen(p);
//printf("%d\n",n);
vector <int> next(n + 1,0);
for(int i = 1;i < n;i++)
{
int j = i;
while(j > 0)
{
j = next[j];
if(p[j] == p[i])
{
next[i + 1] = j + 1;
break;
}
}
}
vector <int> q;
int m = strlen(t);
//printf("%d\n",m);
for(int i = 0,j = 0;i < m;i++)
{
if(j < n && t[i] == p[j])
j++;
else
{
while(j > 0)
{
j = next[j];
if(t[i] == p[j])
{
j++;
break;
}
}
}
if(j == n)
q.push_back(i - n + 1);
}
return q;
}
int main()
{
//freopen("int.txt","r",stdin);
//freopen("out.txt","w",stdout);
scanf("%s %s",t,p);
vector <int> l = KMP(p,t);
if(l.size() == 0)
printf("0\n");
else
{
stack <int> st;
st.push(l[0]);
//for(int i = 0;i < l.size();i++)
//printf("%d ",l[i]);
//puts("");
for(int i = 1;i < l.size();i++)
{
if(l[i] - st.top() >= strlen(p))
st.push(l[i]);
}
printf("%d\n",st.size());
}
return 0;
}
K - 卿大爷的三个女友
找到一个最长的子串使得它既是前缀又是后缀,还在中间,求这个子串的长度。
KMP中next数组的理解
//#include <bits/stdc++.h>
//#define _ ios_base::sync_with_stdio(0);cin.tie(0);
#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <bitset>
#include <string>
#include <numeric>
#include <algorithm>
#include <functional>
#include <iterator>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <complex>
#include <ctime>
#define INF 0x3f3f3f3f
#define eps 1e-6
typedef long long LL;
const double pi = acos(-1.0);
const long long mod = 1e9 + 7;
using namespace std;
char s[100005];
int Next[100005];
int f[100005];
int main()
{
//freopen("int.txt","r",stdin);
//freopen("out.txt","w",stdout);
int T;
scanf("%d",&T);
while(T--)
{
scanf("%s",s);
int N = strlen(s);
memset(Next, 0, sizeof(Next));
memset(f, 0, sizeof(f));
int t1 = 0, t2 = -1;
Next[0] = -1;
while(t1 < N)
{
if(t2 == -1 || s[t1] == s[t2])
Next[++t1] = ++t2;
else
t2 = Next[t2];
}
int ans = 0;
int t = N;
while(t)
{
if(N >= t * 2)
f[t] = 1;
t = Next[t];
}
for(int i = 0;i < N;i++)
{
t = i;
while(t > 0)
{
if(f[t] && i >= t * 2 && N >= i + t)
{
ans = max(t, ans);
break;
}
t = Next[t];
}
}
printf("%d\n",ans);
}
return 0;
}