PAT(A) 1077. Kuchiguse (20)

本文介绍了一种通过分析角色对话来识别其口语习惯的方法。利用最长公共后缀算法,可以找出动漫或漫画中角色特有的口语结尾,即“Kuchiguse”。通过实际样例展示了如何从多个句子中提取共同的口语特征。

The Japanese language is notorious for its sentence ending particles. Personal preference of such particles can be considered as a reflection of the speaker's personality. Such a preference is called "Kuchiguse" and is often exaggerated artistically in Anime and Manga. For example, the artificial sentence ending particle "nyan~" is often used as a stereotype for characters with a cat-like personality:

  • Itai nyan~ (It hurts, nyan~)
  • Ninjin wa iyada nyan~ (I hate carrots, nyan~)

Now given a few lines spoken by the same character, can you find her Kuchiguse?

Input Specification:

Each input file contains one test case. For each case, the first line is an integer N (2<=N<=100). Following are N file lines of 0~256 (inclusive) characters in length, each representing a character's spoken line. The spoken lines are case sensitive.

Output Specification:

For each test case, print in one line the kuchiguse of the character, i.e., the longest common suffix of all N lines. If there is no such suffix, write "nai".

Sample Input 1:

3
Itai nyan~
Ninjin wa iyadanyan~
uhhh nyan~

Sample Output 1:

nyan~

Sample Input 2:

3
Itai!
Ninjinnwaiyada T_T
T_T

Sample Output 2:

nai
#include <cstdio>
#include <cstring>
/*求最长公共后缀
(1)取n个字符串中的最短长度minlen
(2)将接收到的n个字符串逆序,转化为求公共前缀
(3)求公共前缀,只需对比字符串前minlen长度的字符(即下标范围:0 -> minlen-1)
*/
int main()
{
    int n;
    char str[105][260];     //至多100个字符串,每个字符串至多256个字符            
    scanf("%d", &n);
    getchar();              //划重点(1):接受掉换行,不然会被下面的gets()收走

    //(1)反转字符串s[i], 转化为求公共前缀
    int minlen=256;         //划重点(2):n个字符串中取最短长度,初始化为最大
    for(int i=0; i<n; i++){
        //注:用scanf输入会错,因为无法判断字符串有效长度
        gets(str[i]);      //gets()会吃掉空格,换行符,故前面须用getchar()/getc()接收

        int len=strlen(str[i]);
        if(len<minlen)
            minlen=len;       //取最小长度
        for(int j=0; j<len/2; j++){      //反转字符串str[i], 转化为求公共前缀
            char temp=str[i][j];
            str[i][j]=str[i][len-1-j];
            str[i][len-1-j]=temp;
        }
    }
    //(2)判断所有字符串的第i个字符是否相等
    int ans=0;    //记录公共前缀有多少个字符相等
    for(int i=0; i<minlen; i++){
        char c=str[0][i];           //取第一个字符串的第i个字符
        bool same=true;
        for(int j=1; j<n; j++){    //判断其余字符串的第i个字符是否等于c
            if(str[j][i]!=c){       //只要有一个不等于c,说明公共前缀到此为止
                same=false;
                break;
            }
        }
        if(same)    ans++;     //若所有字符串的第i位相等时,公共前缀长度ans+1
        else    break;
    }
    //(3)倒序输出公共前缀 => 公共后缀
    if(ans){
        for(int i=ans-1; i>=0; i--)
            printf("%c", str[0][i]);
    }
    else
        printf("nai");          //不存在公共前缀

    return 0;
}
    

 

转载于:https://www.cnblogs.com/claremore/p/6548959.html

内容概要:本文系统介绍了算术优化算法(AOA)的基本原理、核心思想及Python实现方法,并通过图像分割的实际案例展示了其应用价值。AOA是一种基于种群的元启发式算法,其核心思想来源于四则运算,利用乘除运算进行全局勘探,加减运算进行局部开发,通过数学优化器加速函数(MOA)和数学优化概率(MOP)动态控制搜索过程,在全局探索与局部开发之间实现平衡。文章详细解析了算法的初始化、勘探与开发阶段的更新策略,并提供了完整的Python代码实现,结合Rastrigin函数进行测试验证。进一步地,以Flask框架搭建前后端分离系统,将AOA应用于图像分割任务,展示了其在实际工程中的可行性与高效性。最后,通过收敛速度、寻优精度等指标评估算法性能,并提出自适应参数调整、模型优化和并行计算等改进策略。; 适合人群:具备一定Python编程基础和优化算法基础知识的高校学生、科研人员及工程技术人员,尤其适合从事人工智能、图像处理、智能优化等领域的从业者;; 使用场景及目标:①理解元启发式算法的设计思想与实现机制;②掌握AOA在函数优化、图像分割等实际问题中的建模与求解方法;③学习如何将优化算法集成到Web系统中实现工程化应用;④为算法性能评估与改进提供实践参考; 阅读建议:建议读者结合代码逐行调试,深入理解算法流程中MOA与MOP的作用机制,尝试在不同测试函数上运行算法以观察性能差异,并可进一步扩展图像分割模块,引入更复杂的预处理或后处理技术以提升分割效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值