两个子序列(?标识符)

Problem 1 两个子序列(twosub.pas/c/cpp)

【题目描述】

将给定的n个长度为M的01串序列{a1,a2…an} 分为两个子序列{b1,b2…bk}和{c1,c2…cm},m+k=n,使|f(b1,b2…bk)|+|f(c1,c2…cm)|最小化。

f函数定义如下:

f(空序列)=空串
       f(s)=s
       f(s1,s2)=最小长度的有一个前缀等于s1并且有一个后缀等于s2的字符串。        f(a1,a2,…,an)=f(f(a1,a2,…,an-1),an)。

 

【输入格式】

第一行两个整数n。

接下来n行每行一个长度为M的01串,表示a1,a2...an。

 

【输出格式】

输出一个整数,表示答案。

 

【样例输入】

4
000
111
110
001

【样例输出】

8

【数据范围】

对于30%的数据,n<=20

对于60%的数据,n<=2000

对于100%的数据,1<=n<=200000,1<=M<=20

 

 这题要用后缀DP

f[i][j]表示非当前枚举点结尾的那个字符串结尾为i



#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<cmath>
#include<iostream>
using namespace std;
#define MAXN (200000+10)
#define MAXM (20+10)
#define MAXSum (1<<20)
int n,m,f[MAXSum][MAXM],a[MAXN],bin[MAXM];
char s[MAXM];
int main()
{
	freopen("twosub.in","r",stdin);
	freopen("twosub.out","w",stdout);
	scanf("%d",&n);
	memset(f,128,sizeof(f));
	memset(a,0,sizeof(a));
	for (int i=1;i<=n;i++)
	{
		scanf("%s",s);
		m=strlen(s);
		for (int j=0;j<m;j++) a[i]=(a[i]<<1)+s[j]-48;
	}
	int tot=0;
	bin[0]=0;
	for (int i=1;i<=20;i++) bin[i]=(1<<i)-1;
	int ans=n*m;
	for (int i=2;i<=n;i++)
	{
		int k=m;
		while (k&&((bin[k]&a[i-1])!=a[i]>>(m-k)))
		{
		//	cout<<(bin[k]&a[i-1])<<' '<<a[i]<<m-k<<(a[i]>>(m-k))<<endl;
			k--;
		}
	//	cout<<k<<endl;
		ans-=k;
		int temp=0;
		for (int j=0;j<=m;j++) temp=max(temp,f[a[i]>>j][m-j]+m-j);
		for (int j=0;j<=m;j++) f[a[i-1]&bin[j]][j]=max(f[a[i-1]&bin[j]][j],temp-k);
	}
	int temp=0;
	for (int j=0;j<=m;j++)
		for (int i=0;i<=bin[j];i++) temp=max(temp,f[i][j]);
	cout<<ans-temp;
	return 0;
}

HDU 1075(What Are You Talking About-Trie的插入和查找)

What Are You Talking About

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/204800 K (Java/Others)
Total Submission(s): 8571    Accepted Submission(s): 2696



Problem Description
Ignatius is so lucky that he met a Martian yesterday. But he didn't know the language the Martians use. The Martian gives him a history book of Mars and a dictionary when it leaves. Now Ignatius want to translate the history book into English. Can you help
him?
 


Input
The problem has only one test case, the test case consists of two parts, the dictionary part and the book part. The dictionary part starts with a single line contains a string "START", this string should be ignored, then some lines follow, each line contains
two strings, the first one is a word in English, the second one is the corresponding word in Martian's language. A line with a single string "END" indicates the end of the directory part, and this string should be ignored. The book part starts with a single
line contains a string "START", this string should be ignored, then an article written in Martian's language. You should translate the article into English with the dictionary. If you find the word in the dictionary you should translate it and write the new
word into your translation, if you can't find the word in the dictionary you do not have to translate it, and just copy the old word to your translation. Space(' '), tab('t'), enter('n') and all the punctuation should not be translated. A line with a single
string "END" indicates the end of the book part, and that's also the end of the input. All the words are in the lowercase, and each word will contain at most 10 characters, and each line will contain at most 3000 characters.
 


Output
In this problem, you have to output the translation of the history book.
 


Sample Input
START from fiwo hello difh mars riwosf earth fnnvk like fiiwj END START difh, i'm fiwo riwosf. i fiiwj fnnvk! END
 


Sample Output
hello, i'm from mars. i like earth!
Hint
Huge input, scanf is recommended.
 


Author
Ignatius.L
 

Trie的插入和查找

话说也不难啊……

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<functional>
#include<cstdlib>
#include<iostream>
#include<cctype>
using namespace std;
#define MAXWordlen (10+10)
#define MAXLen (3000+10)
char s[MAXLen];
char word[MAXWordlen],word1[MAXWordlen];
struct node
{
	node *next[26+1];
	char t[MAXWordlen];
	node()
	{
		t[0]=0;
		for (int i=0;i<=26;i++) next[i]=NULL;
	}
}root;
void insert(char *str,char str1[])
{
	int len=strlen(str);
	node *p=&root;
	for (int i=0;i<len;i++)
	{
		if (p->next[str[i]-'a']==NULL) p->next[str[i]-'a']=new node();
		p=p->next[str[i]-'a'];
	}
	strcpy(p->t,str1);
}
node* find(int l,int r)
{
	node *p=&root;
	for (int i=l;i<=r;i++)
	{
		if (p->next[s[i]-'a']==NULL) return 0;
		p=p->next[s[i]-'a'];
	}
	if (strcmp(p->t,"")) return p;
	return 0;
}
int main()
{
	scanf("START");
	while (scanf("%s",word)&&strcmp(word,"END"))
	{
		scanf("%s",word1);
		insert(word1,word);
	}
	gets(word);
	scanf("START");
	gets(word);
	while (gets(s))
	{
		if (!strcmp(s,"END")) break;
		int len=strlen(s);
		s[len+1]=0;
		for (int i=0;i<len;i++)
		{
			if (islower(s[i]))
			{
				int j=i;
				while (islower(s[j+1])) j++;
				node *p=find(i,j);
				if (p) printf("%s",p->t);
				else
				{
					for (int k=i;k<=j;k++) printf("%c",s[k]);
				}
				i=j;
			}
			else printf("%c",s[i]);
		}
		printf("n");
	}
	return 0;
}