sdut 2536 字母哥站队 A. Hometask

http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2536

http://codeforces.com/problemset/problem/154/A

简单dp

代码:

#include<iostream>

#include<cstdio>

#include<cstring>

#include<string>

#include<map>

#include<vector>

#include<stack>

#include<set>

#include<map>

#include<queue>

#include<algorithm>

#include<cmath>

#define LL long long

using namespace std;

const int N=100005;

bool can[27][27];

string s;

int ans[N][27];

int main()

{

    //freopen("data.in","r",stdin);

    while(cin>>s)

    {

        int n=s.size();

        memset(can,true,sizeof(can));

        int m;

        cin>>m;

        while(m--)

        {

            char a,b;

            cin>>a>>b;

            can[a-'a'][b-'a']=false;

            can[b-'a'][a-'a']=false;

        }

        memset(ans,0,sizeof(ans));

        ans[0][s[0]-'a']=1;

        for(int i=0;i<n-1;++i)

        {

            int l=s[i+1]-'a';

            ans[i+1][l]=max(ans[i+1][l],1);

            for(int j=0;j<26;++j)

            {

                ans[i+1][j]=max(ans[i+1][j],ans[i][j]);

                if(can[j][l])

                ans[i+1][l]=max(ans[i+1][l],ans[i][j]+1);

            }

        }



        int tmp=0;

        for(int i=0;i<27;++i)

        tmp=max(tmp,ans[n-1][i]);

        cout<<(n-tmp)<<endl;

    }

    return 0;

}

  

你可能感兴趣的:(meta)