JZOI-P2882 字符串替换


小明最近迷上了字符串操作。对每个字符串,小明每次可以执行以下两种操作之一:
1. 把字符串中的某个字符改成任意一个其他字符,花费1的代价。

2. 交换字符串中的两个字符,花费0的代价。

小明发现,把一个字符串通过一系列的操作,可以转换成任何一个与之等长的字符串。例如,把 “hello”变为“world”的一种代价为 3 的操作序列如下:

1. hello → wello (替换 h 为 w,代价为 1)

2. wello → wolle (交换 e 和 o,代价为 0)

3. wolle → worle (替换 l 为 r,代价为 1)

4. worle → world (替换 e 为 d,代价为 1)

小明发现,无法用少于 3 次的代价将“hello”变为“world”。 显然,不同的转换方案花费的代价是不同的,请编程帮助小明计算把一个字符串变为另一个字符 串的最小代价。

本题中的字符串根据给定的初始数值 s 按以下规则生成:

for i = 1, 2, ... n

  s ← (s × 345) mod 19997

  第一个字符串的第 i 个字符的 ASCII 码为(97 + (s mod 26))
for i = 1, 2, ... n

  s ← (s × 345) mod 19997

  第二个字符串的第 i 个字符的 ASCII 码为(97 + (s mod 26))

#define f(i,a,b) for(int i=a;i<=b;++i)
#include 
inline int read(){
    int s=0,flag=1;
    register char ch=getchar();
    while(!isdigit(ch)){
        if(ch=='-') flag=-1;
        ch=getchar();
    }
    while(isdigit(ch)){
        s=s*10+ch-'0';
        ch=getchar();
    }
    return s*flag;
}//快读
using namespace std;
int n=read(),m=read();
int t[80],p[80],ans;
int main()
{
    for(int i=1;i<=n;++i){
        m=(m*345)%19997;         //按题意生成字符串
        t[m%26]++;    //生成一个桶
    }
    for(int i=1;i<=n;++i){
        m=(m*345)%19997;     //按题意生成字符串
        p[m%26]++;         //生成另一个桶
    }
    for(int i=0;i<=25;++i)
        ans+=abs(t[i]-p[i]);         //统计两个字符串差距
    cout<



 
//江苏省选拔题