心血来潮整理一下STL——string

       呐,今天做了一道单词接龙的题(http://www.luogu.org/problem/show?pid=1019),一开始用传统方法深搜,但是就是过不了,于是去看关于STL函数的东西,然后。。。

      //本期主要安利博客,因为我想讲的都在博客里。。。

     第一个:  http://blog.csdn.net/y990041769/article/details/8763366

     第二个:  http://www.cnblogs.com/wangkangluo1/archive/2011/07/22/2114118.html

     这两篇讲string的函数还是比较详细的= =我就懒得总结了。。。

     单词接龙这个题,我用了substr函数                  //s.substr(i,j)   截取s串中从i到j的子串;

    = =好像并没有什么要说的了(在你们看完那两篇博客后)

    = =那我就把我的代码放上去吧(习惯左对齐,不喜勿喷)

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int n;
 4 string pp[50];
 5 int aa[50]={0};
 6 string search(int left,int right,bool first)
 7 {
 8 string t;
 9 if (first==true)
10     {
11       if(pp[left][0]==pp[right][0])
12         {
13           t=pp[left];
14         }
15         else
16           {
17             return "";
18           }
19     }
20 else
21   {
22     bool ss = false;
23     int sa=pp[right].length();
24     int sb=pp[left].length();
25     for(long long i=1;i<=min(sa,sb);i++)
26       {
27         string cc=pp[right].substr(pp[right].length()-i);
28         if (cc==pp[left].substr(0,i))
29         {
30           ss = true;
31           t = pp[left].substr(i);
32           break;
33         }
34       }
35 if (!ss) return "";
36 }
37   string t1="";
38   for (int i=0;i<n;i++)
39     {
40       string t2="";
41       if (aa[i]<2)
42         {
43           aa[i]++;
44           t2=search(i,left,false);
45           aa[i]--;
46             if (t2.length()>t1.length()) t1=t2;
47         }
48     }
49     t+=t1;
50     return t;
51 }
52 int main()
53 {
54   cin>>n;
55   for(int i=0;i<n;i++) cin>>pp[i];
56   cin>>pp[n];
57   long long ans=0;
58   for (int i=0;i<n;i++)
59     {
60       aa[i]++;
61       long long t=search(i,n,true).length();
62       aa[i]--;
63       if (t>ans) ans=t;
64     }
65   cout <<ans<< endl;
66   return 0;
67 }

呐,那个神奇的库,就是传说中包含所有头文件的库了= =

#include<bits/stdc++.h>

使用时注意安全= =

原文地址:https://www.cnblogs.com/qianyintinghan/p/5576890.html