您的位置:首页 > 其它

uva 11019 题目数据不合法!

2016-08-28 12:39 337 查看
经过试验,题目数据中,有各种不合法情况,只能使用scanf %s进行读入。否则会出现各种问题。

即使用网上别人的标程,进行修改为gets一样会挂。

还有网上一些同学的程序对拍后出现错误。。也AC了,数据太弱。

我的程序十分缓慢。。。现在正在研究原因。。(2300ms)

直接AC自动机强行跑每一行的串即可

#include<cstring>
#include <algorithm>
#include <cmath>
#include <iostream>
#include<queue>
#include<cstdio>
#include<map>
#include<string>
using namespace std;

const int SIGMA_SIZE = 26;
const int MAXNODE = 10010;

#define prln(x) cout<<#x<<" = "<<x<<endl
#define pr(x) cout<<#x<<" = "<<x<<" "

int n, m, X, Y;

/*
* AC自动机,令g[i,j]表示从i到j这一路遍历的所有字符串。 f[i]的意义就是g[?,i]和g[0,f[i]]的字符串是相等的
* last[i] ,表示g[0,last[i]]的字符串,是确定存在的,并且以last[i]结尾的字符串*/

struct AhoCorasickAutomata {
int ch[MAXNODE][SIGMA_SIZE];
int f[MAXNODE]; // fail函数
int val[MAXNODE]; // 每个字符串的结尾结点都有一个非0的val
int last[MAXNODE]; // 输出链表的下一个结点
int cnt[1010][1010];
int sz;
queue<int>q;
int g[MAXNODE];//i节点,能匹配到哪个串
int pipei[1100];

void init() {
sz = 1;
memset(ch[0], 0, sizeof(ch[0]));
//memset(cnt, 0, sizeof(cnt));
memset(pipei,-1,sizeof(pipei));
}

// 字符c的编号
int idx(char c)
{
//if (c == '\0') return 62;
/*
if (c >= '0' && c <= '9') return c - '0';
if (c >= 'a' && c <= 'z') return c - 'a' + 10;
return c - 'A' + 36;
*/
return c-'a';
}

// 插入字符串。v必须非0
void insert(char s[], int len, int id) {
int now = 0;
for(int i = 0; i < len; i++) {
int c = idx(s[i]);
if(!ch[now][c]) {
memset(ch[sz], 0, sizeof(ch[sz]));
val[sz] = 0;
g[sz] = -1;
ch[now][c] = sz++;
}
now = ch[now][c];
}
val[now] =1;//这里有单词
pipei[id] = g[now];
g[now] = id;
}

// 在T中找模板,text串的下标从0开始,长度为len
void find(char text[], int len, int hang) {
int j = 0; // 当前结点编号,初始为根结点
for(int i = 0; i < len; i++) { // 文本串当前指针
int c = idx(text[i]);
j = ch[j][c];
if(val[j])
{
for (int tmp = g[j]; tmp != -1; tmp = pipei[tmp])
{

int lie = i;
if (hang - tmp < 0)continue;
if (lie-Y+1<0)continue;
cnt[hang-tmp][lie-Y+1]++;
}
}
}
}

//计算fail指针
void get_fail()
{
f[0] = 0;//fail[i]表示,当匹配到某个位置失败,下一个自动的位置
for (int c = 0; c < SIGMA_SIZE; c++)
{
int will = ch[0][c];
if (will)
{
f[will]=0;
q.push(will);
last[will] = 0;
}
}
while (!q.empty())
{
int now = q.front();
q.pop();
for (int c = 0; c < SIGMA_SIZE; ++ c)
{
int will = ch[now][c]; //now节点,想要访问的下标
if (!will)
{
ch[now][c] = ch[f[now]][c];
continue;
}
q.push(will);
int pre = f[now]; //失配指针,先指now的失配,至少有一段都是相等的
while (pre && !ch[pre][c]) pre = f[pre];//往前跳失配指针,类似 KMP
f[will] = ch[pre][c]; // f[i]的意义就是g[?,i]和g[0,f[i]]的字符串是相等的
last[will] = val[f[will]] ? f[will] : last[f[will]];
}
}
}

void doit()
{
int ans = 0;
for (int i = 0; i < n; ++ i)
for (int j = 0; j < m; ++ j)
if (cnt[i][j] == X) ++ans;
printf("%d\n", ans);
}
}ac;

char text[1010][1010];
char pattern[1100][1100];

int main() {
int T;
scanf("%d", &T);

while (T--)
{
scanf("%d%d ", &n, &m);

for (int i = 0; i <n;++i)
for (int j =0;j<m;++j)
ac.cnt[i][j]=0;

for (int i = 0; i < n; ++ i)
{
scanf("%s", text[i]);
}
scanf("%d%d ", &X, &Y);
for (int i = 0; i < X; ++ i)
{
scanf("%s", pattern[i]);
}

ac.init();
for (int i = 0; i < X; ++ i)
{
ac.insert(pattern[i], strlen(pattern[i]), i);
}
ac.get_fail();

for (int i = 0; i < n; ++ i)
{
ac.find(text[i], strlen(text[i]), i);
}
ac.doit();

}
return 0;
}

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