---
# System prepended metadata

title: 106-10 APCS

---

# 106-10 APCS

## [黃建庭老師ㄉ解答(點窩)](https://sites.google.com/site/zsgititit/zi-xun-neng-li-jian-ding/apcs)
---
### #1 交錯字串 (Alternating Strings)
```clike=
#include <bits/stdc++.h>
using namespace std;
int main()
{
    int a,b,n;
    while(cin>>a>>b>>n)
    {
        int ans=0;
        if(a==0&&b==0)
        {
            if(n==0)
            {
                cout<<"AND"<<endl;
                cout<<"OR"<<endl;
                cout<<"XOR"<<endl;
                ans=1;
            }
        }
        if(a==0&&b!=0)
        {
            if(n==0)
            {
                cout<<"AND"<<endl;
                ans=1;
            }
            else
            {
                cout<<"OR"<<endl;
                cout<<"XOR"<<endl;
                ans=1;
            }
        }
        if(a!=0&&b==0)
        {
            if(n==0)
            {
                cout<<"AND"<<endl;
                ans=1;
            }
            else
            {
                cout<<"OR"<<endl;
                cout<<"XOR"<<endl;
                ans=1;
            }
        }
        if(a!=0&&b!=0)
        {
            if(n==0)
            {
                cout<<"XOR"<<endl;
                ans=1;
            }
            else
            {
                cout<<"AND"<<endl;
                cout<<"OR"<<endl;
                ans=1;
            }
        }
        if(ans==0) cout<<"IMPOSSIBLE"<<endl;
    }
}
```

### #2 交錯字串 (Alternating Strings
```clike=
#include <bits/stdc++.h>
using namespace std;
int main()
{
    int k;
    while(cin>>k)
    {
        string s;
        cin>>s;
        int ans=0;
        for(int i=0; i<s.length(); i++)
        {
            int c=1;
            int n=2; //記錄每k位交換
            int cun=0;
            int y=0; 
            if(s[i]<95)
            {
                for(int j=i; j<s.length(); j++,c++)
                {
                    if(n%2==0) //奇偶判斷要大寫還小寫
                    {
                        if(s[j]<95)
                        {
                            cun++;
                            y++;
                        }
                        else
                            break;
                    }
                    if(n%2==1)
                    {
                        if(s[j]>95)
                        {
                            cun++;
                            y++;
                        }
                        else
                            break;
                    }
                    if(c>=k)  //每k位交換大小寫
                    {
                        c=0;
                        n++;
                        y=0;
                    }
                }
            }
            if(s[i]>95)
            {
                for(int j=i; j<s.length(); j++,c++)
                {
                    if(n%2==0)
                    {
                        if(s[j]>95)
                        {
                            cun++;
                            y++;
                        }
                        else
                            break;
                    }
                    if(n%2==1)
                    {
                        if(s[j]<95)
                        {
                            cun++;
                            y++;
                        }
                        else
                            break;
                    }
                    if(c>=k)
                    {
                        c=0;
                        n++;
                        y=0;
                    }
                }
            }
            if(y<k)         //未滿k次就減掉
                cun-=y;
            if(cun>ans)
            {
                ans=cun;
            }
        }
        cout<<ans<<endl;
    }
}

```

### #3
```clike=
#include <bits/stdc++.h>
using namespace std;
#define MAX 100001
int main()
{
    int n,a,k;
    while(cin>>n)
    {
        int d[MAX]={0};
        int p[MAX]={0};
        int num[MAX]={0};
        deque<int> v;
        for(int i=1;i<=n;i++)
        {
            cin>>k;
            if(k==0)
            {
                d[i]=0;
                v.push_back(i);
            }
            else
            {
                num[i]=k;
                for(int j=1;j<=k;j++)
                {
                    cin>>a;
                    p[a]=i;
                }
            }
        }
        int x;
        while(!v.empty())
        {
            x=v.front();
            v.pop_front();
            d[p[x]]=max(d[x]+1,d[p[x]]);
            num[p[x]]--;
            if(num[p[x]]==0)
            {
                v.push_back(p[x]);
            }
        }
        long long int sum=0;
        for(int i=1;i<=n;i++)
        {
            sum+=d[i];
        }
        cout<<x<<endl;
        cout<<sum<<endl;
    }
}

```