原來我題目看錯了!XD 頭昏!
VERDI vivaVittorioEmanueleReDiItalia YES
我前面兩個寫的都是比對要完全相同才行 t裡的字串要完全相同才判斷正確,
其實不用只要 當你移走t字串中的某些字元後,剩下的字串就是s。就可以了!
也就是 s在t中不需要完全相連!
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
int main()
{
char s[1000000],t[1000000];
int i,j,counter;
bool flag = false;
int temp;
int length;
while(scanf("%s %s",s,t)!=EOF)
{
int t_length = strlen(t);
int s_length = strlen(s);
j=0;
for(i=0;i<t_length;i++)
{
if(t[i]==s[j]&& j<s_length)
{
j++;
if(j==s_length)
{
flag = true;
i=t_length;
break;
}
}
}
if(flag)
{
flag = false;
printf("Yes\n");
}
else
printf("No\n");
}
system("pause");
//return 0;
}
[心得] WA 不知道邏輯哪裡錯了!有空再看!
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
int main()
{
char s[1000000],t[1000000];
int i,j,counter;
bool flag = false;
int temp;
int length;
while(scanf("%s %s",s,t)!=EOF)
{
counter=0;
int t_length = strlen(t);
int s_length = strlen(s);
for(i=0;i<t_length;i++)
{ j=0;
temp=i;
counter =0;
while(t[temp]==s[j]&& j<s_length)
{
temp++;
j++;
counter++;
if(counter==s_length)
{
flag = true;
i=t_length;
break;
}
}
}
if(flag)
{
flag = false;
printf("Yes\n");
}
else
printf("No\n");
}
system("pause");
//return 0;
}
[心得] WA
不懂為什麼用 strstr判斷 會WA,只好重寫一支,不知道是否於字串超長有關係!
剛剛GOOGL了一下, 使用 strstr的確有長度限制!
#include <stdio.h>
#include <string.h>
int main()
{
char s[100000],t[100000];
while(scanf("%s %s",s,t)!=EOF)
{
int length,comp;
char *pos;
pos = strstr(t,s);
if(pos!=NULL)
printf("YES\n");
else
printf("NO\n");
}
system("pause");
return 0;
}
沒有留言:
張貼留言