USACO 2.3.1The Longest Prefix


 prefix.cpp : 定义控制台应用程序的入口点。
//
/*
ID: maiyuet1
PROG: prefix
LANG: C++
*/
//#include "stdafx.h"
//一维DP可解
//下面的用字典树+BFS的代码,挂在第6组数据上。至今未解....
//ok[i] 表示前缀0...i 可完成
//ok[i] = ok[i-len] && s[i-len...i] == str[j]   //len = strlen(str[j])

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;

int ok[200010];

int main()
{
	freopen("prefix.in","r",stdin);
	freopen("prefix.out","w",stdout);	
	char str[210][11];
	char s[200010] = "";
	char word[1000];
	int ans = 0;
	int k = 0;
	memset(ok,0,sizeof(ok));
	while(1)
	{
		cin>>str[k];
		if(strcmp(str[k],".") == 0)
		{
			break;
		}
		k++;
	}
	while(cin>>word)
	{
		strcat(s,word);
	}

	int len1 = strlen(s);
	int len2;
	for(int i=0; i<len1; i++)
	{
		if(ok[i])
		{
			continue;
		}
		for(int j=0; j<k; j++)
		{
			len2 = strlen(str[j]);
			if(i < len2 - 1)
			{
				continue;
			}
			int ind = 0;
			if(i-len2 == -1 || ok[i-len2])
			{
				for(int k=i-len2+1; k<=i; k++)
				{
					word[ind++] = s[k];
				}
				word[ind] = '\0';
				if(strcmp(word,str[j]) == 0)
				{
					ok[i] = 1;
					if(i+1 > ans)
					{
						ans = i+1;
					}
					break;
				}
			}
		}
		
	}
	cout<<ans<<endl;
	return 0;
}


//
//const int N = 27;
//int used[200010];
//int maxPre;
//priority_queue<int>Q;
//
//typedef struct TireTree
//{
//	int sign; //标记是一个元素
//	struct TireTree *child[N];
//}TireNode;
//
//TireNode *root;
//
//void initRoot()
//{
//	root = (TireNode*)malloc(sizeof(TireNode));
//	for(int i=0; i<N; i++)
//	{
//		root->child[i] = NULL;
//	}
//	root->sign = 0;
//}
//
//void insert(char *word)
//{
	cout<<word<<endl;
//	int len;
//	TireNode *newNode,*currNode;
//	len = strlen(word);
//	if(len == 0)
//	{
//		return ;
//	}
//	currNode = root;
//	for(int i=0; i<len; i++)
//	{
//		if(currNode->child[word[i]-'A'] != NULL)
//		{
//			currNode = currNode->child[word[i]-'A'];
//		}
//		else
//		{
//			newNode = (TireNode*)malloc(sizeof(TireTree));
//			newNode->sign = 0;
//			for(int j=0; j<N; j++)
//			{
//				newNode->child[j] = NULL;
//			}
//			currNode->child[word[i]-'A'] = newNode;
//			currNode = newNode;
//		}
//	}
//	currNode->sign = 1;
//}
//
//void pushQ(int node)
//{
//	if(!used[node])
//	{
//		used[node] = 1;
//		Q.push(node);
//		if(node > maxPre)
//		{
//			maxPre = node;
//		}
//	}
//}
//
//void find(char *word, int k)
//{
//	int len = strlen(word);
//	TireNode *currNode = root;
//	for(int i=k; i<len; i++)
//	{
//		if(currNode->child[word[i]-'A'] != NULL)
//		{
//			currNode = currNode->child[word[i]-'A'];
//			if(currNode->sign)
//			{
//				pushQ(i+1);
//			}
//		}
//		else
//		{
//			break;
//		}
//	}
//	return ;
//}
//
//int main()
//{
//	freopen("prefix.in","r",stdin);
//	freopen("prefix.out","w",stdout);
//	char str[100];
//	char S[200010] = "";
//	maxPre = 0;
//	memset(used,0,sizeof(used));
//	initRoot();
//	while(cin>>str)
//	{
//		if(strcmp(str,".") == 0)
//		{
//			break;
//		}
//		insert(str);
//	}
//	while(scanf("%s",str)!=EOF)
//	{
//		strcat(S,str);
//	}
//	//cin>>S;
//	Q.push(0);
//	while(!Q.empty())
//	{
//		int node = Q.top();
//		Q.pop();
//		find(S,node);
//		if(maxPre == strlen(S))
//		{
//			break;
//		}
//	}
//	cout<<maxPre<<endl;
//	return 0;
//}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值