时间限制:50000ms
单点时限:5000ms
内存限制:512MB

描写叙述

我们有一个字符串集合S,当中有N个两两不同的字符串。还有M个询问,每一个询问都会先给出一个字符串w,你须要回答下面三个问题:

1. 输出全部S的串中。能够由w恰好加入两个字符得到的串中。编号最小的3个。

2. 输出全部S的串中,能够由w改动不超过2个字符得到的串中,编号最小的3个。

3. 输出全部S的串中,能够由w删除恰好两个字符得到的串中,编号最小的3个。

字母能够加入在包含开头结尾在内的任何位置,比方在"abc"中加入"x"和"y"。可能能够得到"yxabc","aybxc","axbyc"等等的串。

全部字符串仅仅由小写字母构成。

输入

第一行两个数N和M,表示集合S中字符串的数量和询问的数量。

接下来N行。当中第i行给出S中第i个字符串。

第i个字符串的编号就是i。

接下来M行。当中第i行给出第i个询问串。

数据范围:

N,M<=10000。

S中字符串长度和<=100000。

全部询问中字符串长度和<=100000。

输出

对每一个询问输出三行,每行三个数,分别表示每一个问题编号最小的3个串的编号。从小到大排列。

假设不到3个串,则在最后用-1补到3个输出,比方假设结果是1和2,那么输出1 2 -1。假设S中没有满足条件的串,就输出-1 -1 -1。

例子输入
5 5
dsxmlkxp
asxglqkdxp
asxgavxp
asxglp
sxglkx
kebvpyky
hjpntqft
asxglkxp
polbmzgq
jdczlmtd

例子输出

-1
  -1    -1

   -1   -1    -1

   -1   -1    -1

   -1   -1    -1

  1   -1    -1

-1   -1    -1

2
   -1    -1

1     3    -1

4     5    -1

-1
  -1    -1

-1   -1    -1

-1   -1    -1

-1   -1    -1

   -1   -1    -1

-1   -1    -1

解题思路:

   跟上一题一样,仅仅是上一题的拓展。

代码:

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <string>
#include <vector>
#include <map>
#include <set>
using namespace std;
const int maxn=100000+100;
char s[maxn];
const int Max=27;
struct node
{
int sign;
int next[Max];
} a[maxn];
set<int> ve;
int cur=0;
void insert(char *s,int flg)
{
int len,ans;
int p=0;
len=strlen(s);
for(int i=0; i<len; i++)
{
ans=s[i]-'a';
if(a[p].next[ans]!=0)
{
p=a[p].next[ans];
}
else
{
a[p].next[ans]=++cur;
a[cur].sign=0;
p=a[p].next[ans];
}
}
a[p].sign=flg;
}
int len;
void find0(int sign,int p,int x)
{
if(x==len&&sign==2&&a[p].sign)
{
ve.insert(a[p].sign);
return;
}
if(sign<2&&x<=len)
{
for(int i=0; i<26; i++)
{
if(a[p].next[i]>0)
{
int tp=a[p].next[i];
find0(sign+1,tp,x);
}
}
}
if(x<len)
{
int ne=s[x]-'a';
if(a[p].next[ne]>0)
{
int tp=a[p].next[ne];
find0(sign,tp,x+1);
}
}
}
void find1(int sign,int p,int x)
{
if(x==len&&sign<=2&&a[p].sign)
{
ve.insert(a[p].sign);
return;
}
int ne=s[x]-'a';
if(sign<2&&x<=len)
{
for(int i=0; i<26; i++)
{
if(a[p].next[i]>0&&i!=ne)
{
int tp=a[p].next[i];
find1(sign+1,tp,x+1);
}
}
}
if(x<len)
{
//int ne=s[x]-'a';
if(a[p].next[ne]>0)
{
int tp=a[p].next[ne];
find1(sign,tp,x+1);
}
}
}
void find2(int sign,int p,int x)
{
if(x+(2-sign)==len&&sign<=2&&a[p].sign)
{
ve.insert(a[p].sign);
return;
}
int ne=s[x]-'a';
if(sign<=1)
find2(sign+1,p,x+1);
if(x<len)
{
if(a[p].next[ne]>0)
{
int tp=a[p].next[ne];
find2(sign,tp,x+1);
}
}
}
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
for(int i=0; i<maxn; i++)
{
cur=0;
a[i].sign=0;
memset(a[i].next,0,sizeof(a[i].next));
}
for(int i=0; i<n; i++)
{
scanf("%s",s);
insert(s,i+1);
}
for(int i=0; i<m; i++)
{
ve.clear();
scanf("%s",s);
len=strlen(s);
find0(0,0,0);
int lo=ve.size();
set<int>::iterator it;
if(lo>0)
it=ve.begin();
for(int j=0;j<3;j++)
{
if(j!=0)
printf(" ");
if(j>=lo)
printf("-1");
else
{
printf("%d",*it);
it++;
}
}
printf("\n");
ve.clear();
find1(0,0,0);
lo=ve.size();
if(lo>0)
it=ve.begin();
for(int j=0;j<3;j++)
{
if(j!=0)
printf(" ");
if(j>=lo)
printf("-1");
else
{
printf("%d",*it);
it++;
}
}
printf("\n");
ve.clear();
find2(0,0,0);
// sort(ve.begin(),ve.end());
lo=ve.size();
if(lo>0)
it=ve.begin();
for(int j=0;j<3;j++)
{
if(j!=0)
printf(" ");
if(j>=lo)
printf("-1");
else
{
printf("%d",*it);
it++;
}
}
printf("\n");
} }
return 0;
}

