前些日子 事情有点多没怎么总结 今天一口气总结下吧。
not sub sequence
TimeLimit: 1 Second MemoryLimit: 32 Megabyte
Totalsubmit: 145 Accepted: 48
Description
You have known how to find the longest common subsequence of two sequences. Today you are not going to solve this problem, but to find the shortest subsequence that is not in a sequence. For example, given a sequence of "ABCDEFG", the shortest subsequence not appearing in the sequence is BA, CB, CA and so on.
Input
The input contains multiple test cases, each a line consisting of a sequence of capital letters. The length of each sequence is N(2<=N<=999999).
Output
For each test case, output the length of shortest subsequence that not appearing in the sequence given.
Sample Input
ABCDEFG
DADAADDD
CDBBDCB
Sample Output
2
4
3
第一次做到这么诡异的dp。。。。 最后连状态转移方程都不会写。。。。。
在n个字符时候需要最好MAX长度的字符串
如果遇到前面没有出现过字母,那么MAX更改为2 MAX位置改变到当前
如果遇到前面的字母了,只有当所有前面的字母在MAX位置后都出现了一次,MAX+1,MAX位置改变到当前位置(注意这里MAX位置为最后一次改变MAX值的位置)
我的代码:
#include <cstdio>
#include <cstring>
using namespace std;
bool vis1[26],vis2[26];
char s[120000];
int ans=0;
int bj(bool vis1[26],bool vis2[26]){
for (int i=0;i<26;i++){
if (vis1[i]!=vis2[i]) return 0;
}
return 1;
}
int main (){
int n;
while (scanf("%s",s)!=EOF){
n=strlen(s);
memset(vis1,0,sizeof(vis1));
memset(vis2,0,sizeof(vis2));
vis1[s[0]-'A']=true;ans=2;
for (int i=1;i<n;i++){
vis2[s[i]-'A']=true;
if (vis1[s[i]-'A']==false){
vis1[s[i]-'A']=true;
ans=2;
memset(vis2,0,sizeof(vis2));
}
else if (bj(vis1,vis2)){
ans++;
memset(vis2,0,sizeof(vis2));
}
}
printf("%d\n",ans);
}
return 0;
}