1. 程式人生 > >華為機試題-判斷一個數字中是否包含兩個相同的子串

華為機試題-判斷一個數字中是否包含兩個相同的子串

#include <iostream>
#include <sstream>
#include <string>
#include <map>
using namespace std;

int main()
{
     int a;
     cin>>a;
     stringstream stream;
     stream.clear();
     string s;
     stream<<a;
     stream>>s;
     map<string,bool> my_map;
     for(int i=0;i<s.length();i++)
          for(int j=2;j<=s.length()-i;j++)
          {
               string s1 = s.substr(i,j);
               // cout<<s1<<endl;
               if(my_map[s1])
               {
                    cout<<1<<endl;
                    return 0;
               }
               else
               {
                    my_map[s1]=true;
               }
          }
     cout<<0<<endl;
}

使用了字串流來講整數轉換為字串,用到了map來進行子串的標記。