Spoj-NPC2015A Eefun Guessing Words
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
Spoj-NPC2015A Eefun Guessing Words的更多相关文章
- BZOJ 2588: Spoj 10628. Count on a tree [树上主席树]
2588: Spoj 10628. Count on a tree Time Limit: 12 Sec Memory Limit: 128 MBSubmit: 5217 Solved: 1233 ...
- SPOJ DQUERY D-query(主席树)
题目 Source http://www.spoj.com/problems/DQUERY/en/ Description Given a sequence of n numbers a1, a2, ...
- 2632: [neerc2011]Gcd guessing game
2632: [neerc2011]Gcd guessing game Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 144 Solved: 84[S ...
- SPOJ GSS3 Can you answer these queries III[线段树]
SPOJ - GSS3 Can you answer these queries III Description You are given a sequence A of N (N <= 50 ...
- 【填坑向】spoj COT/bzoj2588 Count on a tree
这题是学主席树的时候就想写的,,, 但是当时没写(懒) 现在来填坑 = =日常调半天lca(考虑以后背板) 主席树还是蛮好写的,但是代码出现重复,不太好,导致调试的时候心里没底(虽然事实证明主席树部分 ...
- SPOJ bsubstr
题目大意:给你一个长度为n的字符串,求出所有不同长度的字符串出现的最大次数. n<=250000 如:abaaa 输出: 4 2 1 1 1 spoj上的时限卡的太严,必须使用O(N)的算法那才 ...
- 【SPOJ 7258】Lexicographical Substring Search
http://www.spoj.com/problems/SUBLEX/ 好难啊. 建出后缀自动机,然后在后缀自动机的每个状态上记录通过这个状态能走到的不同子串的数量.该状态能走到的所有状态的f值的和 ...
- 【SPOJ 1812】Longest Common Substring II
http://www.spoj.com/problems/LCS2/ 这道题想了好久. 做法是对第一个串建后缀自动机,然后用后面的串去匹配它,并在走过的状态上记录走到这个状态时的最长距离.每匹配完一个 ...
- 【SPOJ 8222】Substrings
http://www.spoj.com/problems/NSUBSTR/ clj课件里的例题 用结构体+指针写完模板后发现要访问所有的节点,改成数组会更方便些..于是改成了数组... 这道题重点是求 ...
随机推荐
- vue.js与react.js相比较的优势
vue.js的简介 vue.js是一个javascript mvvm库,它是以数据驱动和组件化的思想构建的.我们平时多用js去操作dom,vue.js则是使用了数据绑定驱动来操作dom的,也就是说创建 ...
- 补充---spring多线程任务调度
在spring任务调度的基础上增加多线程 三种方式: (1)使用OpenSymphony Quartz 调度器 (2)使用JDK Timer支持类 (3)SpringTaskExecutor抽象 sp ...
- haml scss转换编写html css的前期工作
http://www.w3cplus.com/sassguide/install.html 先下载ruby $ gem sources $ gem sources --remove https://r ...
- leecode 旋转数组
描述 给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数. 示例 1: 输入: [1,2,3,4,5,6,7] 和 k = 3 输出: [5,6,7,1,2,3,4] 解释: 向右旋 ...
- 优化SQL语句的方法
首先,对于where语句的注意事项: 1.应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索引而进行全表扫描,如:select id from t where nu ...
- redux是全局状态(数据)的管理机制,局部数据没有意义
redux是全局状态(数据)的管理机制,局部数据没有意义
- hibernate4+spring3+struts2搭建框架实例
1.所需要的JAR包 2.web.xml配置文件,这个和平时的配置是一样的 <?xml version="1.0" encoding="UTF-8"?&g ...
- 身份证号正则校验(js校验+JAVA校验)
js校验身份证号[15位和18位] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 3 ...
- java在线聊天项目 swt可视化窗口Design 重新设计好友列表窗口 增加菜单栏
增加的菜单栏效果图如下: eclipse 中调整到 swt的design视图下 控件区域选择Menu Controls 将Menu Bar拖动到窗口标题栏 将Cascaded Menu拖动到Menu ...
- c++ 读取一行的2个数
#include <iostream> using namespace std; double harmonicMean(double x, double y); int main() { ...