题目大意:给定n个字符串,让你找到他们的最长公共字符串后缀是什么,可能为空。

分析:题目数据范围比较小,可以O(n*n)暴力匹配,即可解决这道问题。之所以写这道题的题解还是因为写字符串的题还不够多啊,菜的一批。

代码:

#include<bits/stdc++.h>
using namespace std;
string common(string s,string t) {
int most = ;
int len = min(s.length(), t.length());
for (int i = ; i < len; i++) {
if (s[i] == t[i])
most++;
else
break;
}
return s.substr(, most);
}
int main() {
int n;
while (cin >> n) {
if (!n) break;
string s;
cin >> s;
reverse(s.begin(), s.end());
n--;
for (int i = ; i < n; i++) {
string t;
cin >> t;
reverse(t.begin(), t.end());
s = common(s, t);
}
reverse(s.begin(), s.end());
cout << s << endl;
}
return ;
}

Acwing779 最长公共字符串后缀的更多相关文章

  1. [AcWing 779] 最长公共字符串后缀

    点击查看代码 #include<iostream> using namespace std; const int N = 200; string str[N]; int n ; int m ...

  2. poj 2774 最长公共子串 后缀数组

    Long Long Message Time Limit: 4000MS   Memory Limit: 131072K Total Submissions: 25752   Accepted: 10 ...

  3. hdoj 2594 Simpsons’ Hidden Talents 【KMP】【求串的最长公共前缀后缀】

    Simpsons' Hidden Talents Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java ...

  4. 算法 -- 求最长公共字符串&PHP

    https://blog.csdn.net/hongyuancao/article/details/83308093 本文是利用PHP,求最长公共字符串.思路:利用动态规划和矩阵的思想. 动态规划:就 ...

  5. 求字符串A与字符串B的最长公共字符串(JAVA)

    思路:引入一个矩阵的思想,把字符串A(长度为m)当成矩阵的行,把字符串B(长度为n)当矩阵的列.这样就构成一个m*n的矩阵.若该矩阵的节点相应的字符同样,即m[i]=n[j]时.该节点值为1:当前字符 ...

  6. Codeforces Round #545 (Div. 2)D(KMP,最长公共前后缀,贪心)

    #include<bits/stdc++.h>using namespace std;const int N=1000007;char s1[N],s2[N];int len1,len2; ...

  7. Java算法——求出两个字符串的最长公共字符串

    问题:有两个字符串str1和str2,求出两个字符串中最长公共字符串. 例如:“acbbsdef”和"abbsced"的最长公共字符串是“bbs” 算法思路: 1.把两个字符串分别 ...

  8. POJ 3080 Blue Jeans (求最长公共字符串)

    POJ 3080 Blue Jeans (求最长公共字符串) Description The Genographic Project is a research partnership between ...

  9. HDU 2594(求最长公共前后缀 kmp)

    题意是在所给的两个字符串中找最长的公共前后缀,即第一个字符串前缀和第二个字符串后缀的最长相等串. 思路是将两个字符串拼接在一起,然后直接套用 kmp 算法即可. 要注意用 next 会报编译错误,改成 ...

随机推荐

  1. jQuery中$("input")与$(":input")的区别

    $("input")表示获取页面所有的input元素 $(":input")选取表单中所有的input,select 和 button元素

  2. javac 编译引用外部指定jar包进行编译和执行编译后的class文件

    1.libs新建文件夹存放依赖所有jar包 2.cmd 执行: 2.1 javac -encoding UTF-8 -classpath .;C:\Users\chenquan\IdeaProject ...

  3. SQLite3约束介绍

    SQLite 约束 约束是在表的数据列上强制执行的规则.这些是用来限制可以插入到表中的数据类型.这确保了数据库中数据的准确性和可靠性. 约束可以是列级或表级.列级约束仅适用于列,表级约束被应用到整个表 ...

  4. Plastic Bottle Manufacturer Tips - Attention To Plastic Bottle Processing Technology

    In fact, the processing technology of plastic bottles is actually quite strict. In fact, regular man ...

  5. Python:列表类型

    概念 列表:有序的,可变的,元素集合 因为列表和字符串都是序列类型,所以很多操作和字符串很相似 但是注意:列表是可变类型,字符串是不可变类型 定义 基本定义 定义方法:[ 元素1, 元素2, .... ...

  6. linux开机启动项问题。6版本与7版本不同之处 。

    参考 网址:https://blog.csdn.net/weixin_41909810/article/details/82775247

  7. ZOJ1008 Gnome Tetravex

    DFS+剪枝~ #include<bits/stdc++.h> using namespace std; ][]; int N; int cnt; ]; ]; unordered_map& ...

  8. Post请求的两种编码格式:application/x-www-form-urlencoded和multipart/form-data

    在常见业务开发中,POST请求常常在这些地方使用:前端表单提交时.调用接口代码时和使用Postman测试接口时.我们下面来一一了解: 一.前端表单提交时 application/x-www-form- ...

  9. Educational Codeforces Round 81 (Rated for Div. 2)

    A 0~9需要多少笔画,自取7和1,判奇偶 #include<bits/stdc++.h> using namespace std; #define ll long long #defin ...

  10. #P4770 [NOI2018]你的名字 的题解

    题目背景 实力强大的小A 被选为了ION2018 的出题人,现在他需要解决题目的命名问题. 题目描述 小A 被选为了ION2018 的出题人,他精心准备了一道质量十分高的题目,且已经把除了题目命名以外 ...