码迷,mamicode.com
首页 > 其他好文 > 详细

GYM 101061 I. Playing with strings(有待更新)

时间:2019-08-19 23:16:18      阅读:79      评论:0      收藏:0      [点我收藏+]

标签:not   color   别人   single   多个   red   contains   lines   with   

I. Playing with strings

time limit per test

2.0 s

memory limit per test

64 MB

input

standard input

output

standard output

Taboush is a 10 year-old school boy. On his birthday, his parents got him a new Alphabet blocks game.

Alphabet blocks game consists of cubes. Each cube has a letter written on it, and each letter is written on an infinite number of cubes.

Taboush wasted no time and he immediately started forming his magical string S1. He spent all day forming the string. At the end, he was so tired, so he felt asleep.

While he was asleep, his naughty cat Basbouse - as he called her - found the magical string. She started to add, delete and shuffle letters in S1, and she ended up forming a new string S2.

When Taboush finally woke up, he was upset that Basbouse had ruined his magical string. Now, he wants to form his magical string all over again.

In one move, Taboush can add a letter to S2 or delete a letter from S2. At the end, Taboush can shuffle the letters to any order he wants.

Can you help Taboush by telling him what is the minimum number of moves he needs to do, in order to convert S2 into S1?

Please note that shuffling the letters is not counted as a move.

Input

The first line of the input consists of a single integer t denoting the number of test cases.

Each test case consists of 2 lines. The first line contains S1, and the second line contains S2 (1?≤?|S1|,?|S2|?≤?105) consisting of lower case English letters only.

Output

For each test case print a single line containing the minimum number of moves Taboush needs in order to convert S2 into S1.

Example

input

Copy

3
abc
abd
abcde
bdcea
taboush
basbouse

output

Copy

2
0
5

Note

|S| means the length of the string S.

In the first test case, Taboush has to delete the letter d, then add the letter c. Thus, he needs 2 moves.

In the second test case, Taboush has to shuffle the letters only. Thus, he doesn‘t need any number of moves.

 

这题我wa了二三次,tle了四五次;最开始在想用STL中的set写,但是set容器不能存放多个相同的元素,后来想用STL中的string做,就是把每一组测试数据的两个字符串分别存放在STL的string中;

然后遍历第一个字符串,在第二个字符串中用find()查找有没有第一个字符串中的元素,有的话就sum++;这样的话如果第二个字符串有多个和第一个字符串中的某个字母相同的元素时sum会持续加一,所以应该在查找到有相同元素时就erase掉二个字符串中相同的元素,继续查找;但是我还没按这种思路写出来,不过估计会tle;

然后上网搜了别人的博客,发现是另一种思路:

就是先开一个char[]字符数组,(前几次我开了char[10000],结果超时了,不知道为啥开char[100000]就不超时。。。)然后再定义两个整数数组a[30],b[30],分别存放输入的两组字符数组每个字母的数量;

然后i从1到26遍历,取a[i]-b[i]的绝对值累加,就可以得到结果;

但是开始我按照这个思路做的时候还tle了一次,就是在遍历两个字符数组分别计算a~z每个字母的个数时,我用for(int i=0;i<strlen(chr);i++)   a[chr[i]-‘a’]++;超时了,但换成

for(int i=0;chr[i];i++)就过了,感觉好奇怪。。

下面是最后的ac代码:

#include <cstdio>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring>
using namespace std;
int main(){
  int t;
  int i;
  char chr[100000];
  int a[33],b[33];
  scanf("%d",&t);
  while(t--){
      scanf("%s",chr);
      memset(a,0,sizeof(a));
      for(i=0;chr[i];i++) a[chr[i]-a]++;
      scanf("%s",chr);
      memset(b,0,sizeof(b));
      for(i=0;chr[i];i++) b[chr[i]-a]++;
      int sum=0;
      for(i=0;i<26;i++) sum+=abs(a[i]-b[i]);
      printf("%d\n",sum);
  }    
  return 0;
}

GYM 101061 I. Playing with strings(有待更新)

标签:not   color   别人   single   多个   red   contains   lines   with   

原文地址:https://www.cnblogs.com/jianqiao123/p/11380074.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有 京ICP备13008772号-2
迷上了代码!