2473 Junk-Mail Filter

Junk-Mail Filter

Time Limit: 15000/8000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6635    Accepted Submission(s): 2119


Problem Description
Recognizing junk mails is a tough task. The method used here consists of two steps:
1) Extract the common characteristics from the incoming email.
2) Use a filter matching the set of common characteristics extracted to determine whether the email is a spam.

We want to extract the set of common characteristics from the N sample junk emails available at the moment, and thus having a handy data-analyzing tool would be helpful. The tool should support the following kinds of operations:

a) “M X Y”, meaning that we think that the characteristics of spam X and Y are the same. Note that the relationship defined here is transitive, so
relationships (other than the one between X and Y) need to be created if they are not present at the moment.

b) “S X”, meaning that we think spam X had been misidentified. Your tool should remove all relationships that spam X has when this command is received; after that, spam X will become an isolated node in the relationship graph.

Initially no relationships exist between any pair of the junk emails, so the number of distinct characteristics at that time is N.
Please help us keep track of any necessary information to solve our problem.
 

Input
There are multiple test cases in the input file.
Each test case starts with two integers, N and M (1 ≤ N ≤ 10 5 , 1 ≤ M ≤ 10 6), the number of email samples and the number of operations. M lines follow, each line is one of the two formats described above.
Two successive test cases are separated by a blank line. A case with N = 0 and M = 0 indicates the end of the input file, and should not be processed by your program.
 

Output
For each test case, please print a single integer, the number of distinct common characteristics, to the console. Follow the format as indicated in the sample below.
 

Sample Input
  
  
5 6 M 0 1 M 1 2 M 1 3 S 1 M 1 2 S 3 3 1 M 1 2 0 0
 

Sample Output
  
  
Case #1: 3 Case #2: 2
 
这道题核心就是虚拟父节点,



#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=100005*2;
int pre[maxn],mark[maxn];
int flag[maxn];
int n,m;
int Find(int x)
{
    int r=x;
    while(r!=pre[r]) r=pre[r];
    int i=x,j;
    while(pre[i]!=r)
    {
        j=pre[i];
        pre[i]=r;
        i=j;
    }
    return r;
}
void join(int x,int y)
{
    int fx=Find(x);
    int fy=Find(y);
    if(fx!=fy) pre[fx]=fy;
}
int main()
{
    int num=0;
    while(scanf("%d%d",&n,&m)!=EOF&&(n||m))
    {
        int k=n;//虚结点初始值
        for(int i=0;i<n;i++)
        {
            pre[i]=i;
            mark[i]=i;
        }
        while(m--)
        {
            char order;
            int x,y,z;
         cin>>order;
          if(order=='M')
          {
                cin>>x>>y;
                join(mark[x],mark[y]);
          }
          else
          {
              cin>>z;
              mark[z]=k;
              pre[k]=k;<span class="comment" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 130, 0); font-family: Consolas, 'Courier New', Courier, mono, serif; line-height: 18px;">//新虚结点给定初始父节点,类似于set[i]=i;</span><span style="margin: 0px; padding: 0px; border: none; font-family: Consolas, 'Courier New', Courier, mono, serif; line-height: 18px;">  </span>
              k++;
          }

         }
         int countn=0;
         memset(flag,0,sizeof(flag));
         for(int i=0;i<n;i++)
            if(flag[Find(mark[i])]==0)//去找父节点,看父节点个数
            {
                flag[Find(mark[i])]=1;
                countn++;
            }
            printf("Case #%d: %d\n",++num,countn);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
android-junk-code,指的是在安卓应用程序中存在的垃圾代码。垃圾代码是指那些对程序功能没有直接影响,或者不被正式使用的代码片段。 垃圾代码的存在有几个原因。首先,开发者在开发过程中可能临时添加了一些测试代码或者调试代码,但是忘记在最后的版本中移除它们。这些代码虽然不会影响程序功能,但是会占用额外的内存和存储空间,增加应用的体积。 其次,由于需求变更或者代码重构,一些原本有用的代码可能变得无效。然而,为了防止引发新的问题,开发者可能选择保留这些无用的代码,而不进行清理。 另外,一些广告库、统计库或者第三方SDK可能会在应用中添加一些无用的代码。这些库的开发者为了实现既定的功能,可能会添加一些额外的代码,而这些代码对于应用来说是无用的。 垃圾代码的存在会对应用的性能和用户体验产生一定的影响。首先,它会增加应用的启动时间和响应时间。其次,垃圾代码会增加应用的内存占用和存储空间占用,从而影响设备的性能和电池寿命。此外,由于垃圾代码的存在,应用的维护和升级也会变得更加困难。 为了解决android-junk-code问题,开发者可以采取以下措施。首先,定期进行代码审查和清理,剔除无用的代码。其次,选择优质的第三方库,避免集成过多无用功能的库。另外,在开发过程中,尽量避免添加无用的测试或调试代码。 总之,android-junk-code是一种常见的问题,需要开发者注意并进行及时清理。清理垃圾代码可以提升应用的性能和用户体验,减少占用的存储空间和设备资源。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值