B. Good String---简单思维题--Educational Codeforces Round 62 (Rated for Div. 2)

    xiaoxiao2022-07-07  216

    Good String

    time limit per test 1 second memory limit per test 256 megabytes

    题目链接http://codeforces.com/problemset/problem/1140/B


    emmm,题目大意:给你一个尖括号序列,你可以进行操作,你可以选择其中一个,假如选择’>‘那么你可以删去它右边的一个字符,’<'则是左边的。假设一个good string 是一个单一形态的串。在进行选择之前你可以删去一些字符。问你最少删去多少个字符可使得给定的序列在操作之后为good string。

    首先我们考虑什么情况下不要删字符: 1.当第一个字符为‘>’的时候,我们可以将后面的序列全部删除所以不需要删除 2.当最后一个字符为‘<’的时候同1理

    那么我们所需要的就是将序列变成首字符为‘>’ 或尾字符为‘<’那么就只需要将第一个‘>’之前的全部删掉,或最后一个‘<’之后的全删的就行了。

    以下是AC代码:

    #include <bits/stdc++.h> using namespace std; char s[150]; int main() { int t,n; scanf ("%d",&t); while (t--){ scanf ("%d",&n); scanf ("%s",s+1); if (n==1) printf ("0\n"); else if (s[1]=='>' || s[n]=='<') printf ("0\n"); else { int head=1,num=0; while (s[head]=='<') head++; head--; int tail=n; while (s[tail]=='>') tail--,num++; if (head==n ||num==n) printf ("0\n"); else printf ("%d\n",min(head,num)); } } return 0; }
    最新回复(0)