显示代码纯文本
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <list>
#include <vector>
#include <ctime>
#include <functional>
#define pritnf printf
#define scafn scanf
#define For(i,j,k) for(int i=(j);i<=(k);(i)++)
using namespace std;
typedef long long LL;
typedef unsigned int Uint;
const int INF=0x7fffffff;
//==============struct declaration==============
//==============var declaration=================
const int MAXN=100010;
char color[MAXN];
int c,n;
int f[MAXN][27];
//==============function declaration============
//==============main code=======================
int main()
{
string FileName="shimi";//程序名
string FloderName="COGS";//文件夹名
freopen((FileName+".in").c_str(),"r",stdin);
freopen((FileName+".out").c_str(),"w",stdout);
#ifdef DEBUG
system(("cp C:\\Users\\Administrator\\Desktop\\"+FloderName+"\\standard.cpp C:\\Users\\Administrator\\Desktop\\"+FloderName+"\\submit.txt").c_str());
clock_t Start_Time=clock();
#endif
scanf("%d%d",&n,&c);c--;n--;
scanf(" %s",color);
For(i,0,n)
color[i]-='A';
For(i,0,c)
if (i==color[0])
f[0][i]=0;
else
f[0][i]=1;
For(i,1,n)
{
For(nc,0,c)
{
f[i][nc]=INF;
For(bc,0,c)
{
if (nc==bc)
continue;
if (nc==color[i])
f[i][nc]=min(f[i-1][bc],f[i][nc]);
else
f[i][nc]=min(f[i-1][bc]+1,f[i][nc]);
}
}
}
int minn=INF;
For(i,0,c)
if (f[n][i]<minn)
minn=f[n][i];
pritnf("%d\n",minn);
#ifdef DEBUG
clock_t End_Time=clock();
cout<<endl<<endl<<"Time Used: "<<double(End_Time-Start_Time)/CLOCKS_PER_SEC<<endl;
#endif
return 0;
}
//================fuction code====================