Eefun Guessing Words

Eefun is currently learning to read. His way of learning  is unique, by trying to make every possible substring from a given string. However, when the string becomes longer, he can no longer remember all the substring. His friends want to test Eefun's skill by asking questions, given a string S, is there any substring that begins with letter X and ends with letter Y? According to Eefun, each substring contains at least 2 letters. As the given string S is pretty big, Eefun need your help to answer his friend's questions

Input

First line of input contain a single string S which was given by his friends.
Second line contain a number N, the number of questions that Eefun's friends ask.
Next N lines each consists of 2 characters, X and Y

Output

For each questions, output a single string "YA" if the substring exists, or "TIDAK" otherwise
(YA means yes and TIDAK means no in Indonesian)

Example

Input:
HALO
4
H O
L O
A O
O L 
Output:
YA
YA
YA
TIDAK 

Constraints:

  • 'A' ≤ X,Y ≤ 'Z'
  • 1 ≤ |S| ≤ 1.000.000
  • 1 ≤ N ≤ 1.000.000

记一下每个字母第一次和最后一次出现的位置就好了

 #include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<queue>
#include<deque>
#include<set>
#include<map>
#include<ctime>
#define LL long long
#define inf 0x7ffffff
#define pa pair<int,int>
#define mkp(a,b) make_pair(a,b)
#define pi 3.1415926535897932384626433832795028841971
#define mod 100007
using namespace std;
inline LL read()
{
LL x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int fst[],lst[];
char s[],x,y;
int n,q;
int main()
{
while (~scanf("%s",s+))
{
memset(fst,,sizeof(fst));
memset(lst,,sizeof(lst));
n=strlen(s+);
for (int i=;i<=n;i++)
{
if (!fst[s[i]])fst[s[i]]=i;
lst[s[i]]=i;
}
q=read();
for (int i=;i<=q;i++)
{
scanf(" %c %c",&x,&y);
if (fst[x]&&fst[y]&&fst[x]<lst[y])puts("YA");
else puts("TIDAK");
}
}
}

Spoj NPC2015A

最新文章

  1. Singleton模式和Mono-State模式
  2. JQueryEasyUI datagrid框架的基本使用
  3. Android:将View的内容映射成Bitmap转图片导出
  4. Ajax 之【文件上传】
  5. Android-ViewPagerIndicator-master 、Android-PullToRefresh 学习篇
  6. Vm image download resource
  7. Mac RTX
  8. Objective-C相关Category的收集
  9. How to Find the Self Service Related File Location and Versions
  10. jsp篇 之 指令元素和动作元素
  11. 关于apache 开启 ssl https 支持 TLS1.2 的些事
  12. Ajax与跨域Ajax
  13. devexpress WinForms MVVM
  14. PHP 如何创建守护(daemon)进程
  15. 转:写的不错的eclipse配置cdt的文章
  16. LayUI把表格中的时间戳改成格式化的时间
  17. SQL 注入,永不过时的黑客技术
  18. [JS&amp;Jquery]实现页面表格中相同内容的行或列合并
  19. Spark源码分析 &ndash; SchedulerBackend
  20. new ,malloc

热门文章

  1. 从零开发分布式数据库中间件 二、构建MyBatis的读写分离数据库中间件
  2. PostgreSQL 的日期函数用法举例
  3. 51nod 1526 分配笔名
  4. codevs 1390 回文平方数 USACO
  5. SQL增删查改语句
  6. 原创 :xftp SFTP子系统申请已拒绝 请确保SSH链接的SFTP子系统设置有效
  7. win10 多桌面 win+tab | ctrl+win+左右箭头
  8. zust_第二周——瞎扯系列
  9. ELK日志分析 学习笔记
  10. java常考小程序