首页 > 其他分享 >C - Rotate and Palindrome

C - Rotate and Palindrome

时间:2023-01-22 13:44:13浏览次数:35  
标签:Rotate cost ++ long Palindrome rotate

C - Rotate and Palindrome

https://atcoder.jp/contests/abc286/tasks/abc286_c

 

思路

从原始字符串开始, rotate第一次, rotate第二次, ... , rotate最后一次

对于每种情况得到的字符串:

判断如果转换成回文,需要替换多少个字符串,

计算每种情况的cost

选取最少的cost

 

Code

long long n, a, b;
string s;
 
int main()
{
    cin >> n >> a >> b;
 
    cin >> s;
    
    // Now try all rotations one by one
    long long cost_min = LLONG_MAX;
    
    int n = s.length();
    for (long long i = -1; i < n - 1; i++) {
//        cout << "i=" << i << endl;
        
        long long costi = 0;
        costi += (i+1) * a;
        
        string str1 = s.substr(i + 1, n - i - 1);
        string str2 = s.substr(0, i + 1);
 
        string rstr = str1.append(str2);
        
//        cout << "rstr=" << rstr << endl;
 
        int diff_count = 0;
        
        // Start from leftmost and rightmost corners of str
        int l = 0;
        int h = rstr.length() - 1;
 
        // Keep comparing characters while they are same
        while (h > l){
            if (rstr[l++] != rstr[h--])
                diff_count ++;
        }
        
//        cout << "diff_count=" << diff_count << endl;
        
        costi += (long long)diff_count * b;
        
        cost_min = min(cost_min, costi);
    }
 
    cout << cost_min << endl;
 
    return 0;
}
 

 

标签:Rotate,cost,++,long,Palindrome,rotate
From: https://www.cnblogs.com/lightsong/p/17064405.html

相关文章

  • 308. 最廉价的回文串 Cheapest Palindrome(挑战程序设计竞赛)
    地址https://www.papamelon.com/problem/308给定一个长度为m(m≤2000)的小写字母字符串,在给定组成该字符串的n(n≤26)个字符的添加和删除费用,求使原字符串变为......
  • 如何理解 logrotate 实用工具?请收下这份保姆级教程
    当你想对一个应用程序跟踪使用状况或者进行故障排除的时候,日志是十分有用的。然而,随着越来越多的信息被记录,日志文件占据的硬盘空间也会越来越大。久而久之,一个日志文件能变......
  • [LeetCode] 1328. Break a Palindrome 破坏回文串
    GivenapalindromicstringoflowercaseEnglishletterspalindrome,replaceexactlyonecharacterwithanylowercaseEnglishlettersothattheresultingstri......
  • Codeforces 1335E2 Three Blocks Palindrome (hard version)
    链接难度:\(\texttt{1800}\)\(T\)组数据。一个序列\(a_{1\simn}\)。定义\([\underbrace{a,a,\dots,a}_{x},\underbrace{b,b,\dots,b}_{y},\underbrace{a,......
  • 125. Valid Palindrome [Easy]
    125.ValidPalindromeAphraseisapalindromeif,afterconvertingalluppercaselettersintolowercaselettersandremovingallnon-alphanumericcharacters,......
  • P1217 [USACO1.5]回文质数 Prime Palindromes
    题目题目描述因为151既是一个质数又是一个回文数(从左到右和从右到左是看一样的),所以151是回文质数。写一个程序来找出范围[a,b](5<=a<b<=100,000,000)(一亿)间的......
  • B2. Palindrome Game (hard version)-cf1527
    B2.PalindromeGame(hardversion)https://codeforces.com/problemset/problem/1527/B2题意给定一个长度为n的二进制串两人博弈每次可以做下面两个操作的一个:1.将......
  • POJ 1159 Palindrome
    POJ1159Palindrome题意:给出一个字符串,求最少插入多少个字符可以让该字符串变成回文字符串思路1:思路1是卡过去的(用\(short\)换\(int\)和用了一个常数优化),所......
  • 日志切割 logrotate
    TOCSre@iddcipl01043:~$cat/etc/.d/nginx/var/log/nginx/*.log{dailymissingokrotate14compressdelaycompres......
  • 日志切割: logrotate、python、shell实现
    对于Linux系统安全来说,日志文件是极其重要的工具。不知为何,我发现很多运维同学的服务器上都运行着一些诸如每天切分Nginx日志之类的CRON脚本,大家似乎遗忘了Logrotate,争相发......