最近写什么 WA什么烦死了,于是练练码力,水一水
顺便换个心情换个头像什么的 - -
这道题难点就是找准什么时候用 "automaton" 什么时候 用 "array"(这不是废话吗)
#include <cstdio>
#include <cstring>
using namespace std;
char s[200],t[200];
int main()
{
scanf("%s%s",s,t);
int ls=strlen(s),lt=strlen(t);
int hashs[26]={0},hasht[26]={0};///记录字母用数组
if(lt>ls) ///t>s肯定是变不了的
{
puts("need tree");
return 0;
}
int i=0,j=0,au=0,ar=0,f=0;
while(i<ls)
{
if(s[i]==t[j])
{
f++;
hasht[t[j++]-'a']++;
}
if(f==lt) ///不是中间断开肯定不要用array
ar=-1;
hashs[s[i++]-'a']++;
}
if(j!=lt)
au=0;
else
au=1;
//printf("j:%d\n",j);
while(j<ls)
hasht[t[j++]-'a']++;
for(i=0;i<26;i++)
{
if(hashs[i]<hasht[i])
break;
}
if(ar+1) ///如果ar不为-1时才判断是否用array
if(i!=26) ar=0;
else ar=1;
else ar=0;
if(ar&&ls!=lt)
au=1;
if(au&&ar)
puts("both");
else if(au)
puts("automaton");
else if(ar)
puts("array");
else
puts("need tree");
return 0;
}

本文探讨了在字符串匹配问题中如何灵活运用自动机和数组技术,通过实例展示了如何准确判断一个字符串是否能通过一系列操作转变为另一个更长的字符串。文章深入分析了自动机和数组在解决此类问题时的各自优势和适用场景,同时提供了一个具体代码实现,帮助读者更好地理解并实践这一过程。

244

被折叠的 条评论
为什么被折叠?



