s = input() # 先分割字符串 lst_first = s.split('[') lst_final = [] for item in lst_first: lst_final.extend(item.split(']')) # 然后倒序遍历,获得[img]和[\img]封闭的范围后返回 res = [] flag = False for i in range(len(lst_final) - 1, -1, -1): if lst_final[i] == "\img": res.append("[\img]") flag = True if flag and lst_final[i] == "img": res.append("[img]") flag = False break elif flag and (lst_final[i] not in ["img", "\img"]): res.append(lst_final[i]) if not flag: # 如果有[img]则结果正常,进行打印 print("".join(res[::-1])) else: # 否则并没有匹配到符合题意的字符串 print("null")
# 正则表达式,python环视需要限定匹配的字符个数,不能很好的解决这个问题 # 但这个字符串整体倒过来,都是在遇到第一个字符的时候匹配,然后在倒回来就可以了 # 注意字符串倒置,匹配的时候匹配的是]gmi\[而不是原字符 ss = input()[::-1] r = ss.find(']gmi\[') l = ss.find(']gmi[') if r == -1 or l == -1 or r > l: print("null") else: print(ss[r:l+5][::-1])# 字符串的来回倒置,容易弄混,另外注意加上查找串的长度
#include <bits/stdc++.h> using namespace std; int main() { string s,res=""; cin>>s; int m=-1,n=s.size(); for(int i=s.size()-1;i>=0;i--) { if(s[i]==']') { string st=""; for(int j=i-5;j<=i;j++) st+=s[j]; if(st=="[\\img]") { n=i; break; } } } for(int i=n-1;i>=0;i--) { if(s[i]==']') { string st=""; for(int j=i-4;j<=i;j++) st+=s[j]; if(st=="[img]") { m=i-4; break; } } } if(m==-1||n==s.size()) cout<<"null"<<endl; else { for(int i=m;i<=n;i++) cout<<s[i]; cout<<endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { string str; while(getline(cin,str)) { int len = str.length(); int i = str.rfind("[img]",len-1); //最后一个[img]的'['所在下标 int j = str.rfind("[\\img]",len-1) + 5; //最后一个[\img]的']'所在下标 if(i < j) // 可能出现\img前面没有img的情况 { cout << str.substr(i,j-i+1) << endl; //从第一个[img]开始截取(j-i+1)个长度的子字符串 } else //未找到匹配的字符串则返回"null" { cout << "null" << endl; } } return 0; }
import java.util.*; import java.lang.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String in = sc.nextLine(); int e = in.lastIndexOf("[\\img]"); int b = in.lastIndexOf("[img]"); String result; if (b > 0 && e > 0 && b < e) { result = in.substring(b, e) + "[\\img]"; } else { result = "null"; } System.out.println(result); } }
import java.util.*; public class Main { // 抖个机灵 public static void main(String[] args) { Scanner in = new Scanner(System.in); String s = in.nextLine(); String[] ss = s.split("\\[img]"); String res = ss[ss.length-1]; int idx = res.lastIndexOf("[\\img]"); if (idx != -1) System.out.println("[img]" + res.substring(0, idx+6)); else System.out.println("null"); } }
#include <stdio.h> #include <string.h> char* substr(char *str, int left, int right) { char *temp = (char *) malloc((right - left) * sizeof(char)); for (int i = left; i < right; ++i) { temp[i - left] = str[i]; } temp[right - left] = '\0'; return temp; } int match(char *str, int pos) { char *s1 = "[img]"; char *s2 = "[\\img]"; if (strlen(str) - pos >= 5 && strncmp(str + pos, s1, strlen(s1)) == 0) { return 1; } else if (strlen(str) - pos >= 6 && strncmp(str + pos, s2, strlen(s2)) == 0) { return -1; } else { return 0; } } int main() { char str[100]; scanf("%s", str); int begin, end; begin = end = -1; int left, right; left = right = 0; for (int i = strlen(str) - 5; i >= 0; --i) { if (!left && match(str, i) == 1) { begin = i; left = 1; } else if (!right && match(str, i) == -1) { end = i; right = 1; } } if (begin == -1 || end == -1 || begin >= end) { printf("null"); } else { char *ptr = substr(str, begin, end + 6); printf("%s", ptr); free(ptr); } return 0; }
import java.util.Scanner; public class Main{ public static void main(String args[]){ String s = new Scanner(System.in).nextLine(); String sReverse = new StringBuilder(s).reverse().toString(); int begin = -1, end = -1; begin = sReverse.indexOf("]gmi\\["); end = sReverse.indexOf("]gmi["); if (begin < end) { String s1 = sReverse.substring(begin, end +5); System.out.println(new StringBuilder(s1).reverse().toString()); }else System.out.println("null"); } }
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; int l = 0, r = 0; char* c = new char[s.size()]; for (int i = 0; i < s.size(); i++)c[i] = s[i]; for (int i = 0; i < s.size() - 4; i++)if (c[i] == '['&&c[i + 1] == 'i'&&c[i + 2] == 'm'&&c[i + 3] == 'g'&&c[i + 4] == ']')l = i; for (int i = 0; i < s.size() - 5; i++)if (c[i] == '['&&c[i + 1] == '\\'&&c[i + 2] == 'i'&&c[i + 3] == 'm'&&c[i + 4] == 'g'&&c[i + 5] == ']')r = i; if (l < r&&l != 0 && r != 0)for (int i = l; i < r + 6; i++)cout << s[i]; else cout << "null"; }
string input = string.Empty; while (!string.IsNullOrEmpty(input = Console.ReadLine())) { int imgIndex = input.LastIndexOf("[img]"); int imgIndex2 = input.LastIndexOf(@"[\img]"); if (imgIndex == -1 || imgIndex2 == -1) { Console.WriteLine("null"); } else { int end = imgIndex2 + @"[\img]".Length - imgIndex; if (imgIndex > end) { Console.WriteLine("null"); } else { string output = input.Substring(imgIndex, imgIndex2 + 6 - imgIndex); Console.WriteLine(output); } } }
def solve(s): new = s[::-1] i = 0 stack = [] while i<len(s) and not stack: if new[i]==']' and i+6<len(s) and new[i:i+6]==']gmi\[': i += 6 while i<len(s): if new[i]==']' and i+5<len(s) and new[i:i+5]==']gmi[': break stack.append(new[i]) i += 1 i += 1 if i<len(s): return '[img]'+''.join(stack)[::-1]+'[\img]' return 'null' if __name__=='__main__': s = raw_input().strip() print solve(s)
#include<iostream> #include<string> #include<cstring> using namespace std; int main() { string str; cin>>str; int len=str.length(); int flag_start=0,flag_end=0,start_ad,end_ad; for(int i=0;i<len-5;i++) { if(str.substr(i,5)=="[img]") { flag_start=1; start_ad=i; } } for(int i=0;i<len-6;i++) { if(str.substr(i,6)=="[\\img]") { flag_end=1; end_ad=i; } } if((flag_start==0||flag_end==0)||end_ad<start_ad) { cout<<"null"<<endl; return 0; } else { cout<<str.substr(start_ad,end_ad-start_ad+6)<<endl; } return 0; }关键在于对结尾判断时,反斜杠要输入两次
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; /** * @author wylu */ public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String str = br.readLine(); int end = str.lastIndexOf("[\\img]"); int start = str.lastIndexOf("[img]"); if (start == -1 || end == -1 || start > end) System.out.println("null"); else System.out.println(str.substring(start, end + 6)); } }