题目:http://poj.org/problem?id=3167
题意:给一个模式串,按照模式串的大小对应关系,找出匹配串有相同大小对应关系的子串。利用指针,下标可以直接一一对应。
View Code
#include <cstdio> #include <cstring> #include <vector> using namespace std; const int N = 100000+10; const int M = 25000+10; int n, k, s, at; int a[N], b[M]; int d[30], low[M], high[M], path[M], ans[N]; bool cmp(int *aa, int *bb, int k) { if (aa[d[bb[k]]] != aa[k]) return false; if (low[k]>=0&&aa[low[k]]>=aa[k]) return false; if (high[k]>=0&&aa[high[k]]<=aa[k]) return false; return true; } int main() { //freopen("D:/a.txt", "r", stdin); scanf("%d%d%d", &n, &k, &s); for (int i=1; i<=s; i++)d[i]=-1; d[0] = d[s+1] = -2, at=0; for (int i=0; i<n; i++) scanf("%d", &a[i]); for (int i=0, j; i<k; i++) { scanf("%d", &b[i]); if (d[b[i]]==-1) d[b[i]]=i; for (j=b[i]-1; d[j]==-1; j--); low[i] = d[j]; for (j=b[i]+1; d[j]==-1; j++); high[i] = d[j]; } path[0] = -1; for (int i=1,j=-1; i<k; i++) { while (j>=0&&!cmp(b+(i-j-1),b,j+1))j=path[j]; if (cmp(b+(i-j-1),b,j+1))j++; path[i] = j; } for (int i=0,j=-1; i<n; i++) { while (j>=0&&!cmp(a+(i-j-1),b,j+1))j=path[j]; if (cmp(a+(i-j-1),b,j+1))j++; if (j+1 == k) { ans[at++] = i-j+1; j = path[j]; } } printf("%d\n", at); for (int i=0; i<at; i++) printf("%d\n", ans[i]); return 0; }