hihoCoder 1261 String Problem II的更多相关文章

  1. hihocoder #1260 : String Problem I

    题目链接   时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 我们有一个字符串集合S,其中有N个两两不同的字符串. 还有M个询问,每个询问给出一个字符串w,求有多少S中的 ...

  2. (string高精度)A + B Problem II hdu1002

    A + B Problem II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  3. hdu1032 Train Problem II (卡特兰数)

    题意: 给你一个数n,表示有n辆火车,编号从1到n,入站,问你有多少种出站的可能.    (题于文末) 知识点: ps:百度百科的卡特兰数讲的不错,注意看其参考的博客. 卡特兰数(Catalan):前 ...

  4. HDU 1002 A + B Problem II

    A + B Problem II   Time Limit: 1000MS      Memory Limit: 65536K Total Submissions: 16104    Accepted ...

  5. A + B Problem II

    之前总是在查阅别人的文档,看着其他人的博客,自己心里总有一份冲动,想记录一下自己学习的经历.学习算法有一段时间了,于是想从算法开始自己的博客生涯O(∩_∩)O~~ 今天在网上看了一道大数相加(高精度) ...

  6. nyoj 103 A + B problem II

    点击打开链接 A+B Problem II 时间限制:3000 ms  |  内存限制:65535 KB 难度:3 描述 I have a very simple problem for you. G ...

  7. hdu 1023 Train Problem II

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1212 Train Problem II Description As we all know the ...

  8. HDU1002 -A + B Problem II(大数a+b)

    A + B Problem II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  9. hdoj 1002 A + B Problem II

    A + B Problem II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

随机推荐

  1. Redis学习篇(六)之ZSet类型及其操作

    ZADD 作用: 将元素及其分数添加到集合中 语法: ZADD key score membre [score member] 当集合元素已经存在时,再次添加会更新其分数 当score是 +inf 时 ...

  2. 【BZOJ 3958】 3958: [WF2011]Mummy Madness (二分+扫描线、线段树)

    3958: [WF2011]Mummy Madness Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 96  Solved: 41 Descripti ...

  3. FFTW3学习笔记2:FFTW(快速傅里叶变换)中文参考

    据说FFTW(Fastest Fourier Transform in the West)是世界上最快的FFT.为了详细了解FFTW以及为编程方便,特将用户手册看了一下,并结合手册制作了以下FFTW中 ...

  4. Topcoder Srm 726 Div1 Hard

    Topcoder Srm 726 Div1 Hard 解题思路: 问题可以看做一个二分图,左边一个点向右边一段区间连边,匹配了左边一个点就能获得对应的权值,最大化所得到的权值的和. 然后可以证明一个结 ...

  5. 【MPI】矩阵向量乘法

    输入作乘法的次数K 然后输入矩阵和向量的维度n 然后输入一个n维向量 然后输入K个n阶方阵 程序会给出该向量连续与此K个方阵做乘法后的结果 主要用了MPI_Gather, MPI_Allgather, ...

  6. [CodeForces-763C]Timofey and remoduling

    题目大意: 告诉你一个长度为n的等差数列在模m意义下的乱序值(互不相等),问是否真的存在满足条件的等差数列,并尝试构造任意一个这样的数列. 思路: 首先我们可以有一个结论: 两个等差数列相等,当且仅当 ...

  7. 让你的chrome开发工具console支持jquery

    首先执行以下代码: ;(function(d,s){d.body.appendChild(s=d.createElement('script')).src='http://code.jquery.co ...

  8. 使用openssl生成双向加密证书(转)

    要生成证书的目录下建立几个文件和文件夹,有./demoCA/./demoCA/newcerts/./demoCA/private/./demoCA/index.txt (空文件,生成证书时会将数据记录 ...

  9. HDU 5652 India and China Origins 二分+并查集

    India and China Origins 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5652 Description A long time ...

  10. UESTCACM 每周算法讲堂 延迟标记+bfs dfs搜索入门

    http://www.bilibili.com/video/av4163472/ 地址在上面~