hdu 5763 题目链接
题意:给定两个字符串A和B,每个出现在A中的B(可以overlap)都有两种含义,问A串一共可能有多少种含义。
思路:kmp+dp.
考虑dp[i]为前i个字符(也就是从开始长度为i,注意不是字符串的下标为i)的含义数。
hdu 1867 题意 题意:给两个字符串,将两个字符串首尾拼接之后得到一个长度最短的字符串,求这个最短的字符串(一个串的前缀可能是另一个串的后缀,这样的话只出现一次就行了)
思路:kmp。。注意和hdu 1841区分。那道题是只要得到一个串包含两个串即可。这道题是首尾拼接得到。
hdu 1841题目链接 题意:给两个字符串,问包含这两个字符串的最小的字符串的长度(最小是因为,一个串的子串可能是另一个串的后缀,这样出现一次就可以了)
思路:其实这道题最关键的思想部分是和kmp没有关系的。。。
hdu 1358 题目链接
题意:给一个字符串,求这个字符串的每个前缀(包括本身)的能否由k个子串组成(K>1)
思路:和poj 2406 比较类似。。
结论:字符编号从0开始,如果又i%(i-next[i])==0,则i前面的 串为一个轮回串,其中轮回子串出现i/(i-next[i])次。
hdu 2087 题目链接
题意:问模式串在文本串中出现的次数,不允许重叠。
思路:kmp,关键在于不允许重叠。。。
其实只要每次找到的时候j=0一下就好咯。
代码实现 1/* *********************************************** 2Author :111qqz 3Created Time :2016年08月11日 星期四 02时52分44秒 4File Name :code/hdu/2087.cpp 5************************************************ */ 6 7#include <cstdio> 8#include <cstring> 9#include <iostream> 10#include <algorithm> 11#include <vector> 12#include <queue> 13#include <stack> 14#include <set> 15#include <map> 16#include <string> 17#include <cmath> 18#include <cstdlib> 19#include <deque> 20#include <ctime> 21#define fst first 22#define sec second 23#define lson l,m,rt<<1 24#define rson m+1,r,rt<<1|1 25#define ms(a,x) memset(a,x,sizeof(a)) 26typedef long long LL; 27#define pi pair < int ,int > 28#define MP make_pair 29 30using namespace std; 31const double eps = 1E-8; 32const int dx4[4]={1,0,0,-1}; 33const int dy4[4]={0,-1,1,0}; 34const int inf = 0x3f3f3f3f; 35const int N=1E3+7; 36char a[N],b[N]; 37int nxt[N]; 38 39void getnxt(char *s) 40{ 41 int i = 0; 42 int j = -1; 43 nxt[0] = -1; 44 int n = strlen(s); 45 while (i<n) 46 if (j==-1||s[i]==s[j]) nxt[++i]=++j; 47 else j = nxt[j]; 48} 49int kmp(char *a,char *b) 50{ 51 int n = strlen(a); 52 int m = strlen(b); 53 getnxt(b); 54 int i = 0; 55 int j = 0; 56 int cnt = 0 ; 57 while (i<n) 58 { 59 if (j==-1||a[i]==b[j]) i++,j++; 60 else j=nxt[j]; 61 if (j==m) 62 { 63 cnt++; 64 j=0; //不允许重叠 65 } 66 } 67 return cnt; 68 69} 70int main() 71{ 72 #ifndef ONLINE_JUDGE 73 freopen("code/in.txt","r",stdin); 74 #endif 75 76 while (~scanf("%s",a)) 77 { 78 if (a[0]=='#') break; 79 scanf("%s",b); 80 int ans = kmp(a,b); 81 printf("%d\n",ans); 82 } 83 84 #ifndef ONLINE_JUDGE 85 fclose(stdin); 86 #endif 87 return 0; 88}
hdu 1711 题目链接
题意:给定两个数列,问第二个数列在第一个数列中出现的位置(第一个元素对应的位置)
思路:数列也可以看成字符串,然后左kmp,返回的答案是i+1-m。。。1A
poj 3080 题目链接
题意:给出n个字符串(n<=10),字符串长度不超过70,问出现在全部n个字符串中的最长并且字典序最小的长度大于等于3的子串。
思路:数据范围很小。。。直接暴力枚举+kmp匹配一下。。。
poj 2185 题目链接
题意:给出一个字符矩形,问一个面积最小的矩形,覆盖掉整个矩形。大概就是二维的最小覆盖子串。
思路:对于每一行做最小覆盖子串,然后求lcm,每一列也是如此。最后记得判断不能超过原有的n,m。
参考资料(本文大部分是参考这篇博客,附带一些证明步骤的解释) 首先明确一些概念: 最小覆盖子串:对于某个字符串s,它的最小覆盖子串指的是长度最小的子串p,p满足通过自身的多次连接得到q,最后能够使s成为q的子串。 比如: 对于s=“abcab”,它的最小覆盖子串p=“abc”,因为p通过在它后面再接上一个p(即重叠0个字符),可以得到q=“abcabc”,此时s是q的子串。 对于s=“ababab”,它的最小覆盖子串为p=“ab”。
poj 2752题目链接
题意:求出所有的前缀和后缀相同的子串的长度。
思路:求出nxt函数,观察发现,从从len递归向前就是答案。
代码实现 1/* *********************************************** 2Author :111qqz 3Created Time :2016年08月10日 星期三 21时05分52秒 4File Name :code/poj/2752.cpp 5************************************************ */ 6 7#include <cstdio> 8#include <cstring> 9#include <iostream> 10#include <algorithm> 11#include <vector> 12#include <queue> 13#include <stack> 14#include <set> 15#include <map> 16#include <string> 17#include <cmath> 18#include <cstdlib> 19#include <deque> 20#include <ctime> 21#define fst first 22#define sec second 23#define lson l,m,rt<<1 24#define rson m+1,r,rt<<1|1 25#define ms(a,x) memset(a,x,sizeof(a)) 26typedef long long LL; 27#define pi pair < int ,int > 28#define MP make_pair 29 30using namespace std; 31const double eps = 1E-8; 32const int dx4[4]={1,0,0,-1}; 33const int dy4[4]={0,-1,1,0}; 34const int inf = 0x3f3f3f3f; 35const int N=4E6+7; 36int n; 37string a; 38int nxt[N]; 39void getnxt( int n) 40{ 41 int i = 0 ; 42 int j = -1 ; 43 nxt[0] = -1; 44 while (i<n) 45 if (j==-1||a[i]==a[j]) nxt[++i]=++j; 46 else j = nxt[j]; 47} 48void print( int x) 49{ 50 if (nxt[x]!=-1) 51 { 52 print(nxt[x]); 53 printf("%d ",x); 54 } 55} 56int main() 57{ 58 #ifndef ONLINE_JUDGE 59 freopen("code/in.txt","r",stdin); 60 #endif 61// ios::sync_with_stdio(false); 62 while (cin>>a) 63 { 64 int len = a.length(); 65 getnxt(len); 66 // for ( int i = 0 ; i < len ; i++) cout<<i<<" nxt[i]:"<<nxt[i]<<endl; 67 print(nxt[len]); 68 printf("%d\n",len); 69 70 } 71 72 #ifndef ONLINE_JUDGE 73 fclose(stdin); 74 #endif 75 return 0; 76}
hdu1686
题意:给出模式串和文本串,问模式串在文本串中出现了多少次,可以overlap.
思路:思考naive的匹配过程。nxt函数不过是改进了当失配发生时,不是移动1位,而是移动多位。nxt函数的含义是当失配发生时,移动到的位置….所以有的教程管这个叫失配函数吧,也不是很难理解的样子。
20170801update:当时竟然没有强调 next 函数的含义?
next[i] 的含义是,i 之前的整个前缀中,最长的「前缀和后缀相同」的长度。
看图:
KMP 感觉是我学到现在最难懂的一个算法了 QAQ 为什么你们都那么强啊,看几个小时就看懂了……
题目链接 傻逼模拟。。读完题就ac了。。。
代码实现 1/* *********************************************** 2Author :111qqz 3Created Time :2016年08月07日 星期日 18时04分18秒 4File Name :code/whust2016/#1/D.cpp 5************************************************ */ 6#include <cstdio> 7#include <cstring> 8#include <iostream> 9#include <algorithm> 10#include <vector> 11#include <queue> 12#include <stack> 13#include <set> 14#include <map> 15#include <string> 16#include <cmath> 17#include <cstdlib> 18#include <deque> 19#include <ctime> 20#define fst first 21#define sec second 22#define lson l,m,rt<<1 23#define rson m+1,r,rt<<1|1 24#define ms(a,x) memset(a,x,sizeof(a)) 25typedef long long LL; 26#define pi pair < int ,int > 27#define MP make_pair 28using namespace std; 29const double eps = 1E-8; 30const int dx4[4]={1,0,0,-1}; 31const int dy4[4]={0,-1,1,0}; 32const int inf = 0x3f3f3f3f; 33const int N=280; 34int n,m; 35int total,adva,advb; 36struct Friend 37{ 38 int money; 39 int adv; 40}f[N]; 41struct Room 42{ 43 int type; 44 int cost; 45 int adv; 46}r[N]; 47struct Ans 48{ 49 int val; 50 int rid; 51 int fid; 52 bool operator < (Ans b)const 53 { 54 return val>b.val; 55 } 56}ans[300*300]; 57int main() 58{ 59 #ifndef ONLINE_JUDGE 60 freopen("code/in.txt","r",stdin); 61 #endif 62 cin>>total>>adva>>advb; 63 cin>>n; 64 for ( int i = 1 ; i <= n ; i++) 65 scanf("%d %d",&f[i].money,&f[i].adv); 66 scanf("%d",&m); 67 for ( int i = 1 ; i <= m ; i++) 68 scanf("%d%d%d",&r[i].type,&r[i].cost,&r[i].adv); 69 int cnt = 0 ; 70 for ( int i = 1 ; i <= m ; i++) 71 { 72 if (r[i].type==1) 73 { 74 if (r[i].cost<=total) 75 { 76 cnt++; 77 ans[cnt].val = r[i].adv+adva; 78 ans[cnt].rid = i; 79 ans[cnt].fid = -1; 80 } 81 continue; 82 } 83 else 84 { 85 for ( int j = 0 ; j <= n ; j++) 86 { 87 if (j==0) //自己住双人间 88 { 89 if (r[i].cost<=total) 90 { 91 cnt++; 92 ans[cnt].val = r[i].adv+advb; 93 ans[cnt].rid = i ; 94 ans[cnt].fid = -1; 95 } 96 } 97 else 98 { 99 if (r[i].cost<=total*2&&r[i].cost<=f[j].money*2) 100 { 101 cnt++; 102 ans[cnt].val = r[i].adv+f[j].adv; 103 ans[cnt].rid = i ; 104 ans[cnt].fid = j; 105 } 106 } 107 } 108 } 109 } 110// for ( int i = 1 ; i <= cnt ; i++) 111// { 112// printf("val:%d room: %d friend : %d \n",ans[i].val,ans[i].rid,ans[i].fid); 113// } 114 if (cnt==0) 115 { 116 puts("Forget about apartments. Live in the dormitory."); 117 }else 118 { 119 sort(ans+1,ans+cnt+1); 120 if (r[ans[1].rid].type==1) 121 { 122 printf("You should rent the apartment #%d alone.\n",ans[1].rid); 123 } 124 else 125 { 126 if (ans[1].fid==-1) 127 { 128 printf("You should rent the apartment #%d alone.\n",ans[1].rid); 129 } 130 else 131 { 132 133 printf("You should rent the apartment #%d with the friend #%d.\n",ans[1].rid,ans[1].fid); 134 135 } 136 } 137 } 138 #ifndef ONLINE_JUDGE 139 fclose(stdin); 140 #endif 141 return 0; 142}
题目链接
其实就是括号匹配的模型。。用栈即可。。被我写挂好几发。。该死该死。。。
代码实现 1/* *********************************************** 2Author :111qqz 3Created Time :2016年08月07日 星期日 17时34分13秒 4File Name :code/whust2016/#1/HH.cpp 5************************************************ */ 6 7#include <cstdio> 8#include <cstring> 9#include <iostream> 10 11#include <algorithm> 12#include <vector> 13#include <queue> 14#include <stack> 15#include <set> 16#include <map> 17#include <string> 18#include <cmath> 19#include <cstdlib> 20#include <deque> 21#include <ctime> 22#include <cctype> 23#define fst first 24#define sec second 25#define lson l,m,rt<<1 26#define rson m+1,r,rt<<1|1 27#define ms(a,x) memset(a,x,sizeof(a)) 28typedef long long LL; 29#define pi pair < int ,int > 30#define MP make_pair 31 32using namespace std; 33const double eps = 1E-8; 34const int dx4[4]={1,0,0,-1}; 35const int dy4[4]={0,-1,1,0}; 36const int inf = 0x3f3f3f3f; 37const int N=1E4+7; 38char st[N]; 39int s[N]; 40int n; 41int plow[N]; 42int pup[N]; 43int ans[N]; 44stack<int>stk; 45 46 47bool ok(int x, int y) 48{ 49 // cout<<"x:"<<x<<" y:"<<y<<endl; 50 if (plow[x]>0&&pup[y]>0) 51 { 52 ans[pup[y]] = plow[x]; 53 return true; 54 } 55 if (plow[y]>0&&pup[x]>0) 56 { 57 ans[pup[x]] = plow[y]; 58 return true; 59 } 60 return false; 61} 62int main() 63{ 64 #ifndef ONLINE_JUDGE 65 freopen("code/in.txt","r",stdin); 66 #endif 67 68 cin>>n>>st; 69 n*=2; 70 int cntA = 0 ; 71 int cnta = 0 ; 72 ms(pup,0); 73 ms(plow,0); 74 for ( int i = 0 ; i < n ; i++) 75 { 76 if (st[i]>='a'&&st[i]<='z') 77 { 78 cnta++; 79 plow[i] = cnta; 80 } 81 else 82 { 83 cntA++; 84 pup[i] = cntA; 85 } 86 } 87// for ( int i = 0 ; i < n ; i++) cout<<"plow[i]:"<<plow[i]<<" pup[i]:"<<pup[i]<<endl; 88 for ( int i = 0 ; i < n ; i++) 89 { 90 st[i] = toupper(st[i]); 91 } 92// cout<<"st:"<<st<<endl; 93 ms(ans,-1); 94 while (!stk.empty()) stk.pop(); 95 stk.push(0); 96 for ( int i = 1 ; i < n ; i++) 97 { 98 while (!stk.empty()&&st[stk.top()]==st[i]&&ok(stk.top(),i)&&i<n) 99 { 100 stk.pop(); 101 i++; 102 } 103 stk.push(i); 104 } 105 for ( int i = 1 ; i <= n/2 ; i++) 106 { 107 if (ans[i]==-1) 108 { 109 puts("Impossible"); 110 return 0; 111 } 112 } 113 for ( int i = 1 ; i < n/2 ; i++) printf("%d ",ans[i]); 114 printf("%d\n",ans[n/2]); 115 116 #ifndef ONLINE_JUDGE 117 fclose(stdin); 118 #endif 119 return 0; 120}
hdu 3415 题意:给出n个整数,是一个环(也就是a[n]右边是a[1])求一段长度不超过k的数使得和最大,问最大和是多少并给出这段数的位置。
思路:为了处理环,先把n个数复制一下就好,然后求前缀和sum[i]
ural 1126 题意:n个数,求从第k个元素开始,求每k个元素的最大值(一共求n-k+1次) 思路:单调队列。 单调队列学习链接 其实单调队列挺容易的理解的。。。当时觉得写不明白大概是因为看到的代码写得太丑了2333
hdu 1559
题意:给你一个m×n的整数矩阵,在上面找一个x×y的子矩阵,使子矩阵中所有元素的和最大。
思路:二维前缀和就好。。。和单调栈没有半毛钱关系吧。。。
代码实现 1/* *********************************************** 2Author :111qqz 3Created Time :2016年08月03日 星期三 21时08分18秒 4File Name :code/hdu/1559.cpp 5************************************************ */ 6 7#include <cstdio> 8#include <cstring> 9#include <iostream> 10#include <algorithm> 11#include <vector> 12#include <queue> 13#include <stack> 14#include <set> 15#include <map> 16#include <string> 17#include <cmath> 18#include <cstdlib> 19#include <ctime> 20#define fst first 21#define sec second 22#define lson l,m,rt<<1 23#define rson m+1,r,rt<<1|1 24#define ms(a,x) memset(a,x,sizeof(a)) 25typedef long long LL; 26#define pi pair < int ,int > 27#define MP make_pair 28 29using namespace std; 30const double eps = 1E-8; 31const int dx4[4]={1,0,0,-1}; 32const int dy4[4]={0,-1,1,0}; 33const int inf = 0x3f3f3f3f; 34const int N=1E3+7; 35int a[N][N]; 36int sum[N][N]; 37int n,m,x,y; 38int main() 39{ 40 #ifndef ONLINE_JUDGE 41 freopen("code/in.txt","r",stdin); 42 #endif 43 int T; 44 cin>>T; 45 while (T--) 46 { 47 ms(sum,0); 48 scanf("%d%d%d%d",&n,&m,&x,&y); 49 for ( int i = 1 ; i <= n ; i++) 50 for ( int j = 1 ; j <= m ; j++) 51 { 52 scanf("%d",&a[i][j]); 53 sum[i][j] = sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1] + a[i][j]; 54 } 55 int ans = 0 ; 56 for ( int i = 1 ; i <= n-x+1 ; i++) 57 for ( int j = 1 ; j <= m-y+1 ; j++) 58 ans = max(ans,sum[i+x-1][j+y-1]-sum[i-1][j+y-1]-sum[i+x-1][j-1]+sum[i-1][j-1]); 59 60 printf("%d\n",ans); 61 } 62 63 #ifndef ONLINE_JUDGE 64 fclose(stdin); 65 #endif 66 return 0; 67}
poj 3494
题意:给出一个n*m个0-1图,求最大的全部由1组成的矩阵。
思路:同poj 1964,一共做n+2×m次单调栈。。。数组开小re一发。。某处stk忘记清空re 1发。。。智力-2.。。3A
poj 2796
题意:给出一个人n(1E5)天的情绪值(0..1E6),一段时间的value的定义是这段时间的情绪之和*这段时间情绪的最小值。
现在求value的最大值,并且输出得到这个最大值的区间。
poj 2082 题目链接
题意:这道题简直就是。。。教给大家怎么把一句话把简单的题让人出得看不懂。。。真的一点意思都没有。给出n个矩形的宽度和高度,这些矩形并排顺次排列在x轴上,问最大面积。