原题链接https://codeforces.com/problemset/problem/448/B题目大意:写入两个字符s和t,判断能否通过删除一个元素、交换元素的位置,使s变成t。若仅需要删除则输出automaton,若仅需要交换则输出array,若都需要则输出both,若无法实现则输出need tree。
解题思路:
(1)首先特判s.size()是否小于t.size(),小于这输出need tree。
(2)用双指针判断能否仅通过删除实现,可以则输出automaton。
(3)a[]统计s中每个字母的数量,再遍历t,从a[]中减去每个字母的数量。判断a[]中是否有小于0的元素,如果有则输出need tree;如果有大于0的元素,则输出both;否则输出array。
AC代码(C++)
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define MP make_pair
#define SZ(X) ((int)(X).size())
typedef pair<int, int> PII;
const int INF = 0x3f3f3f3f;
const int N = 1e6 + 10;
int main(){
string s,t;
cin>>s>>t;
int i ,j ;
i = j = 0;
if(s.size() < t.size()){cout<<"need tree";return 0;}
while(i != s.size() && j != t.size()){
if(s[i] == t[j]){i++;j++;}
else i++;
}
if(j == t.size() ){cout<<"automaton";return 0;}
int a[26] = {0};
for (int i = 0;i < s.size();i ++){a[s[i] - 'a' ] ++;}
for (int i = 0;i < t.size();i ++){a[t[i] - 'a' ] --;}
bool f = 1 ;
for(int i = 0 ;i < 26; i ++){
if(a[i] < 0){printf("need tree");return 0;}
else if(a[i] > 0) f = 0;
}
if(f){printf("array");return 0;}
else printf("both");
return 0;
}