字符串的问题(substr,find用法)
链接:https://www.nowcoder.com/acm/contest/77/C
来源:牛客网
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld
题目描述
串的后缀 并且 在字符串中也出现过一次的(提示 要求满足前后缀的同时也要在字符串中出现一次 只是前后缀可不行 输出最长满足要求字符串)
输入描述:
给出一个字符串 长度 1 到 1e6 全部是小写字母
输出描述:
如果找的到就输出这个子串T 如果不行就输出 Just a legend
输入例子:
fixprefixsuffix
输出例子:
fix
-->
输入
fixprefixsuffix
输出
fix
输入
abcdabc
输出
Just a legend
ubstr(字符串,截取开始位置,截取长度) //返回截取的字
substr('Hello World',0,1) //返回结果为 'H' *从字符串第一个字符开始截取长度为1的字符串
substr('Hello World',1,1) //返回结果为 'H' *0和1都是表示截取的开始位置为第一个字符
substr('Hello World',2,4) //返回结果为 'ello'
substr('Hello World',-3,3)//返回结果为 'rld' *负数(-i)表示截取的开始位置为字符串右端向左数第i个字符
测试:
select substr('Hello World',-3,3) value from dual;
find函数的使用:
#include<iostream>
#include<cstring>
#include <string>
using namespace std;
string s;
string s1;
int main()
{
cin >> s;
cin >> s1;
cout << s.find(s1) << endl;
}
asdfghhjd
fgh
3
asd
sd
1
aaaa
bb
4294967295
本题题解:
#include<iostream>
#include<cstring>
#include <string>
using namespace std;
string s;
int main()
{
cin >> s;
int k = , l = s.size();
string ans = "";
while (l> && k<l)
{
string ss = s.substr(, k);//拷贝s第0个数开始,长度为k
if (s.substr(l - k, k) == ss)
{
string sss = s.substr(, l - );
///cout << sss.find(ss) << endl;
if (sss.find(ss)<l - )
ans = ss;
}
k++;
}
if (ans == "")
cout << "Just a legend" << endl;
else
cout << ans << endl;
}
kmp:
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define N 1000010
char s[N];
int ne[N],a[N],b[N];
void get(int x)
{
ne[]=ne[]=;
int i,j;
for(i=;i<x;i++)
{
j=ne[i];
while(j&&s[i]!=s[j])
j=ne[j];
ne[i+]=(s[i]==s[j]?j+:);
}
}
int main()
{
int i,j,k,tot;
scanf("%s",s);
k=strlen(s);
get(k);
j=k;
tot=;
while(ne[j])
{
if(ne[j])
a[tot++]=ne[j];
j=ne[j];
}
for(i=k;i>=;i--)
{
b[i]++;
b[ne[i]]+=b[i];
}
for(i=;i<tot;i++)
{
if(b[a[i]]>=)
{
for(j=;j<a[i];j++)
printf("%c",s[j]);
printf("\n");
return ;
}
}
printf("Just a legend\n");
return ;
}
我的wa代码
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;
int n,m;
char a[];
char c[];
char b[];
int nxt[];
void get_nxt(int *nxt,char *a,int l)
{
int i = ;
int j = -;
nxt[] = -;
while (i < l)
{
if (j == - || a[i] == a[j]) nxt[++i] = ++j;
else j = nxt[j];
}
}
bool kmp(char *b,char *a,int la,int lb)
{
int i, j;
i = ;
j = ;
while (i<la)
{ if (a[i] == b[j] || j == -) //进行两个字符串的匹配
{
i++;//匹配成功,往后继续匹配
j++;
}
else//匹配完成一次,代表出现了一次,记录下来
{
j = nxt[j];
}
if (j == lb)
{
return ;
}
}
return ;
}
int main()
{
int k = ;
int i;
cin >> a;
int n = strlen(a);
get_nxt(nxt,a,n);
int j=;
bool f = ;
int t = nxt[n];
int kk = nxt[n];
int p = ;
for (i = ; i <= n - ; i++)
c[j++] = a[i];
while (t)
{
if (a[kk - p - ] != a[n - ]) break;
for (j = ; j <= t - ; j++)
b[j] = a[j];
j = ;
m = t;
memset(nxt, , sizeof(nxt));
get_nxt(nxt, b, t);
f = kmp(b, c, n - * t, t);
if (f)
{
for (j = ; j <= t - ; j++)
cout << b[j];
break;
}
t--;
p++;
}
if (!f) cout << "Just a legend" << endl;
else cout << endl;
return ;
}
字符串的问题(substr,find用法)的更多相关文章
- js中substring和substr的用法
js中substring和substr的用法 substring 方法用于提取字符串中介于两个指定下标之间的字符 substring(start,end) 开始和结束的位置,从零开始的索引 参数 ...
- Oracle substr() instr() 用法
转载:oracle中substr() instr() 用法 substr(字符串,截取开始位置,截取长度) = 返回截取的字符串instr(源字符串,目标字符串,起始字符串,匹配字符串) = 返回要截 ...
- substring和substr的用法
substring 方法用于提取字符串中介于两个指定下标之间的字符 substring(start,end) 开始和结束的位置,从零开始的索引 返回值是一个新的字符串,该字符串值包含 stringOb ...
- 【转】awk 里的substr函数用法举例
awk 里的substr函数用法举例: 要截取的内容:2007-08-04 04:45:03.084 - SuccessfulTradeResult(status: 1, currencyPair: ...
- 字符串截取 及 substr 和 substring 的区别
1..字符串截取 str.substr(0, 1) // 获取字符串第一个字符 str.substr(-1) // 获取字符串最后一个字符 str.charAt(str.length - 1) // ...
- php去掉字符串的最后一个字符附substr()的用法
转自:http://www.jb51.net/article/26604.htm 今天项目中用到,去掉字符串中的最后一个字符 原字符串1,2,3,4,5,6, 去掉最后一个字符"," ...
- js中substring和substr的用法比较
推荐使用substring 方法 stringObject.substring(start,stop) stringObject.substr(start,length) 定义和用法 提取 ...
- js中substring和substr的用法 (转)
1.substring 方法 定义和用法 substring 方法用于提取字符串中介于两个指定下标之间的字符. 语法 stringObject.substring(start,stop) 参数 ...
- 截取字符串一之substr
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- JS中substring与substr的用法
substring方法用于提取字符串中介于两个指定下标之间的字符 substring(start,end) 开始和结束的位置,从零开始的索引javascript 参数 描述 start 必需.一个非负 ...
随机推荐
- Markdown_01_基础语法
目录 概览 一.区块元素{#BlockElement} 1.段落和换行 2.标题 2.区块引用 2.1 在每行的最前面加上 > 2.2 只在整个段落的第一行最前面加上> 2.3 区块引用可 ...
- Xcode Server (Xcode9)搭建CI
Xcode 9将Xcode Server集成进来了,这是Xcode一个新特性,不用去单独下载server了,server可以用来做CI.自动化Test等等.这里主要介绍搭建CI,相当简单 打开开关,新 ...
- 将window上的项目上传到自己的github
使用git 1.首先在自己的github上面新建仓库 2.记下远程仓库的地址 3.在要上传的项目的目录下使用git命令进行上传 (1)先git init 初始化本地的仓库 (2)git add -A ...
- sql server 插入用户
'创建登陆用户 use master create login [mashenghao] with password='kline',DEFAULT_DATABASE=[kchnetdb], DEFA ...
- Android Bluetooth 总结
一.Android Bluetooth现状 (1)Android2.2版 支持的蓝牙核心版本是Bluetooth 2.0 + EDR. (2)Android 的蓝牙 使用了BlueZ协议栈,但只实现了 ...
- 使用pjax时点击浏览器刷新按钮仅加载内容页的解决办法
pjax可以实现ajax的局部刷新功能,同时能改变地址栏的URL,因此支持浏览器的后退和前进功能. 但是,在使用中,若没有正确使用,仍然会出现一些问题. 比如,我们在使用pjax后,能够在不加载整个页 ...
- "http://127.0.0.1:4723/wd/hub"的解释
先补充一个内容,就是appium安装时候的环境变量配置,必须要配ANDROID_HOME这个变量,不是“要配置”,是“必须配置”,其他的那些放到系统变量的path里就可以了: # coding: ut ...
- LeetCode 417. Pacific Atlantic Water Flow
原题链接在这里:https://leetcode.com/problems/pacific-atlantic-water-flow/description/ 题目: Given an m x n ma ...
- tidb 集群扩容
1. tidb 新增节点 a. 编辑 inventory.ini [tidb_servers] 10.0.230.14 10.0.230.15 10.10.230.20 b. 初始化新增节点 ansi ...
- web 调试工具docker的安装使用
1. weinre 工具 docker run -d -p 8080:8080 beevelop/weinre 2. vorlonjs(不支持https) docker run --name v ...