您的位置:首页 > 其它

USACO 2.3 The Longest Prefix

2011-05-05 20:06 405 查看
TASK: prefix
LANG: C++

Compiling...
Compile: OK

Executing...
Test 1: TEST OK [0.000 secs, 3408 KB]
Test 2: TEST OK [0.000 secs, 3408 KB]
Test 3: TEST OK [0.000 secs, 3408 KB]
Test 4: TEST OK [0.000 secs, 3408 KB]
Test 5: TEST OK [0.000 secs, 3408 KB]
Test 6: TEST OK [0.702 secs, 3408 KB]

All tests OK.

/*
PROG: prefix
ID: jiafeim1
LANG: C++
*/

#include <algorithm>
//#include <iostream>
#include <fstream>
#include <string.h>
#include <stdio.h>
using namespace std;

char sub_string[202][15];
long len_sub_string[202];
long max_sub_len = 0;
char str[200005];
bool can[200005]={false};
int top = 0;
int main()
{
//std::ifstream fin("prefix.in");
//   std::ofstream fout("prefix.out");

FILE* fin = fopen("prefix.in","r");
FILE* fout = fopen("prefix.out","w");

for(;;++top)
{
fscanf(fin,"%s",&sub_string[top][0]);
if(sub_string[top][0] == '.') break;
len_sub_string[top] = strlen(sub_string[top]);
if(max_sub_len<len_sub_string[top])
max_sub_len = len_sub_string[top];
}
char temp_str[80];
str[0]=0;
while(fscanf(fin,"%s",temp_str)!=-1)
strcat(str,temp_str);
long len = strlen(str);
long max = -1;
for(long i = 0 ; i!=len;++i)
{
if(i-max>max_sub_len) break;
char t = str[i+1];
str[i+1]=0;

for(int now_sub = 0;now_sub!=top;++now_sub)
{
if(i+1<len_sub_string[now_sub]) continue;
if((i-len_sub_string[now_sub] == -1 || can[i-len_sub_string[now_sub]]) && !strcmp(str+i+1-len_sub_string[now_sub],sub_string[now_sub]))
{
max = i;
can[i]= true;
break;
}
}
str[i+1]=t;
}
fprintf(fout,"%ld\n",max+1);
fclose(fin);
fclose(fout);
//   fin.close();
//fout.close();

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: