1. 程式人生 > >Spoj-NPC2015A Eefun Guessing Words

Spoj-NPC2015A Eefun Guessing Words

pla cti names memset cst fun second math spl

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

記一下每個字母第一次和最後一次出現的位置就好了

技術分享
 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cstring>
 4 #include<cstdlib>
 5 #include<algorithm>
 6 #include<cmath>
 7
#include<queue> 8 #include<deque> 9 #include<set> 10 #include<map> 11 #include<ctime> 12 #define LL long long 13 #define inf 0x7ffffff 14 #define pa pair<int,int> 15 #define mkp(a,b) make_pair(a,b) 16 #define pi 3.1415926535897932384626433832795028841971 17 #define mod 100007 18 using namespace std; 19 inline LL read() 20 { 21 LL x=0,f=1;char ch=getchar(); 22 while(ch<0||ch>9){if(ch==-)f=-1;ch=getchar();} 23 while(ch>=0&&ch<=9){x=x*10+ch-0;ch=getchar();} 24 return x*f; 25 } 26 int fst[210],lst[210]; 27 char s[1000010],x,y; 28 int n,q; 29 int main() 30 { 31 while (~scanf("%s",s+1)) 32 { 33 memset(fst,0,sizeof(fst)); 34 memset(lst,0,sizeof(lst)); 35 n=strlen(s+1); 36 for (int i=1;i<=n;i++) 37 { 38 if (!fst[s[i]])fst[s[i]]=i; 39 lst[s[i]]=i; 40 } 41 q=read(); 42 for (int i=1;i<=q;i++) 43 { 44 scanf(" %c %c",&x,&y); 45 if (fst[x]&&fst[y]&&fst[x]<lst[y])puts("YA"); 46 else puts("TIDAK"); 47 } 48 } 49 }
Spoj NPC2015A

Spoj-NPC2015A Eefun Guessing Words